summaryrefslogtreecommitdiff
path: root/libshouldbeinlibc
diff options
context:
space:
mode:
authorJonathan Neuschäfer <j.neuschaefer@gmx.net>2011-08-15 22:10:07 +0200
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2011-08-20 18:27:34 +0200
commitf4094214ebc8ef7c5e8140605bc17907b5672b13 (patch)
treeffadc2efb1e7348bd791fd887897c3652856d547 /libshouldbeinlibc
parent849353cad0ad2b0a2242d7048368e06d96ffdea4 (diff)
fix a realloc-related memory leak in localhost()
* libshouldbeinlibc/localhost.c (localhost): assign the return value of realloc to a temporary variable to avoid losing the old value of buf in the case of realloc failing.
Diffstat (limited to 'libshouldbeinlibc')
-rw-r--r--libshouldbeinlibc/localhost.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/libshouldbeinlibc/localhost.c b/libshouldbeinlibc/localhost.c
index f0c67541..f21f970d 100644
--- a/libshouldbeinlibc/localhost.c
+++ b/libshouldbeinlibc/localhost.c
@@ -39,18 +39,25 @@ localhost ()
errno = 0;
if (buf) {
+ char *new;
buf_len += buf_len;
- buf = realloc (buf, buf_len);
+ new = realloc (buf, buf_len);
+ if (! new)
+ {
+ errno = ENOMEM;
+ return 0;
+ }
+ else
+ buf = new;
} else {
buf_len = 128; /* Initial guess */
buf = malloc (buf_len);
+ if (! buf)
+ {
+ errno = ENOMEM;
+ return 0;
+ }
}
-
- if (! buf)
- {
- errno = ENOMEM;
- return 0;
- }
} while ((gethostname(buf, buf_len) == 0 && !memchr (buf, '\0', buf_len))
|| errno == ENAMETOOLONG);