Skip to content

Commit fdf7296

Browse files
peffgitster
authored andcommitted
probe_utf8_pathname_composition: use internal strbuf
When we are initializing a .git directory, we may call probe_utf8_pathname_composition to detect utf8 mangling. We pass in a path buffer for it to use, and it blindly strcpy()s into it, not knowing whether the buffer is large enough to hold the result or not. In practice this isn't a big deal, because the buffer we pass in already contains "$GIT_DIR/config", and we append only a few extra bytes to it. But we can easily do the right thing just by calling git_path_buf ourselves. Technically this results in a different pathname (before we appended our utf8 characters to the "config" path, and now they get their own files in $GIT_DIR), but that should not matter for our purposes. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e2b021e commit fdf7296

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

builtin/init-db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ static int create_default_files(const char *template_path)
312312
strcpy(path + len, "CoNfIg");
313313
if (!access(path, F_OK))
314314
git_config_set("core.ignorecase", "true");
315-
probe_utf8_pathname_composition(path, len);
315+
probe_utf8_pathname_composition();
316316
}
317317

318318
return reinit;

compat/precompose_utf8.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,26 @@ static size_t has_non_ascii(const char *s, size_t maxlen, size_t *strlen_c)
3636
}
3737

3838

39-
void probe_utf8_pathname_composition(char *path, int len)
39+
void probe_utf8_pathname_composition(void)
4040
{
41+
struct strbuf path = STRBUF_INIT;
4142
static const char *auml_nfc = "\xc3\xa4";
4243
static const char *auml_nfd = "\x61\xcc\x88";
4344
int output_fd;
4445
if (precomposed_unicode != -1)
4546
return; /* We found it defined in the global config, respect it */
46-
strcpy(path + len, auml_nfc);
47-
output_fd = open(path, O_CREAT|O_EXCL|O_RDWR, 0600);
47+
git_path_buf(&path, "%s", auml_nfc);
48+
output_fd = open(path.buf, O_CREAT|O_EXCL|O_RDWR, 0600);
4849
if (output_fd >= 0) {
4950
close(output_fd);
50-
strcpy(path + len, auml_nfd);
51-
precomposed_unicode = access(path, R_OK) ? 0 : 1;
51+
git_path_buf(&path, "%s", auml_nfd);
52+
precomposed_unicode = access(path.buf, R_OK) ? 0 : 1;
5253
git_config_set("core.precomposeunicode", precomposed_unicode ? "true" : "false");
53-
strcpy(path + len, auml_nfc);
54-
if (unlink(path))
55-
die_errno(_("failed to unlink '%s'"), path);
54+
git_path_buf(&path, "%s", auml_nfc);
55+
if (unlink(path.buf))
56+
die_errno(_("failed to unlink '%s'"), path.buf);
5657
}
58+
strbuf_release(&path);
5759
}
5860

5961

compat/precompose_utf8.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ typedef struct {
2727
} PREC_DIR;
2828

2929
void precompose_argv(int argc, const char **argv);
30-
void probe_utf8_pathname_composition(char *, int);
30+
void probe_utf8_pathname_composition(void);
3131

3232
PREC_DIR *precompose_utf8_opendir(const char *dirname);
3333
struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *dirp);

git-compat-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ typedef unsigned long uintptr_t;
229229
#else
230230
#define precompose_str(in,i_nfd2nfc)
231231
#define precompose_argv(c,v)
232-
#define probe_utf8_pathname_composition(a,b)
232+
#define probe_utf8_pathname_composition()
233233
#endif
234234

235235
#ifdef MKDIR_WO_TRAILING_SLASH

0 commit comments

Comments
 (0)