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; }