builtin/mv: don't use memory after free

If 'src' already ends with a slash, then add_slash() will just return it, meaning that 'free(src_with_slash)' is actually 'free(src)'. Since we use 'src' later, this will result in use-after-free. In fact, this cannot happen because 'src' comes from internal_copy_pathspec() without the KEEP_TRAILING_SLASH flag, so any trailing '/' will have been stripped; but static analysis tools are not clever enough to realise this and so warn that 'src' could be used after having been free'd. Fix this by checking that 'src_w_slash' is indeed newly allocated memory. Signed-off-by: John Keeping <john@keeping.me.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Keeping committed Feb 16, 2014 at 16:06 UTC d954828d45efbd4b53576e86066657e87391318d
1 file changed +2 -1
builtin/mv.c
+2 -1
@@ -162,7 +162,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
162 if (strncmp(path, src_w_slash, len_w_slash))
163 break;
164 }
165 - free((char *)src_w_slash);
165 + if (src_w_slash != src)
166 + free((char *)src_w_slash);
167
168 if (last - first < 1)
169 bad = _("source directory is empty");