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

Re: [PATCH v9 6/8] convert: check for detectable errors in UTF encodings

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Mar 6, 2018, 20:55 UTC
Message-ID
<CAPig+cSoka-yBTYBz42JGQTyCH7LDWnToeOvdZfG0_64o9QnBQ@mail.gmail.com>
In-Reply-To
<20180304201418.60958-7-lars.schneider@autodesk.com>
On Sun, Mar 4, 2018 at 3:14 PM,  <lars.schneider@autodesk.com> wrote:
Show 25 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,53 @@ 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)
> +{
> +       if (!memcmp("UTF-", enc, 4)) {
> +               [...]
> +               if (has_prohibited_utf_bom(enc, data, len)) {
> +                       [...]
> +                       if (die_on_error)
> +                               die(error_msg, path, enc);
> +                       else {
> +                               return error(error_msg, path, enc);
> +                       }
> +               } [...]
> +       return 0;
> +}
> @@ -291,6 +338,9 @@ static int encode_to_git(const char *path, const char *src, size_t src_len,
> +       if (validate_encoding(path, enc, src, src_len, die_on_error))
> +               return 0;

There could be a cleaner separation of responsibilities here which, as a nice side-effect, would eliminate the repeated "if (die_on_error) die(...); else return error(...);" pattern. Rather than passing a 'die_on_error' flag to validate_encoding(), it could accept a 'strbuf *err' to populate in case of error:

    static int validate_encoding(..., struct strbuf *err)
    {
        if validation error:
            populate 'err'
            return -1;
        return 0
    }
and let the caller be responsible for deciding how to handle failure:
    struct strbuf err = STRBUF_INIT;
    ...
    if (validate_encoding(..., &err)) {
        if (die_on_error)
            die(err.buf);
        else {
            error(err.buf);
            strbuf_release(&err);
            return 0;
        }
    }

Not necessarily worth a re-roll, but perhaps a cleanup someone could submit at some point if interested.

>         dst = reencode_string_len(src, src_len, default_encoding, enc,
>                                   &dst_len);
Previous: Lars SchneiderNext: lars.schneider@autodesk.com
Message 24 of 26 in “convert: add support for different encodings”
  1. 0/8 convert: add support for different encodingslars.schneider@autodesk.com, Mar 4, 2018
  2. 1/8 strbuf: remove unnecessary NUL assignment in xstrdup_tolower()lars.schneider@autodesk.com, Mar 4, 2018
  3. 3/8 utf8: add function to detect prohibited UTF-16/32 BOMlars.schneider@autodesk.com, Mar 4, 2018
  4. 2/8 strbuf: add xstrdup_toupper()lars.schneider@autodesk.com, Mar 4, 2018
  5. 4/8 utf8: add function to detect a missing UTF-16/32 BOMlars.schneider@autodesk.com, Mar 4, 2018
  6. Junio C HamanoMar 6, 2018
  7. Lars SchneiderMar 6, 2018
  8. Junio C HamanoMar 6, 2018
  9. Lars SchneiderMar 6, 2018
  10. Junio C HamanoMar 6, 2018
  11. Lars SchneiderMar 6, 2018
  12. Junio C HamanoMar 6, 2018
  13. Torsten BögershausenMar 7, 2018
  14. 5/8 convert: add 'working-tree-encoding' attributelars.schneider@autodesk.com, Mar 4, 2018
  15. Eric SunshineMar 6, 2018
  16. Lars SchneiderMar 6, 2018
  17. Eric SunshineMar 6, 2018
  18. 8/8 convert: add round trip check based on 'core.checkRoundtripEncoding'lars.schneider@autodesk.com, Mar 4, 2018
  19. 6/8 convert: check for detectable errors in UTF encodingslars.schneider@autodesk.com, Mar 4, 2018
  20. Junio C HamanoMar 5, 2018
  21. Lars SchneiderMar 5, 2018
  22. Junio C HamanoMar 6, 2018
  23. Lars SchneiderMar 6, 2018
  24. Eric SunshineMar 6, 2018
  25. 7/8 convert: add tracing for 'working-tree-encoding' attributelars.schneider@autodesk.com, Mar 4, 2018
  26. Eric SunshineMar 6, 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.