git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/5] builtin/mv: don't use memory after free

From
John Keeping <john@keeping.me.uk>
Date
Feb 16, 2014, 16:06 UTC
Message-ID
<4df594e05741b2bec1201556d34eb22bab2c235a.1392565571.git.john@keeping.me.uk>
In-Reply-To
<cover.1392565571.git.john@keeping.me.uk>

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>
---
 builtin/mv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/builtin/mv.c b/builtin/mv.c
index 21c46d1..7e26eb5 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -162,7 +162,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 					if (strncmp(path, src_w_slash, len_w_slash))
 						break;
 				}
-				free((char *)src_w_slash);
+				if (src_w_slash != src)
+					free((char *)src_w_slash);
 
 				if (last - first < 1)
 					bad = _("source directory is empty");
-- 
1.9.rc0.187.g6292fff
Previous: John KeepingNext: John Keeping
Message 10 of 13 in “Miscellaneous fixes from static analysis”
  1. 0/5 Miscellaneous fixes from static analysisJohn Keeping, Feb 16, 2014
  2. 1/5 notes-utils: handle boolean notes.rewritemode correctlyJohn Keeping, Feb 16, 2014
  3. David KastrupFeb 16, 2014
  4. Jeff KingFeb 18, 2014
  5. David KastrupFeb 18, 2014
  6. Jeff KingFeb 18, 2014
  7. David KastrupFeb 18, 2014
  8. 2/5 utf8: fix iconv error detectionJohn Keeping, Feb 16, 2014
  9. 3/5 utf8: use correct type for values in interval tableJohn Keeping, Feb 16, 2014
  10. 4/5 builtin/mv: don't use memory after freeJohn Keeping, Feb 16, 2014
  11. 5/5 streaming: simplify attaching a filterJohn Keeping, Feb 16, 2014
  12. Junio C HamanoFeb 18, 2014
  13. Junio C HamanoFeb 19, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.