linux-user: Fix possible realloc memory leak
PATCH 1/1 linux-user: Fix possible realloc memory leak
Extract from "man realloc":
"If realloc() fails the original block is left untouched; it is not freed or moved."
Fix a possible memory leak (reported by cppcheck).
--- linux-user/elfload.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-)
linux-user/elfload.c +5 -3
@@ -1474,7 +1474,7 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)struct elf_shdr *shdr;char *strings;struct syminfo *s; -struct elf_sym *syms; +struct elf_sym *syms, *new_syms; shnum = hdr->e_shnum; i = shnum *sizeof (struct elf_shdr );
@@ -1543,12 +1543,14 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)that we threw away. Whether or not this has any effect on the memory allocation depends on the malloc implementation and how many symbols we managed to discard. */ - syms =realloc (syms, nsyms *sizeof (*syms)); -if (syms ==NULL ) { + new_syms =realloc (syms, nsyms *sizeof (*syms)); +if (new_syms ==NULL ) {free (s); +free (syms);free (strings);return ; } + syms = new_syms;qsort (syms, nsyms,sizeof (*syms), symcmp);
Signature · 1 line
1.7.2.3
Quoted 8 lines · Stefan Weil
Extract from "man realloc":
"Ifrealloc()fails the original block is left untouched; it is not freed or moved."Fix a possible memory leak (reported by cppcheck).
Cc Riku Voipio <[email protected]>Signed-off-by Stefan Weil <[email protected]>
Sidestep the problem via qemu_realloc() instead?
Am 18.01.2011 09:26, schrieb Markus Armbruster:
Quoted 6 lines
Quoted 4 lines · Stefan Weil
Extract from "man realloc":
"Ifrealloc()fails the original block is left untouched; it is not freed or moved."
Quoted 6 lines
Quoted 4 lines
Fix a possible memory leak (reported by cppcheck).
Cc Riku Voipio <[email protected]>Signed-off-by Stefan Weil <[email protected]>Sidestep the problem via
qemu_realloc()instead?
The same change was applied to bsd-user/elfload.c.
As symbol loading is not essential in most applications, returning after out-of-memory should be better than aborting (that's what qemu_realloc does).
Quoted 12 lines · Stefan Weil
Am 18.01.2011 09:26, schrieb Markus Armbruster:
Quoted 11 lines
Quoted 8 lines · Stefan Weil
Extract from "man realloc":
"Ifrealloc()fails the original block is left untouched; it is not freed or moved."Fix a possible memory leak (reported by cppcheck).
Cc Riku Voipio <[email protected]>Signed-off-by Stefan Weil <[email protected]>
Quoted 7 lines
Quoted 1 line
Sidestep the problem via
qemu_realloc()instead?The same change was applied to
bsd-user/elfload.c.As symbol loading is not essential in most applications, returning after out-of-memory should be better than aborting (that's what
qemu_reallocdoes).
Unless the requested size is *really* large, I'd expect this to stave off the out-of-memory failure for a few microseconds at best.
Am 18.01.2011 18:51, schrieb Markus Armbruster:
Quoted 19 lines
Quoted 16 lines · Stefan Weil
Am 18.01.2011 09:26, schrieb Markus Armbruster:
Quoted 14 lines
Quoted 9 lines · Stefan Weil
Extract from "man realloc":
"Ifrealloc()fails the original block is left untouched; it is not freed or moved."Fix a possible memory leak (reported by cppcheck).
Cc Riku Voipio<[email protected]>Signed-off-by Stefan Weil<[email protected]>Sidestep the problem via
qemu_realloc()instead?
Quoted 9 lines
Quoted 6 lines
The same change was applied to
bsd-user/elfload.c.As symbol loading is not essential in most applications, returning after out-of-memory should be better than aborting (that's what
qemu_reallocdoes).Unless the requested size is *really* large, I'd expect this to stave off the out-of-memory failure for a few microseconds at best.
If realloc fails, some memory is released before returning, so maybe you would be surprised that your program finishes without any more problems :-)
Peter Maydell
Reviewed-byQuoted 9 lines · Markus Armbruster
Quoted 8 lines · Stefan Weil
Am 18.01.2011 09:26, schrieb Markus Armbruster:
Quoted 6 lines
Quoted 3 lines · Stefan Weil
Extract from "man realloc":
"Ifrealloc()fails the original block is left untouched; it is not freed or moved."Sidestep the problem via
qemu_realloc()instead?
Quoted 8 lines
Quoted 5 lines
The same change was applied to
bsd-user/elfload.c.As symbol loading is not essential in most applications, returning after out-of-memory should be better than aborting (that's what
qemu_reallocdoes).Unless the requested size is *really* large, I'd expect this to stave off the out-of-memory failure for a few microseconds at best.
Yeah, but the patch is OK, it fixes an actual bug and it does so in line with the malloc-failure handling of the rest of the function. It doesn't seem to me to be important enough an issue to worry about one way or the other.
-- PMM