Blame SOURCES/oprofile-coverity.patch

d5df0a
commit be6d22999668ac976acd2008ec13db4385a0c8dd
d5df0a
Author: Maynard Johnson <maynardj@us.ibm.com>
d5df0a
Date:   Mon Jan 27 15:44:18 2014 -0600
d5df0a
d5df0a
    Fix issues detected by Coverity
d5df0a
    
d5df0a
    Will Cohen ran Coverity against oprofile and reported some issues
d5df0a
    on Nov 20, 2013.  I submitted the current oprofile source to the
d5df0a
    Coverity webpage, and a couple new issues were detected. This
d5df0a
    patch addresses most of these issues.  Some issues are either
d5df0a
    false positives from Coverity's analysis or have been marked
d5df0a
    as "Intentional" so as to have Coverity ignore them.
d5df0a
    
d5df0a
    Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
d5df0a
d5df0a
diff --git a/daemon/init.c b/daemon/init.c
d5df0a
index 2882c49..1fed812 100644
d5df0a
--- a/daemon/init.c
d5df0a
+++ b/daemon/init.c
d5df0a
@@ -154,7 +154,7 @@ static void opd_do_jitdumps(void)
d5df0a
 	struct timeval tv;
d5df0a
 	char end_time_str[32];
d5df0a
 	char opjitconv_path[PATH_MAX + 1];
d5df0a
-	char * exec_args[7];
d5df0a
+	char * exec_args[8];
d5df0a
 
d5df0a
 	if (jit_conversion_running)
d5df0a
 		return;
d5df0a
@@ -175,6 +175,7 @@ static void opd_do_jitdumps(void)
d5df0a
 			if (vmisc)
d5df0a
 				exec_args[arg_num++] = "-d";
d5df0a
 			exec_args[arg_num++] = "--delete-jitdumps";
d5df0a
+			exec_args[arg_num++] = "--session-dir";
d5df0a
 			exec_args[arg_num++] = session_dir;
d5df0a
 			exec_args[arg_num++] = start_time_str;
d5df0a
 			exec_args[arg_num++] = end_time_str;
d5df0a
diff --git a/libpe_utils/op_pe_utils.cpp b/libpe_utils/op_pe_utils.cpp
d5df0a
index aa0c1c5..0b7482f 100644
d5df0a
--- a/libpe_utils/op_pe_utils.cpp
d5df0a
+++ b/libpe_utils/op_pe_utils.cpp
d5df0a
@@ -487,7 +487,7 @@ handle_named_um:
d5df0a
 				(endptr <= (mask + strlen(mask) - 2))) { // '- 2' to account for linefeed and '\0'
d5df0a
 
d5df0a
 			// Must be a default named unit mask
d5df0a
-			strncpy(event->um_name, mask, OP_MAX_UM_NAME_LEN);
d5df0a
+			strncpy(event->um_name, mask, OP_MAX_UM_NAME_LEN - 1);
d5df0a
 			goto handle_named_um;
d5df0a
 		}
d5df0a
 		config |= ((event->evt_um & 0xFFULL) << 8);
d5df0a
diff --git a/libutil++/op_bfd.h b/libutil++/op_bfd.h
d5df0a
index 6ce71fa..1aa7e10 100644
d5df0a
--- a/libutil++/op_bfd.h
d5df0a
+++ b/libutil++/op_bfd.h
d5df0a
@@ -334,8 +334,8 @@ private:
d5df0a
         bfd_vma vma_adj;
d5df0a
 
d5df0a
         /**
d5df0a
-         * The file descriptor for an image file that we pass to fdopen_bfd must be kep
d5df0a
-         * open through the life of the op_bfd to enable proper beahvior of certain
d5df0a
+         * The file descriptor for an image file that we pass to fdopen_bfd must be kept
d5df0a
+         * open through the life of the op_bfd to enable proper behavior of certain
d5df0a
          * BFD functions -- in particular, bfd_find_nearest_line().
d5df0a
          */
d5df0a
         int fd;
d5df0a
diff --git a/libutil++/op_spu_bfd.cpp b/libutil++/op_spu_bfd.cpp
d5df0a
index 4ac5245..29d6e06 100644
d5df0a
--- a/libutil++/op_spu_bfd.cpp
d5df0a
+++ b/libutil++/op_spu_bfd.cpp
d5df0a
@@ -50,7 +50,7 @@ op_bfd::op_bfd(uint64_t spu_offset, string const & fname,
d5df0a
 	anon_obj(false),
d5df0a
 	vma_adj(0)
d5df0a
 {
d5df0a
-	int fd = -1;
d5df0a
+	fd = -1;
d5df0a
 	struct stat st;
d5df0a
 	int notes_remaining;
d5df0a
 	bool spu_note_found = false;
d5df0a
diff --git a/opjitconv/conversion.c b/opjitconv/conversion.c
d5df0a
index 111fe9d..add0f95 100644
d5df0a
--- a/opjitconv/conversion.c
d5df0a
+++ b/opjitconv/conversion.c
d5df0a
@@ -39,10 +39,10 @@ static void free_jit_debug_line(void)
d5df0a
 	jitentry_debug_line_list = NULL;
d5df0a
 }
d5df0a
 
d5df0a
-int op_jit_convert(struct op_jitdump_info file_info, char const * elffile,
d5df0a
+int op_jit_convert(struct op_jitdump_info * file_info, char const * elffile,
d5df0a
                    unsigned long long start_time, unsigned long long end_time)
d5df0a
 {
d5df0a
-	void const * jitdump = file_info.dmp_file;
d5df0a
+	void const * jitdump = file_info->dmp_file;
d5df0a
 	int rc= OP_JIT_CONV_OK;
d5df0a
 
d5df0a
 	entry_count = 0;
d5df0a
@@ -53,7 +53,7 @@ int op_jit_convert(struct op_jitdump_info file_info, char const * elffile,
d5df0a
 	jitentry_debug_line_list = NULL;
d5df0a
 	entries_symbols_ascending = entries_address_ascending = NULL;
d5df0a
 
d5df0a
-	if ((rc = parse_all(jitdump, jitdump + file_info.dmp_file_stat.st_size,
d5df0a
+	if ((rc = parse_all(jitdump, jitdump + file_info->dmp_file_stat.st_size,
d5df0a
 	                    end_time)) == OP_JIT_CONV_FAIL)
d5df0a
 		goto out;
d5df0a
 
d5df0a
diff --git a/opjitconv/opjitconv.c b/opjitconv/opjitconv.c
d5df0a
index a9dfa91..9d910be 100644
d5df0a
--- a/opjitconv/opjitconv.c
d5df0a
+++ b/opjitconv/opjitconv.c
d5df0a
@@ -19,6 +19,7 @@
d5df0a
 #include "op_file.h"
d5df0a
 #include "op_libiberty.h"
d5df0a
 
d5df0a
+#include <getopt.h>
d5df0a
 #include <dirent.h>
d5df0a
 #include <fnmatch.h>
d5df0a
 #include <errno.h>
d5df0a
@@ -75,6 +76,19 @@ int debug;
d5df0a
 int non_root;
d5df0a
 /* indicates we should delete jitdump files owned by the user */
d5df0a
 int delete_jitdumps;
d5df0a
+/* Session directory where sample data is stored */
d5df0a
+char * session_dir;
d5df0a
+
d5df0a
+static struct option long_options [] = {
d5df0a
+                                        { "session-dir", required_argument, NULL, 's'},
d5df0a
+                                        { "debug", no_argument, NULL, 'd'},
d5df0a
+                                        { "delete-jitdumps", no_argument, NULL, 'j'},
d5df0a
+                                        { "non-root", no_argument, NULL, 'n'},
d5df0a
+                                        { "help", no_argument, NULL, 'h'},
d5df0a
+                                        { NULL, 9, NULL, 0}
d5df0a
+};
d5df0a
+const char * short_options = "s:djnh";
d5df0a
+
d5df0a
 LIST_HEAD(jitdump_deletion_candidates);
d5df0a
 
d5df0a
 /*
d5df0a
@@ -407,7 +421,7 @@ chk_proc_id:
d5df0a
 			goto free_res3;
d5df0a
 		}
d5df0a
 		/* Convert the dump file as the special user 'oprofile'. */
d5df0a
-		rc = op_jit_convert(dmp_info, tmp_elffile, start_time, end_time);
d5df0a
+		rc = op_jit_convert(&dmp_info, tmp_elffile, start_time, end_time);
d5df0a
 		if (rc < 0)
d5df0a
 			goto free_res3;
d5df0a
 
d5df0a
@@ -772,61 +786,99 @@ static void _cleanup_jitdumps(void)
d5df0a
 
d5df0a
 }
d5df0a
 
d5df0a
-int main(int argc, char ** argv)
d5df0a
+static void __print_usage(const char * extra_msg)
d5df0a
+{
d5df0a
+	if (extra_msg)
d5df0a
+		fprintf(stderr, extra_msg);
d5df0a
+	fprintf(stderr, "usage: opjitconv [--debug | --non-root | --delete-jitdumps ] --session-dir=<dir> <starttime> <endtime>\n");
d5df0a
+}
d5df0a
+
d5df0a
+static int _process_args(int argc, char * const argv[])
d5df0a
+{
d5df0a
+	int keep_trying = 1;
d5df0a
+	int idx_of_non_options = 0;
d5df0a
+	setenv("POSIXLY_CORRECT", "1", 0);
d5df0a
+	while (keep_trying) {
d5df0a
+		int option_idx = 0;
d5df0a
+		int c = getopt_long(argc, argv, short_options, long_options, &option_idx);
d5df0a
+		switch (c) {
d5df0a
+		case -1:
d5df0a
+			if (optind != argc) {
d5df0a
+				idx_of_non_options = optind;
d5df0a
+			}
d5df0a
+			keep_trying = 0;
d5df0a
+			break;
d5df0a
+		case '?':
d5df0a
+			printf("non-option detected at optind %d\n", optind);
d5df0a
+			keep_trying = 0;
d5df0a
+			idx_of_non_options = -1;
d5df0a
+			break;
d5df0a
+		case 's':
d5df0a
+			session_dir = optarg;
d5df0a
+			break;
d5df0a
+		case 'd':
d5df0a
+			debug = 1;
d5df0a
+			break;
d5df0a
+		case 'n':
d5df0a
+			non_root = 1;
d5df0a
+			break;
d5df0a
+		case 'j':
d5df0a
+			delete_jitdumps = 1;
d5df0a
+			break;
d5df0a
+		case 'h':
d5df0a
+			break;
d5df0a
+		default:
d5df0a
+			break;
d5df0a
+		}
d5df0a
+	}
d5df0a
+	return idx_of_non_options;
d5df0a
+}
d5df0a
+
d5df0a
+int main(int argc, char * const argv[])
d5df0a
 {
d5df0a
 	unsigned long long start_time, end_time;
d5df0a
-	char session_dir[PATH_MAX];
d5df0a
-	int rc = 0;
d5df0a
+	struct stat filestat;
d5df0a
+	int non_options_idx, rc = 0;
d5df0a
 	size_t sessdir_len = 0;
d5df0a
-	char * path_end;
d5df0a
 
d5df0a
 	debug = 0;
d5df0a
-	if (argc > 1 && strcmp(argv[1], "-d") == 0) {
d5df0a
-		debug = 1;
d5df0a
-		argc--;
d5df0a
-		argv++;
d5df0a
-	}
d5df0a
 	non_root = 0;
d5df0a
-	if (argc > 1 && strcmp(argv[1], "--non-root") == 0) {
d5df0a
-		non_root = 1;
d5df0a
-		argc--;
d5df0a
-		argv++;
d5df0a
-	}
d5df0a
-
d5df0a
 	delete_jitdumps = 0;
d5df0a
-	if (argc > 1 && strcmp(argv[1], "--delete-jitdumps") == 0) {
d5df0a
-		delete_jitdumps = 1;
d5df0a
-		argc--;
d5df0a
-		argv++;
d5df0a
-	}
d5df0a
-
d5df0a
-	if (argc != 4) {
d5df0a
-		printf("Usage: opjitconv [-d] <session_dir> <starttime>"
d5df0a
-		       " <endtime>\n");
d5df0a
+	session_dir = NULL;
d5df0a
+	non_options_idx = _process_args(argc, argv);
d5df0a
+	// We need the session_dir and two non-option values passed -- starttime and endtime.
d5df0a
+	if (!session_dir || (non_options_idx != argc - 2)) {
d5df0a
+		__print_usage(NULL);
d5df0a
 		fflush(stdout);
d5df0a
 		rc = EXIT_FAILURE;
d5df0a
 		goto out;
d5df0a
 	}
d5df0a
 
d5df0a
 	/*
d5df0a
-	 * Check for a maximum of 4096 bytes (Linux path name length limit) decremented
d5df0a
-	 * by 16 bytes (will be used later for appending samples sub directory).
d5df0a
+	 * Check for a maximum of 4096 bytes (Linux path name length limit) minus 16 bytes
d5df0a
+	 * (to be used later for appending samples sub directory) minus 1 (for terminator).
d5df0a
 	 * Integer overflows according to the session dir parameter (user controlled)
d5df0a
 	 * are not possible anymore.
d5df0a
 	 */
d5df0a
-	path_end = memchr(argv[1], '\0', PATH_MAX);
d5df0a
-	if (!path_end || ((sessdir_len = (path_end - argv[1])) >= PATH_MAX - 16)) {
d5df0a
+	if ((sessdir_len = strlen(session_dir)) >= (PATH_MAX - 17)) {
d5df0a
 		printf("opjitconv: Path name length limit exceeded for session directory\n");
d5df0a
 		rc = EXIT_FAILURE;
d5df0a
 		goto out;
d5df0a
 	}
d5df0a
-	memset(session_dir, '\0', PATH_MAX);
d5df0a
-	assert(sessdir_len < (PATH_MAX - 16 - 1));
d5df0a
-	strncpy(session_dir, argv[1], sessdir_len);
d5df0a
-	session_dir[PATH_MAX -1] = '\0';
d5df0a
 
d5df0a
-	start_time = atol(argv[2]);
d5df0a
-	end_time = atol(argv[3]);
d5df0a
+	if (stat(session_dir, &filestat)) {
d5df0a
+		perror("stat operation on passed session-dir failed");
d5df0a
+		rc = EXIT_FAILURE;
d5df0a
+		goto out;
d5df0a
+	}
d5df0a
+	if (!S_ISDIR(filestat.st_mode)) {
d5df0a
+		printf("Passed session-dir %s is not a directory\n", session_dir);
d5df0a
+		rc = EXIT_FAILURE;
d5df0a
+		goto out;
d5df0a
+	}
d5df0a
+
d5df0a
+	start_time = atol(argv[non_options_idx++]);
d5df0a
+	end_time = atol(argv[non_options_idx]);
d5df0a
 
d5df0a
 	if (start_time > end_time) {
d5df0a
 		rc = EXIT_FAILURE;
d5df0a
diff --git a/opjitconv/opjitconv.h b/opjitconv/opjitconv.h
d5df0a
index f6243c9..a3ce37f 100644
d5df0a
--- a/opjitconv/opjitconv.h
d5df0a
+++ b/opjitconv/opjitconv.h
d5df0a
@@ -99,7 +99,7 @@ int parse_all(void const * start, void const * end,
d5df0a
 	      unsigned long long end_time);
d5df0a
 
d5df0a
 /* conversion.c */
d5df0a
-int op_jit_convert(struct op_jitdump_info file_info, char const * elffile,
d5df0a
+int op_jit_convert(struct op_jitdump_info *file_info, char const * elffile,
d5df0a
                    unsigned long long start_time, unsigned long long end_time);
d5df0a
 
d5df0a
 /* create_bfd.c */
d5df0a
diff --git a/pe_profiling/operf.cpp b/pe_profiling/operf.cpp
d5df0a
index 88aed3d..399308f 100644
d5df0a
--- a/pe_profiling/operf.cpp
d5df0a
+++ b/pe_profiling/operf.cpp
d5df0a
@@ -787,7 +787,7 @@ static void _do_jitdump_convert()
d5df0a
 	struct timeval tv;
d5df0a
 	char end_time_str[32];
d5df0a
 	char opjitconv_path[PATH_MAX + 1];
d5df0a
-	char * exec_args[8];
d5df0a
+	char * exec_args[9];
d5df0a
 
d5df0a
 	jitconv_pid = fork();
d5df0a
 	switch (jitconv_pid) {
d5df0a
@@ -799,6 +799,7 @@ static void _do_jitdump_convert()
d5df0a
 		const char * debug_option = "-d";
d5df0a
 		const char * non_root_user = "--non-root";
d5df0a
 		const char * delete_jitdumps = "--delete-jitdumps";
d5df0a
+		const char * sess_dir =  "--session-dir";
d5df0a
 		gettimeofday(&tv, NULL);
d5df0a
 		end_time = tv.tv_sec;
d5df0a
 		sprintf(end_time_str, "%llu", end_time);
d5df0a
@@ -810,6 +811,7 @@ static void _do_jitdump_convert()
d5df0a
 		if (my_uid != 0)
d5df0a
 			exec_args[arg_num++] = (char *)non_root_user;
d5df0a
 		exec_args[arg_num++] = (char *)delete_jitdumps;
d5df0a
+		exec_args[arg_num++] = (char *)sess_dir;
d5df0a
 		exec_args[arg_num++] = (char *)operf_options::session_dir.c_str();
d5df0a
 		exec_args[arg_num++] = start_time_str;
d5df0a
 		exec_args[arg_num++] = end_time_str;