path: root/builtin/mailinfo.c
diff options
authorJeff King <>2017-03-21 01:28:49 (GMT)
committerJunio C Hamano <>2017-03-21 18:18:41 (GMT)
commite4da43b1f063d227b5f7d2922d27458748763a2d (patch)
tree3631b191d2bbd3f7cbc6e98188d65b0fe7c1974a /builtin/mailinfo.c
parent116fb64e439d3744d0f244a51d7a6d714b7703ae (diff)
prefix_filename: return newly allocated string
The prefix_filename() function returns a pointer to static storage, which makes it easy to use dangerously. We already fixed one buggy caller in hash-object recently, and the calls in apply.c are suspicious (I didn't dig in enough to confirm that there is a bug, but we call the function once in apply_all_patches() and then again indirectly from parse_chunk()). Let's make it harder to get wrong by allocating the return value. For simplicity, we'll do this even when the prefix is empty (and we could just return the original file pointer). That will cause us to allocate sometimes when we wouldn't otherwise need to, but this function isn't called in performance critical code-paths (and it already _might_ allocate on any given call, so a caller that cares about performance is questionable anyway). The downside is that the callers need to remember to free() the result to avoid leaking. Most of them already used xstrdup() on the result, so we know they are OK. The remainder have been converted to use free() as appropriate. I considered retaining a prefix_filename_unsafe() for cases where we know the static lifetime is OK (and handling the cleanup is awkward). This is only a handful of cases, though, and it's not worth the mental energy in worrying about whether the "unsafe" variant is OK to use in any situation. Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/mailinfo.c')
1 files changed, 2 insertions, 9 deletions
diff --git a/builtin/mailinfo.c b/builtin/mailinfo.c
index 681f07f..cfb667a 100644
--- a/builtin/mailinfo.c
+++ b/builtin/mailinfo.c
@@ -11,13 +11,6 @@
static const char mailinfo_usage[] =
"git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info";
-static char *prefix_copy(const char *prefix, const char *filename)
- if (!prefix || is_absolute_path(filename))
- return xstrdup(filename);
- return xstrdup(prefix_filename(prefix, filename));
int cmd_mailinfo(int argc, const char **argv, const char *prefix)
const char *def_charset;
@@ -60,8 +53,8 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
mi.input = stdin;
mi.output = stdout;
- msgfile = prefix_copy(prefix, argv[1]);
- patchfile = prefix_copy(prefix, argv[2]);
+ msgfile = prefix_filename(prefix, argv[1]);
+ patchfile = prefix_filename(prefix, argv[2]);
status = !!mailinfo(&mi, msgfile, patchfile);