From d989285ca3dddb7c8e1a36a7e3d93d9f58c5756b Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 11 Aug 2003 16:34:18 +0000 Subject: [PATCH] * readelf.c (process_archive): Fix error handling. Remove memory leak. --- binutils/ChangeLog | 5 ++++- binutils/readelf.c | 26 ++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/binutils/ChangeLog b/binutils/ChangeLog index e0e1d13848..2d7d37657a 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,4 +1,7 @@ -2003-08-11 Ian Lance Taylor +2003-08-11 Ian Lance Taylor + + * readelf.c (process_archive): Fix error handling. Remove memory + leak. * readelf.c: Add ability to read archives. (archive_file_offset): New variable. diff --git a/binutils/readelf.c b/binutils/readelf.c index 25f5de67ea..ac1955e3ae 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -10275,6 +10275,7 @@ process_archive (char *file_name, FILE *file) char *longnames = NULL; unsigned long longnames_size = 0; size_t file_name_size; + int ret; show_name = 1; @@ -10326,6 +10327,7 @@ process_archive (char *file_name, FILE *file) if (fread (longnames, longnames_size, 1, file) != 1) { + free (longnames); error(_("%s: failed to read string table\n"), file_name); return 1; } @@ -10336,6 +10338,8 @@ process_archive (char *file_name, FILE *file) got = fread (&arhdr, 1, sizeof arhdr, file); if (got != sizeof arhdr) { + free (longnames); + if (got == 0) return 0; @@ -10345,6 +10349,7 @@ process_archive (char *file_name, FILE *file) } file_name_size = strlen (file_name); + ret = 0; while (1) { @@ -10360,7 +10365,8 @@ process_archive (char *file_name, FILE *file) if (off >= longnames_size) { error (_("%s: invalid archive string table offset %lu\n"), off); - return 1; + ret = 1; + break; } name = longnames + off; @@ -10375,14 +10381,16 @@ process_archive (char *file_name, FILE *file) if (nameend == NULL) { error (_("%s: bad archive file name\n")); - return 1; + ret = 1; + break; } namealc = malloc (file_name_size + (nameend - name) + 3); if (namealc == NULL) { error (_("Out of memory\n")); - return 1; + ret = 1; + break; } memcpy (namealc, file_name, file_name_size); @@ -10394,7 +10402,7 @@ process_archive (char *file_name, FILE *file) archive_file_offset = ftell (file); archive_file_size = strtoul (arhdr.ar_size, NULL, 10); - process_object (namealc, file); + ret |= process_object (namealc, file); free (namealc); @@ -10405,24 +10413,26 @@ process_archive (char *file_name, FILE *file) SEEK_SET) != 0) { error (_("%s: failed to seek to next archive header\n"), file_name); - return 1; + ret = 1; + break; } got = fread (&arhdr, 1, sizeof arhdr, file); if (got != sizeof arhdr) { if (got == 0) - return 0; + break; error (_("%s: failed to read archive header\n"), file_name); - return 1; + ret = 1; + break; } } if (longnames != 0) free (longnames); - return 0; + return ret; } static int