]> git.ipfire.org Git - thirdparty/git.git/commitdiff
convert: refactor code to clarify ownership of check_roundtrip_encoding
authorPatrick Steinhardt <ps@pks.im>
Mon, 27 May 2024 11:46:25 +0000 (13:46 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 May 2024 18:19:59 +0000 (11:19 -0700)
The `check_roundtrip_encoding` variable is tracked in a `const char *`
even though it may contain allocated strings at times. The result is
that those strings may be leaking because we never free them.

Refactor the code to always store allocated strings in this variable.
The default value is handled in `check_roundtrip()` now, which is the
only user of the variable.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
config.c
convert.c
convert.h
environment.c

index fb56e11276b779b402ccbac4cb364f1092f2db17..f9101045eedb6b4311f7e22a6718fdd7fdf83dc5 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1564,8 +1564,10 @@ static int git_default_core_config(const char *var, const char *value,
                return 0;
        }
 
-       if (!strcmp(var, "core.checkroundtripencoding"))
-               return git_config_string(&check_roundtrip_encoding, var, value);
+       if (!strcmp(var, "core.checkroundtripencoding")) {
+               FREE_AND_NULL(check_roundtrip_encoding);
+               return git_config_string((const char **) &check_roundtrip_encoding, var, value);
+       }
 
        if (!strcmp(var, "core.notesref")) {
                if (!value)
index 35b25eb3cb9212f92dd918d0f247e49ecd620c41..03c3c528f9d8ff44a7d45724730ee7a91b2012ab 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -345,30 +345,32 @@ static int check_roundtrip(const char *enc_name)
         * space separated encodings (eg. "UTF-16, ASCII, CP1125").
         * Search for the given encoding in that string.
         */
-       const char *found = strcasestr(check_roundtrip_encoding, enc_name);
+       const char *encoding = check_roundtrip_encoding ?
+               check_roundtrip_encoding : "SHIFT-JIS";
+       const char *found = strcasestr(encoding, enc_name);
        const char *next;
        int len;
        if (!found)
                return 0;
        next = found + strlen(enc_name);
-       len = strlen(check_roundtrip_encoding);
+       len = strlen(encoding);
        return (found && (
                        /*
-                        * check that the found encoding is at the
-                        * beginning of check_roundtrip_encoding or
-                        * that it is prefixed with a space or comma
+                        * Check that the found encoding is at the beginning of
+                        * encoding or that it is prefixed with a space or
+                        * comma.
                         */
-                       found == check_roundtrip_encoding || (
+                       found == encoding || (
                                (isspace(found[-1]) || found[-1] == ',')
                        )
                ) && (
                        /*
-                        * check that the found encoding is at the
-                        * end of check_roundtrip_encoding or
-                        * that it is suffixed with a space or comma
+                        * Check that the found encoding is at the end of
+                        * encoding or that it is suffixed with a space
+                        * or comma.
                         */
-                       next == check_roundtrip_encoding + len || (
-                               next < check_roundtrip_encoding + len &&
+                       next == encoding + len || (
+                               next < encoding + len &&
                                (isspace(next[0]) || next[0] == ',')
                        )
                ));
index ab8b4fa68d67ba3da1fc7900b616dc70a9967eba..d925589444b90a11058abe87286df3d5add592f8 100644 (file)
--- a/convert.h
+++ b/convert.h
@@ -92,7 +92,7 @@ void convert_attrs(struct index_state *istate,
                   struct conv_attrs *ca, const char *path);
 
 extern enum eol core_eol;
-extern const char *check_roundtrip_encoding;
+extern char *check_roundtrip_encoding;
 const char *get_cached_convert_stats_ascii(struct index_state *istate,
                                           const char *path);
 const char *get_wt_convert_stats_ascii(const char *path);
index 279ea3fd5e0952b6bcb5935b5b338cb53a5e0813..ab6956559eb8dfda15af0a97ca0c95611a30ebde 100644 (file)
@@ -64,7 +64,7 @@ char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
-const char *check_roundtrip_encoding = "SHIFT-JIS";
+char *check_roundtrip_encoding;
 enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
 enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;