convert: refactor code to clarify ownership of check_roundtrip_encoding
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>
Patrick Steinhardt committed
May 27, 2024 at 13:46 UTC
a6cb0cc61033d10eb948057c45dea25c1ab8e151
4 files changed
+19
-15
config.c
+4
-2
@@ -1564,8 +1564,10 @@ static int git_default_core_config(const char *var, const char *value,
1564
return 0;
1565
}
1566
1567
- if (!strcmp(var, "core.checkroundtripencoding"))
1568
- return git_config_string(&check_roundtrip_encoding, var, value);
1567
+ if (!strcmp(var, "core.checkroundtripencoding")) {
1568
+ FREE_AND_NULL(check_roundtrip_encoding);
1569
+ return git_config_string((const char **) &check_roundtrip_encoding, var, value);
1570
+ }
1571
1572
if (!strcmp(var, "core.notesref")) {
1573
if (!value)
convert.c
+13
-11
@@ -345,30 +345,32 @@ static int check_roundtrip(const char *enc_name)
345
* space separated encodings (eg. "UTF-16, ASCII, CP1125").
346
* Search for the given encoding in that string.
347
*/
348
- const char *found = strcasestr(check_roundtrip_encoding, enc_name);
348
+ const char *encoding = check_roundtrip_encoding ?
349
+ check_roundtrip_encoding : "SHIFT-JIS";
350
+ const char *found = strcasestr(encoding, enc_name);
351
const char *next;
352
int len;
353
if (!found)
354
return 0;
355
next = found + strlen(enc_name);
354
- len = strlen(check_roundtrip_encoding);
356
+ len = strlen(encoding);
357
return (found && (
358
/*
357
- * check that the found encoding is at the
358
- * beginning of check_roundtrip_encoding or
359
- * that it is prefixed with a space or comma
359
+ * Check that the found encoding is at the beginning of
360
+ * encoding or that it is prefixed with a space or
361
+ * comma.
362
*/
361
- found == check_roundtrip_encoding || (
363
+ found == encoding || (
364
(isspace(found[-1]) || found[-1] == ',')
365
)
366
) && (
367
/*
366
- * check that the found encoding is at the
367
- * end of check_roundtrip_encoding or
368
- * that it is suffixed with a space or comma
368
+ * Check that the found encoding is at the end of
369
+ * encoding or that it is suffixed with a space
370
+ * or comma.
371
*/
370
- next == check_roundtrip_encoding + len || (
371
- next < check_roundtrip_encoding + len &&
372
+ next == encoding + len || (
373
+ next < encoding + len &&
374
(isspace(next[0]) || next[0] == ',')
375
)
376
));
convert.h
+1
-1
@@ -92,7 +92,7 @@ void convert_attrs(struct index_state *istate,
92
struct conv_attrs *ca, const char *path);
93
94
extern enum eol core_eol;
95
-extern const char *check_roundtrip_encoding;
95
+extern char *check_roundtrip_encoding;
96
const char *get_cached_convert_stats_ascii(struct index_state *istate,
97
const char *path);
98
const char *get_wt_convert_stats_ascii(const char *path);
environment.c
+1
-1
@@ -64,7 +64,7 @@ char *excludes_file;
64
enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
65
enum eol core_eol = EOL_UNSET;
66
int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
67
-const char *check_roundtrip_encoding = "SHIFT-JIS";
67
+char *check_roundtrip_encoding;
68
enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
69
enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
70
enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;