File fix-error-paths-in-mem_chunk.patch of Package dmidecode.28672
From: Jean Delvare <jdelvare@suse.de>
Date: Wed, 14 Oct 2015 14:37:09 +0200
Subject: Fix error paths in mem_chunk
Git-commit: 458f73d58c24a7addce82bf1e8bfb8c2554ca458
Patch-mainline: 3.1
Use a common error path in function mem_chunk, to make sure it does
not leak memory and does not leave an opened file descriptor behind,
without duplicating the cleaning code.
---
util.c | 24 ++++++++++--------------
1 file changed, 10 insertions(+), 14 deletions(-)
--- a/util.c
+++ b/util.c
@@ -169,7 +169,7 @@ void *mem_chunk(off_t base, size_t len,
if ((p = malloc(len)) == NULL)
{
perror("malloc");
- return NULL;
+ goto out;
}
#ifdef USE_MMAP
@@ -177,8 +177,7 @@ void *mem_chunk(off_t base, size_t len,
{
fprintf(stderr, "%s: ", devmem);
perror("stat");
- free(p);
- return NULL;
+ goto err_free;
}
/*
@@ -189,8 +188,7 @@ void *mem_chunk(off_t base, size_t len,
{
fprintf(stderr, "mmap: Can't map beyond end of file %s\n",
devmem);
- free(p);
- return NULL;
+ goto err_free;
}
#ifdef _SC_PAGESIZE
@@ -223,19 +221,17 @@ try_read:
{
fprintf(stderr, "%s: ", devmem);
perror("lseek");
- free(p);
- return NULL;
+ goto err_free;
}
- if (myread(fd, p, len, devmem) == -1)
- {
- free(p);
- return NULL;
- }
+ if (myread(fd, p, len, devmem) == 0)
+ goto out;
+
+err_free:
+ free(p);
+ p = NULL;
-#ifdef USE_MMAP
out:
-#endif
if (close(fd) == -1)
perror(devmem);