Blame SOURCES/oprofile-coverity.patch

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