# [PATCH] submodule--helper: do not call utf8_fprintf() unnecessarily

4 messages from 2017-06-28 to 2017-06-28. Participants: Junio C Hamano, Stefan Beller.
Thread: https://gitlist.dev/t/46280

## Junio C Hamano, 2017-06-28 20:38

Subject: [PATCH] submodule--helper: do not call utf8_fprintf() unnecessarily
Message-ID: <xmqq1sq3x2xz.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq1sq3x2xz.fsf%40gitster.mtv.corp.google.com

```
The helper function utf8_fprintf(fp, ...) has exactly the same
effect to the output stream fp as fprintf(fp, ...) does, and the
only difference is that its return value counts in display columns
consumed (assuming that the payload is encoded in UTF-8), as opposed
to number of bytes.

There is no reason to call it unless the caller cares about its
return value.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---

 * The helper was introduced at c0821965 ("Add utf8_fprintf helper
   that returns correct number of columns", 2013-02-09), which also
   taught the help text output from the parse_options API to use it
   to align columns.  These original callers naturally do use the
   returned value and left alone by this fix, which corrects all the
   later callers that misuses it.

 builtin/submodule--helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 8517032b3e..50c6af1de7 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -326,7 +326,7 @@ static int module_list(int argc, const char **argv, const char *prefix)
 			printf("%06o %s %d\t", ce->ce_mode,
 			       oid_to_hex(&ce->oid), ce_stage(ce));
 
-		utf8_fprintf(stdout, "%s\n", ce->name);
+		fprintf(stdout, "%s\n", ce->name);
 	}
 	return 0;
 }
@@ -1038,7 +1038,7 @@ static int update_clone(int argc, const char **argv, const char *prefix)
 		return 1;
 
 	for_each_string_list_item(item, &suc.projectlines)
-		utf8_fprintf(stdout, "%s", item->string);
+		fprintf(stdout, "%s", item->string);
 
 	return 0;
 }

```

## Stefan Beller, 2017-06-28 20:44

Subject: Re: [PATCH] submodule--helper: do not call utf8_fprintf() unnecessarily
Message-ID: <CAGZ79kbvncDoSuj8AF_rdDoEzavdzEaLxqr7NXi1_eCLNt01Kg@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kbvncDoSuj8AF_rdDoEzavdzEaLxqr7NXi1_eCLNt01Kg%40mail.gmail.com
In-Reply-To: <xmqq1sq3x2xz.fsf@gitster.mtv.corp.google.com>

```
On Wed, Jun 28, 2017 at 1:38 PM, Junio C Hamano <gitster@pobox.com> wrote:
> The helper function utf8_fprintf(fp, ...) has exactly the same
> effect to the output stream fp as fprintf(fp, ...) does, and the
> only difference is that its return value counts in display columns
> consumed (assuming that the payload is encoded in UTF-8), as opposed
> to number of bytes.
>
> There is no reason to call it unless the caller cares about its
> return value.
>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
>
>  * The helper was introduced at c0821965 ("Add utf8_fprintf helper
>    that returns correct number of columns", 2013-02-09), which also
>    taught the help text output from the parse_options API to use it
>    to align columns.  These original callers naturally do use the
>    returned value and left alone by this fix, which corrects all the
>    later callers that misuses it.
>

The patch looks correct.

Thanks,
Stefan

>  builtin/submodule--helper.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 8517032b3e..50c6af1de7 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -326,7 +326,7 @@ static int module_list(int argc, const char **argv, const char *prefix)
>                         printf("%06o %s %d\t", ce->ce_mode,
>                                oid_to_hex(&ce->oid), ce_stage(ce));
>
> -               utf8_fprintf(stdout, "%s\n", ce->name);
> +               fprintf(stdout, "%s\n", ce->name);
>         }
>         return 0;
>  }
> @@ -1038,7 +1038,7 @@ static int update_clone(int argc, const char **argv, const char *prefix)
>                 return 1;
>
>         for_each_string_list_item(item, &suc.projectlines)
> -               utf8_fprintf(stdout, "%s", item->string);
> +               fprintf(stdout, "%s", item->string);
>
>         return 0;
>  }

```

## Junio C Hamano, 2017-06-28 20:58

Subject: Re: [PATCH] submodule--helper: do not call utf8_fprintf() unnecessarily
Message-ID: <xmqqwp7vvngg.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqwp7vvngg.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CAGZ79kbvncDoSuj8AF_rdDoEzavdzEaLxqr7NXi1_eCLNt01Kg@mail.gmail.com>

```
Stefan Beller <sbeller@google.com> writes:

> On Wed, Jun 28, 2017 at 1:38 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> The helper function utf8_fprintf(fp, ...) has exactly the same
>> effect to the output stream fp as fprintf(fp, ...) does, and the
>> only difference is that its return value counts in display columns
>> consumed (assuming that the payload is encoded in UTF-8), as opposed
>> to number of bytes.
>>
>> There is no reason to call it unless the caller cares about its
>> return value.
>>
>> Signed-off-by: Junio C Hamano <gitster@pobox.com>
>> ---
>>
>>  * The helper was introduced at c0821965 ("Add utf8_fprintf helper
>>    that returns correct number of columns", 2013-02-09), which also
>>    taught the help text output from the parse_options API to use it
>>    to align columns.  These original callers naturally do use the
>>    returned value and left alone by this fix, which corrects all the
>>    later callers that misuses it.
>>
>
> The patch looks correct.

Thanks.  I had a small voice back in my head telling me that I may
have misread the code and this patch breaks things, which you
cleared up for me ;-)

```

## Stefan Beller, 2017-06-28 21:18

Subject: Re: [PATCH] submodule--helper: do not call utf8_fprintf() unnecessarily
Message-ID: <CAGZ79kY++zFEGDZhPMQebbGBw7UXjUf6f+bOzZ58_5NLQpCcjg@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kY%2B%2BzFEGDZhPMQebbGBw7UXjUf6f%2BbOzZ58_5NLQpCcjg%40mail.gmail.com
In-Reply-To: <xmqqwp7vvngg.fsf@gitster.mtv.corp.google.com>

```
On Wed, Jun 28, 2017 at 1:58 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Stefan Beller <sbeller@google.com> writes:
>
>> On Wed, Jun 28, 2017 at 1:38 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>> The helper function utf8_fprintf(fp, ...) has exactly the same
>>> effect to the output stream fp as fprintf(fp, ...) does, and the
>>> only difference is that its return value counts in display columns
>>> consumed (assuming that the payload is encoded in UTF-8), as opposed
>>> to number of bytes.
>>>
>>> There is no reason to call it unless the caller cares about its
>>> return value.
>>>
>>> Signed-off-by: Junio C Hamano <gitster@pobox.com>
>>> ---
>>>
>>>  * The helper was introduced at c0821965 ("Add utf8_fprintf helper
>>>    that returns correct number of columns", 2013-02-09), which also
>>>    taught the help text output from the parse_options API to use it
>>>    to align columns.  These original callers naturally do use the
>>>    returned value and left alone by this fix, which corrects all the
>>>    later callers that misuses it.
>>>
>>
>> The patch looks correct.

I said this because I had a similar implementation a couple weeks back
when Peff tried to poke (security) holes into submodule usage.

I tried finding the reason why it was originally introduced, but to no avail.
It seems to be randomly introduced.

> Thanks.  I had a small voice back in my head telling me that I may
> have misread the code and this patch breaks things, which you
> cleared up for me ;-)

That said, #include "utf8.h" could also go from the file with this
or after this patch, I believe.

```
