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>

Jeff King committed Oct 4, 2015 at 23:45 UTC fdf729661a777d8bd598f40055d92b2df5601332
4 files changed +13 -11
builtin/init-db.c
+1 -1
@@ -312,7 +312,7 @@ static int create_default_files(const char *template_path)
312 strcpy(path + len, "CoNfIg");
313 if (!access(path, F_OK))
314 git_config_set("core.ignorecase", "true");
315 - probe_utf8_pathname_composition(path, len);
315 + probe_utf8_pathname_composition();
316 }
317
318 return reinit;
compat/precompose_utf8.c
+10 -8
@@ -36,24 +36,26 @@ static size_t has_non_ascii(const char *s, size_t maxlen, size_t *strlen_c)
36 }
37
38
39 -void probe_utf8_pathname_composition(char *path, int len)
39 +void probe_utf8_pathname_composition(void)
40 {
41 + struct strbuf path = STRBUF_INIT;
42 static const char *auml_nfc = "\xc3\xa4";
43 static const char *auml_nfd = "\x61\xcc\x88";
44 int output_fd;
45 if (precomposed_unicode != -1)
46 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);
49 if (output_fd >= 0) {
50 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;
53 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);
57 }
58 + strbuf_release(&path);
59 }
60
61
compat/precompose_utf8.h
+1 -1
@@ -27,7 +27,7 @@ typedef struct {
27 } PREC_DIR;
28
29 void precompose_argv(int argc, const char **argv);
30 -void probe_utf8_pathname_composition(char *, int);
30 +void probe_utf8_pathname_composition(void);
31
32 PREC_DIR *precompose_utf8_opendir(const char *dirname);
33 struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *dirp);
git-compat-util.h
+1 -1
@@ -229,7 +229,7 @@ typedef unsigned long uintptr_t;
229 #else
230 #define precompose_str(in,i_nfd2nfc)
231 #define precompose_argv(c,v)
232 -#define probe_utf8_pathname_composition(a,b)
232 +#define probe_utf8_pathname_composition()
233 #endif
234
235 #ifdef MKDIR_WO_TRAILING_SLASH