git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v10 7/9] convert: check for detectable errors in UTF encodings

From
Lars Schneider <larsxschneider@gmail.com>
Date
Mar 7, 2018, 22:12 UTC
Message-ID
<B001E8E2-9C98-4136-87A3-4425C50E90DB@gmail.com>
In-Reply-To
<xmqqr2ovzmwo.fsf@gitster-ct.c.googlers.com>
Show 23 quoted lines
> On 07 Mar 2018, at 20:49, Junio C Hamano <gitster@pobox.com> wrote:
> 
> lars.schneider@autodesk.com writes:
> 
>> +static int validate_encoding(const char *path, const char *enc,
>> +		      const char *data, size_t len, int die_on_error)
>> +{
>> +	/* We only check for UTF here as UTF?? can be an alias for UTF-?? */
>> +	if (startscase_with(enc, "UTF")) {
>> +		/*
>> +		 * Check for detectable errors in UTF encodings
>> +		 */
>> +		if (has_prohibited_utf_bom(enc, data, len)) {
>> +			const char *error_msg = _(
>> +				"BOM is prohibited in '%s' if encoded as %s");
>> +			/*
>> +			 * This advice is shown for UTF-??BE and UTF-??LE encodings.
>> +			 * We cut off the last two characters of the encoding name
>> +			 # to generate the encoding name suitable for BOMs.
>> +			 */
> 
> Yuck.  The code pretends to abstract away the details in a helper
> has_prohibited_x() yet the caller still knows quite a lot.

True, but has_prohibited_x() cannot create a proper error/advise message unless we give it more parameters (e.g. path name). Therefore, I don't see a better way right now.

Show 21 quoted lines
>> +			const char *advise_msg = _(
>> +				"The file '%s' contains a byte order "
>> +				"mark (BOM). Please use %s as "
>> +				"working-tree-encoding.");
>> +			char *upper_enc = xstrdup_toupper(enc);
>> +			upper_enc[strlen(upper_enc)-2] = '\0';
>> +			advise(advise_msg, path, upper_enc);
>> +			free(upper_enc);
> 
> I think this up-casing is more problematic than without, not from
> the point of view of the internal code, but from the point of view
> of the end user experience.  When the user writes utf16le or
> utf-16le and the data does not trigger the BOM check, we are likely
> to successfully convert it.  I do not see the merit of suggesting
> UTF16 or UTF-16 in such a case, over telling them to just drop the
> byte-order suffix from the encoding names (i.e. utf16 or utf-16).
> 
> If you are trying to force/nudge people in the direction of
> canonical way of spelling things (which may not be a bad idea), then
> "utf16le" as the original input would want to result in "UTF-16"
> with dash in the advise, no?
Correct. In the error messages I kept the encoding name "as-is" and
only in the advise message I used the uppercase variant to steer
people into the canonical direction. My initial reason for this was
that in is_missing_required_utf_bom() we add "BE/LE" to the encoding
in the advise message. Let's say the user used "Utf-16" as encoding.
 Should "BE/LE" be upper case or lower case? To avoid that question 
I made it always upper case.

I also would have liked to advise "UTF-16" instead of "UTF16" as you suggested. However, that required a few more lines and I wanted to keep the change to a minimum. I feel this could be added in a follow up patch.

> On the other hand, if we are not enforcing such a policy decision
> but merely explaining a way to work around this check, then it may
> be better to give a variant with the smaller difference from the
> original (i.e. without up-casing).
See example mentioned above: "Utf-16". How would you handle that?

Thanks, Lars

Previous: Junio C HamanoNext: Junio C Hamano
Message 21 of 32 in “convert: add support for different encodings”
  1. 0/9 convert: add support for different encodingslars.schneider@autodesk.com, Mar 7, 2018
  2. 1/9 strbuf: remove unnecessary NUL assignment in xstrdup_tolower()lars.schneider@autodesk.com, Mar 7, 2018
  3. 2/9 strbuf: add xstrdup_toupper()lars.schneider@autodesk.com, Mar 7, 2018
  4. 3/9 strbuf: add a case insensitive starts_with()lars.schneider@autodesk.com, Mar 7, 2018
  5. Duy NguyenMar 8, 2018
  6. Junio C HamanoMar 8, 2018
  7. Lars SchneiderMar 9, 2018
  8. Junio C HamanoMar 9, 2018
  9. Ævar Arnfjörð BjarmasonMar 9, 2018
  10. 4/9 utf8: add function to detect prohibited UTF-16/32 BOMlars.schneider@autodesk.com, Mar 7, 2018
  11. 5/9 utf8: add function to detect a missing UTF-16/32 BOMlars.schneider@autodesk.com, Mar 7, 2018
  12. 6/9 convert: add 'working-tree-encoding' attributelars.schneider@autodesk.com, Mar 7, 2018
  13. Eric SunshineMar 7, 2018
  14. Lars SchneiderMar 7, 2018
  15. Junio C HamanoMar 7, 2018
  16. Junio C HamanoMar 7, 2018
  17. 7/9 convert: check for detectable errors in UTF encodingslars.schneider@autodesk.com, Mar 7, 2018
  18. Eric SunshineMar 7, 2018
  19. Lars SchneiderMar 9, 2018
  20. Junio C HamanoMar 7, 2018
  21. Lars SchneiderMar 7, 2018
  22. Junio C HamanoMar 7, 2018
  23. Lars SchneiderMar 7, 2018
  24. Junio C HamanoMar 7, 2018
  25. Lars SchneiderMar 7, 2018
  26. Junio C HamanoMar 7, 2018
  27. 9/9 convert: add round trip check based on 'core.checkRoundtripEncoding'lars.schneider@autodesk.com, Mar 7, 2018
  28. Junio C HamanoMar 7, 2018
  29. Lars SchneiderMar 7, 2018
  30. Junio C HamanoMar 7, 2018
  31. Lars SchneiderMar 7, 2018
  32. 8/9 convert: add tracing for 'working-tree-encoding' attributelars.schneider@autodesk.com, Mar 7, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.