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

Fix alloc_filespec() initialization

From
Linus Torvalds <torvalds@osdl.org>
Date
Sep 14, 2005, 20:41 UTC
Message-ID
<Pine.LNX.4.58.0509141334480.26803@g5.osdl.org>
In-Reply-To
<Pine.LNX.4.58.0509141321180.26803@g5.osdl.org>

This simplifies and fixes the initialization of a "diff_filespec" when allocated.

The old code would not initialize "sha1_valid". Noticed by valgrind.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

This does not fix the issue Wayne saw, but I'm not going to look at the later valgrind errors before I've fixed the first ones.

On Wed, 14 Sep 2005, Linus Torvalds wrote:
Show 8 quoted lines
> 
> I get even more, including:
> 
> 	==3234== Use of uninitialised value of size 4
> 	==3234==    at 0x80507C1: alloc_filespec (diff.c:224)
> 	==3234==    by 0x8052387: diff_addremove (diff.c:1144)
> 	==3234==    by 0x8049B74: show_file (diff-tree.c:97)
> 	==3234==    by 0x8049E17: diff_tree (diff-tree.c:118)
diff --git a/diff.c b/diff.c
--- a/diff.c
+++ b/diff.c
@@ -214,14 +214,10 @@ struct diff_filespec *alloc_filespec(con
 {
 	int namelen = strlen(path);
 	struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
+
+	memset(spec, 0, sizeof(*spec));
 	spec->path = (char *)(spec + 1);
-	strcpy(spec->path, path);
-	spec->should_free = spec->should_munmap = 0;
-	spec->xfrm_flags = 0;
-	spec->size = 0;
-	spec->data = NULL;
-	spec->mode = 0;
-	memset(spec->sha1, 0, 20);
+	memcpy(spec->path, path, namelen+1);
 	return spec;
 }
 
Previous: Linus TorvaldsNext: Paul Mackerras
Message 5 of 23 in “git-diff-tree rename detection bug”
  1. Wayne ScottSep 14, 2005
  2. Junio C HamanoSep 14, 2005
  3. Wayne ScottSep 14, 2005
  4. Linus TorvaldsSep 14, 2005
  5. Fix alloc_filespec() initializationLinus Torvalds, Sep 14, 2005
  6. Paul MackerrasSep 15, 2005
  7. Paul MackerrasSep 15, 2005
  8. Linus TorvaldsSep 15, 2005
  9. Paul MackerrasSep 15, 2005
  10. Junio C HamanoSep 15, 2005
  11. Josef WeidendorferSep 15, 2005
  12. Paul MackerrasSep 20, 2005
  13. Linus TorvaldsSep 15, 2005
  14. Junio C HamanoSep 16, 2005
  15. H. Peter AnvinSep 15, 2005
  16. Junio C HamanoSep 15, 2005
  17. Linus TorvaldsSep 15, 2005
  18. Matthias UrlichsSep 16, 2005
  19. Nicolas PitreSep 15, 2005
  20. Junio C HamanoSep 14, 2005
  21. Junio C HamanoSep 14, 2005
  22. Linus TorvaldsSep 14, 2005
  23. Junio C HamanoSep 14, 2005

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.