Skip to contentSkip to conversation

parse-options: report uncorrupted multi-byte options

PATCH In review Erik Faye-Lund 4 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 parse-options: report uncorrupted multi-byte options

Because our command-line parser considers only one byte at the time for short-options, we incorrectly report only the first byte when multi-byte input was provided. This makes user-erros slightly awkward to diagnose for instance under UTF-8 locale and non-English keyboard layouts.

Make the reporting code report the whole argument-string when a non-ASCII short-option is detected.

---

Here's a second attempt at fixing error-reporting with UTF-8 encoded input, this time without corrupting other non-ascii multi-byte encodings.

I decided to change the text from what Jeff suggested; all we know is that it's non-ASCII. It might be Latin-1 or some other non-ASCII, single byte encoding. And since we're trying not to care, let's also try to not be overly specific :)

I wasn't entirely sure who to attribute for the improvement, so I just picked Jeff; he provided some code. That decision might not be correct, feel free to change it.

 parse-options.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
parse-options.c +4 -1
index 67e98a6..6a39446 100644
@@ -461,8 +461,11 @@ int parse_options(int argc, const char **argv, const char *prefix,
 	default: /* PARSE_OPT_UNKNOWN */
 		if (ctx.argv[0][1] == '-') {
 			error("unknown option `%s'", ctx.argv[0] + 2);
-		} else {
+		} else if (isascii(*ctx.opt)) {
 			error("unknown switch `%c'", *ctx.opt);
+		} else {
+			error("unknown non-ascii option in string: `%s'",
+			    ctx.argv[0]);
 		}
 		usage_with_options(usagestr, options);
 	}
Signature · 1 line
1.8.1.1
#1 lore raw
Quoted 4 lines · Erik Faye-Lund

I decided to change the text from what Jeff suggested; all we know is that it's non-ASCII. It might be Latin-1 or some other non-ASCII, single byte encoding. And since we're trying not to care, let's also try to not be overly specific :)

Yeah, that makes more sense (I did not put too much thought into the original wording). Thanks.

-Peff

#3 lore raw
In reply to Erik Faye-Lund
Quoted 8 lines · Erik Faye-Lund

Because our command-line parser considers only one byte at the time for short-options, we incorrectly report only the first byte when multi-byte input was provided. This makes user-erros slightly awkward to diagnose for instance under UTF-8 locale and non-English keyboard layouts.

Make the reporting code report the whole argument-string when a non-ASCII short-option is detected.

Similar cases:

config.c:git_default_core_config() assumes core.commentchar is ascii. We should catch and report non-ascii chars, or simply accept it as a string.

builtin/update-index.c:cmd_update_index(): error("unknown switch '%c'", *ctx.opt);

builtin/apply.c:apply_one_fragment(): error(_("invalid start of line:
'%c'"), first); where 'first' may be a part of utf-8 from a broken patch.

Signature · 1 line
Duy
#4 lore raw
Quoted 7 lines · Duy Nguyen
Quoted 6 lines · Erik Faye-Lund

Because our command-line parser considers only one byte at the time for short-options, we incorrectly report only the first byte when multi-byte input was provided. This makes user-erros slightly awkward to diagnose for instance under UTF-8 locale and non-English keyboard layouts.

Quoted 8 lines
Quoted 2 lines

Make the reporting code report the whole argument-string when a non-ASCII short-option is detected.

Similar cases:

config.c:git_default_core_config() assumes core.commentchar is ascii. We should catch and report non-ascii chars, or simply accept it as a string.

That one is just an uninterpreted byte. core.commentString might be a nice extension to the concept, but it is an entirely different category.

Quoted 2 lines

builtin/update-index.c:cmd_update_index(): error("unknown switch '%c'", *ctx.opt);

This one is in the same category as this topic.

Quoted 3 lines

builtin/apply.c:apply_one_fragment(): error(_("invalid start of line:
'%c'"), first); where 'first' may be a part of utf-8 from a broken patch.

This is where the patch is expected to have either " ", "-" or "+", again, anything else is an uninterpreted byte. It is more like reporting the file we found an error in, whose filename is not encoded in UTF-8 to the user's terminal.

#5 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 5 lines

Similar cases:

config.c:git_default_core_config() assumes core.commentchar is ascii. We should catch and report non-ascii chars, or simply accept it as a string.

That one is just an uninterpreted byte. core.commentString might be a nice extension to the concept, but it is an entirely different category.

My point is not to output broken utf-8 if we can. If someone accidentally puts a UTF-8 character in core.commentChar, it will produce broken utf-8 templates that editors might react, but hard to see by eye. Something like this may give sufficient protection:

config.c +0 -0
index aefd80b..b6f73e0 100644
@@ -726,8 +726,11 @@ static int git_default_core_config(const char

*var, const char *value)

        if (!strcmp(var, "core.commentchar")) {
                const char *comment;
                int ret = git_config_string(&comment, var, value);

- if (!ret)
+ if (!ret) {
+ if (comment[1])
+ return error("core.commentchar must be

one ASCII character");
                        comment_line_char = comment[0];
+               }
                return ret;
        }
Signature · 1 line
Duy