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
Eric Sunshine <sunshine@sunshineco.com>
Date
Mar 7, 2018, 18:04 UTC
Message-ID
<CAPig+cTsRufRNZbKJS-02fvbtaLB30FO3Em6HR_LZrsK+CfOjw@mail.gmail.com>
In-Reply-To
<20180307173026.30058-8-lars.schneider@autodesk.com>
On Wed, Mar 7, 2018 at 12:30 PM,  <lars.schneider@autodesk.com> wrote:
Show 22 quoted lines
> Check that new content is valid with respect to the user defined
> 'working-tree-encoding' attribute.
>
> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>
> ---
> diff --git a/convert.c b/convert.c
> @@ -266,6 +266,58 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
> +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.
s/#/*/
Show 7 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';

Due to startscase_with(...,"UTF"), we know at this point that the string is at least 3 characters long, thus it's safe to back up by 2. Good.

Show 16 quoted lines
> +                       advise(advise_msg, path, upper_enc);
> +                       free(upper_enc);
> +                       if (die_on_error)
> +                               die(error_msg, path, enc);
> +                       else {
> +                               return error(error_msg, path, enc);
> +                       }
> diff --git a/t/t0028-working-tree-encoding.sh b/t/t0028-working-tree-encoding.sh
> @@ -62,6 +62,46 @@ test_expect_success 'check $GIT_DIR/info/attributes support' '
>  for i in 16 32
>  do
> +       test_expect_success "check prohibited UTF-${i} BOM" '
> +               test_when_finished "git reset --hard HEAD" &&
> +
> +               echo "*.utf${i}be text working-tree-encoding=utf-${i}be" >>.gitattributes &&
> +               echo "*.utf${i}le text working-tree-encoding=utf-${i}le" >>.gitattributes &&

v10 is checking only hyphenated lowercase encoding name; earlier versions checked uppercase. For better coverage, it would be nice to check several combinations: all uppercase, all lowercase, mixed case, hyphenated, not hyphenated.

I'm not suggesting running all the tests repeatedly but rather just varying the format of the encoding name in these tests you're adding. For instance, the above could instead be:

    echo "*.utf${i}be text working-tree-encoding=UTF-${i}be" >>.gitattributes &&
    echo "*.utf${i}le text working-tree-encoding=utf${i}LE" >>.gitattributes &&
or something.
Show 24 quoted lines
> +               # Here we add a UTF-16 (resp. UTF-32) files with BOM (big/little-endian)
> +               # but we tell Git to treat it as UTF-16BE/UTF-16LE (resp. UTF-32).
> +               # In these cases the BOM is prohibited.
> +               cp bebom.utf${i}be.raw bebom.utf${i}be &&
> +               test_must_fail git add bebom.utf${i}be 2>err.out &&
> +               test_i18ngrep "fatal: BOM is prohibited .* utf-${i}be" err.out &&
> +
> +               cp lebom.utf${i}le.raw lebom.utf${i}be &&
> +               test_must_fail git add lebom.utf${i}be 2>err.out &&
> +               test_i18ngrep "fatal: BOM is prohibited .* utf-${i}be" err.out &&
> +
> +               cp bebom.utf${i}be.raw bebom.utf${i}le &&
> +               test_must_fail git add bebom.utf${i}le 2>err.out &&
> +               test_i18ngrep "fatal: BOM is prohibited .* utf-${i}le" err.out &&
> +
> +               cp lebom.utf${i}le.raw lebom.utf${i}le &&
> +               test_must_fail git add lebom.utf${i}le 2>err.out &&
> +               test_i18ngrep "fatal: BOM is prohibited .* utf-${i}le" err.out
> +       '
> +
> +       test_expect_success "check required UTF-${i} BOM" '
> +               test_when_finished "git reset --hard HEAD" &&
> +
> +               echo "*.utf${i} text working-tree-encoding=utf-${i}" >>.gitattributes &&

This is another opportunity for checking a variation (case, hyphenation) of the encoding name rather than using only hyphenated lowercase.

Show 9 quoted lines
> +
> +               cp nobom.utf${i}be.raw nobom.utf${i} &&
> +               test_must_fail git add nobom.utf${i} 2>err.out &&
> +               test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out &&
> +
> +               cp nobom.utf${i}le.raw nobom.utf${i} &&
> +               test_must_fail git add nobom.utf${i} 2>err.out &&
> +               test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out
> +       '
Previous: lars.schneider@autodesk.comNext: Lars Schneider
Message 18 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.