Blame SOURCES/0002-fix-RESOURCE_LEAK-error-detected-by-covscan-in-src-u.patch
Branch: b522128a935f9ed10451e9408d926c9622509d88
b52212
From 58d91636a0269ded8f550c07fa1a8d362685f7c9 Mon Sep 17 00:00:00 2001
b52212
From: Coiby Xu <coiby.xu@gmail.com>
b52212
Date: Fri, 11 Jun 2021 11:06:59 +0800
b52212
Subject: [PATCH 2/8] fix RESOURCE_LEAK error detected by covscan in src/util.c
b52212
b52212
Fix the following error,
b52212
Error: RESOURCE_LEAK (CWE-772): [#def5]
b52212
python-dmidecode-3.12.2/src/util.c:123: open_fn: Returning handle opened by "open". [Note: The source code implementation of the function has been overridden by a user model.]
b52212
python-dmidecode-3.12.2/src/util.c:123: var_assign: Assigning: "fd" = handle returned from "open(devmem, 0)".
b52212
python-dmidecode-3.12.2/src/util.c:147: noescape: Resource "fd" is not freed or pointed-to in "mmap".
b52212
python-dmidecode-3.12.2/src/util.c:191: leaked_handle: Handle variable "fd" going out of scope leaks the handle.
b52212
b52212
by initializing fd to -1 moving close(fd) to exit.
b52212
b52212
Signed-off-by: Coiby Xu <coiby.xu@gmail.com>
b52212
---
b52212
src/util.c | 9 +++++----
b52212
1 file changed, 5 insertions(+), 4 deletions(-)
b52212
b52212
diff --git a/src/util.c b/src/util.c
b52212
index acef5bd..da97767 100644
b52212
--- a/src/util.c
b52212
+++ b/src/util.c
b52212
@@ -112,7 +112,7 @@ void sigill_handler(int ignore_this) {
b52212
void *mem_chunk(Log_t *logp, size_t base, size_t len, const char *devmem)
b52212
{
b52212
void *p;
b52212
- int fd;
b52212
+ int fd = -1;
b52212
b52212
#ifdef USE_MMAP
b52212
size_t mmoffset;
b52212
@@ -182,10 +182,11 @@ void *mem_chunk(Log_t *logp, size_t base, size_t len, const char *devmem)
b52212
}
b52212
#endif /* USE_MMAP */
b52212
b52212
- if(close(fd) == -1)
b52212
- perror(devmem);
b52212
-
b52212
exit:
b52212
+ if (fd >= 0) {
b52212
+ if(close(fd) == -1)
b52212
+ perror(devmem);
b52212
+ }
b52212
signal(SIGILL, SIG_DFL);
b52212
sigill_logobj = NULL;
b52212
return p;
b52212
--
b52212
2.31.1
b52212