Skip to content

Commit a6cb0cc

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f9c1989 commit a6cb0cc

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

config.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,8 +1564,10 @@ static int git_default_core_config(const char *var, const char *value,
15641564
return 0;
15651565
}
15661566

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+
}
15691571

15701572
if (!strcmp(var, "core.notesref")) {
15711573
if (!value)

convert.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,30 +345,32 @@ static int check_roundtrip(const char *enc_name)
345345
* space separated encodings (eg. "UTF-16, ASCII, CP1125").
346346
* Search for the given encoding in that string.
347347
*/
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);
349351
const char *next;
350352
int len;
351353
if (!found)
352354
return 0;
353355
next = found + strlen(enc_name);
354-
len = strlen(check_roundtrip_encoding);
356+
len = strlen(encoding);
355357
return (found && (
356358
/*
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.
360362
*/
361-
found == check_roundtrip_encoding || (
363+
found == encoding || (
362364
(isspace(found[-1]) || found[-1] == ',')
363365
)
364366
) && (
365367
/*
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.
369371
*/
370-
next == check_roundtrip_encoding + len || (
371-
next < check_roundtrip_encoding + len &&
372+
next == encoding + len || (
373+
next < encoding + len &&
372374
(isspace(next[0]) || next[0] == ',')
373375
)
374376
));

convert.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void convert_attrs(struct index_state *istate,
9292
struct conv_attrs *ca, const char *path);
9393

9494
extern enum eol core_eol;
95-
extern const char *check_roundtrip_encoding;
95+
extern char *check_roundtrip_encoding;
9696
const char *get_cached_convert_stats_ascii(struct index_state *istate,
9797
const char *path);
9898
const char *get_wt_convert_stats_ascii(const char *path);

environment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ char *excludes_file;
6464
enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
6565
enum eol core_eol = EOL_UNSET;
6666
int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
67-
const char *check_roundtrip_encoding = "SHIFT-JIS";
67+
char *check_roundtrip_encoding;
6868
enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
6969
enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
7070
enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;

0 commit comments

Comments
 (0)