[PATCH] Fixes for coverity reported issues in opagent.c

William Cohen <[email protected]>
Newsgroups gmane.linux.oprofile
Message-ID <[email protected]>
The patch fixes the following errors:        :

Error: COMPILER_WARNING:
oprofile-1.0.0git/libopagent/opagent.c: scope_hint: In function 'op_open_agent'
oprofile-1.0.0git/libopagent/opagent.c:205:2: warning: implicit declaration of function 'flock' [-Wimplicit-function-declaration]
  rc = flock(fd, LOCK_EX | LOCK_NB);
  ^

Error: RESOURCE_LEAK (CWE-772):
oprofile-1.0.0git/libopagent/opagent.c:195: alloc_fn: Storage is returned from allocation function "fdopen(int, char const *)".
oprofile-1.0.0git/libopagent/opagent.c:195: var_assign: Assigning: "dumpfile" = storage returned from "fdopen(fd, "w")".
oprofile-1.0.0git/libopagent/opagent.c:213: leaked_storage: Variable "dumpfile" going out of scope leaks the storage it points to.

Error: UNINIT (CWE-457):
oprofile-1.0.0git/libopagent/opagent.c:266: var_decl: Declaring variable "dumpfd" without initializer.
oprofile-1.0.0git/libopagent/opagent.c:285: uninit_use_in_call: Using uninitialized value "dumpfd" when calling "flock()".

Error: COMPILER_WARNING:
oprofile-1.0.0git/libopagent/opagent.c: scope_hint: In function 'op_close_agent'
oprofile-1.0.0git/libopagent/opagent.c:285:5: warning: 'dumpfd' may be used uninitialized in this function [-Wmaybe-uninitialized]
  rc = flock(dumpfd, LOCK_EX | LOCK_NB);
     ^
---
 libopagent/opagent.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/libopagent/opagent.c b/libopagent/opagent.c
index 0d97265..c38d7df 100644
--- a/libopagent/opagent.c
+++ b/libopagent/opagent.c
@@ -63,6 +63,7 @@
 #include <unistd.h>
 #include <time.h>
 #include <bfd.h>
+#include <sys/file.h>
 
 #include "opagent.h"
 #include "op_config.h"
@@ -210,6 +211,7 @@ again:
 			goto again;
 		} else {
 			printf("opagent: Unable to obtain lock on JIT dumpfile\n");
+			fclose(dumpfile);
 			return NULL;
 		}
 	}
@@ -279,6 +281,10 @@ int op_close_agent(op_agent_t hdl)
 	}
 	rec.timestamp = tv.tv_sec;
 
+	if ((dumpfd = fileno(dumpfile)) < 0) {
+		fprintf(stderr, "opagent: Unable to get file descriptor for JIT dumpfile\n");
+		return -1;
+	}
 again:
 	/* We need OS-level file locking here because the opjitconv process may need to
 	 * copy the dumpfile while the JIT agent is still writing to it. */
-- 
1.9.3


------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.