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

Re: [PATCH v8 3/7] utf8: add function to detect prohibited UTF-16/32 BOM

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Feb 25, 2018, 03:41 UTC
Message-ID
<CAPig+cQdoicxSMLRbdCfFGXyXLirbRLVJi2QahCSe1ZesVOKBg@mail.gmail.com>
In-Reply-To
<20180224162801.98860-4-lars.schneider@autodesk.com>
On Sat, Feb 24, 2018 at 11:27 AM,  <lars.schneider@autodesk.com> wrote:
Show 22 quoted lines
> Whenever a data stream is declared to be UTF-16BE, UTF-16LE, UTF-32BE
> or UTF-32LE a BOM must not be used [1]. The function returns true if
> this is the case.
>
> [1] http://unicode.org/faq/utf_bom.html#bom10
>
> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>
> ---
> diff --git a/utf8.c b/utf8.c
> @@ -538,6 +538,30 @@ char *reencode_string_len(const char *in, int insz,
> +int has_prohibited_utf_bom(const char *enc, const char *data, size_t len)
> +{
> +       return (
> +         (!strcmp(enc, "UTF-16BE") || !strcmp(enc, "UTF-16LE")) &&
> +         (has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||
> +          has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))
> +       ) || (
> +         (!strcmp(enc, "UTF-32BE") || !strcmp(enc, "UTF-32LE")) &&
> +         (has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||
> +          has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))
> +       );
> +}

Is this interpretation correct? When I read [1], I interpret it as saying that no BOM _of any sort_ should be present when the encoding is declared as one of UTF-16BE, UTF-16LE, UTF-32BE, or UTF-32LE. This code, on the other hand, only checks for BOMs corresponding to the declared size (16 or 32 bits).

I suppose the intention of [1] is to detect a mismatch between the declared encoding and how the stream is actually encoded. The check implemented here will fail to detect a mismatch between, say, declared encoding UTF-16BE and actual encoding UTF-32BE.

Previous: lars.schneider@autodesk.comNext: Lars Schneider
Message 5 of 20 in “convert: add support for different encodings”
  1. 0/7 convert: add support for different encodingslars.schneider@autodesk.com, Feb 24, 2018
  2. 1/7 strbuf: remove unnecessary NUL assignment in xstrdup_tolower()lars.schneider@autodesk.com, Feb 24, 2018
  3. 2/7 strbuf: add xstrdup_toupper()lars.schneider@autodesk.com, Feb 24, 2018
  4. 3/7 utf8: add function to detect prohibited UTF-16/32 BOMlars.schneider@autodesk.com, Feb 24, 2018
  5. Eric SunshineFeb 25, 2018
  6. Lars SchneiderFeb 25, 2018
  7. Eric SunshineFeb 27, 2018
  8. Lars SchneiderFeb 28, 2018
  9. 5/7 convert: add 'working-tree-encoding' attributelars.schneider@autodesk.com, Feb 24, 2018
  10. Eric SunshineFeb 25, 2018
  11. Lars SchneiderFeb 27, 2018
  12. Eric SunshineFeb 28, 2018
  13. 6/7 convert: add tracing for 'working-tree-encoding' attributelars.schneider@autodesk.com, Feb 24, 2018
  14. 4/7 utf8: add function to detect a missing UTF-16/32 BOMlars.schneider@autodesk.com, Feb 24, 2018
  15. Eric SunshineFeb 25, 2018
  16. Lars SchneiderFeb 25, 2018
  17. 7/7 convert: add round trip check based on 'core.checkRoundtripEncoding'lars.schneider@autodesk.com, Feb 24, 2018
  18. Eric SunshineFeb 25, 2018
  19. Lars SchneiderMar 4, 2018
  20. Eric SunshineMar 4, 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.