da2d47
commit 284f25c227d1b6c07e87f6336d3b6ff0533c85d7
da2d47
Author: William Cohen <wcohen@redhat.com>
da2d47
Date:   Thu Jan 30 16:20:24 2014 -0500
da2d47
da2d47
    Use correct specification for signed and unsigned int
da2d47
    
da2d47
    A run of cppcheck showed that some mismatches between the specfications
da2d47
    for sscanf and the variables being used to store the values.  This corrects
da2d47
    those minor issues.
da2d47
da2d47
diff --git a/src/components/lustre/linux-lustre.c b/src/components/lustre/linux-lustre.c
da2d47
index 4f4fb7a..46899f2 100644
da2d47
--- a/src/components/lustre/linux-lustre.c
da2d47
+++ b/src/components/lustre/linux-lustre.c
da2d47
@@ -334,13 +334,13 @@ read_lustre_counter( )
da2d47
 			if (fgets(buffer,BUFSIZ,fff)==NULL) break;
da2d47
 	
da2d47
 			if (strstr( buffer, "write_bytes" )) {
da2d47
-			  sscanf(buffer,"%*s %*d %*s %*s %*d %*d %lld",&fs->write_cntr->value);
da2d47
-			  SUBDBG("Read %lld write_bytes\n",fs->write_cntr->value);
da2d47
+			  sscanf(buffer,"%*s %*d %*s %*s %*d %*d %llu",&fs->write_cntr->value);
da2d47
+			  SUBDBG("Read %llu write_bytes\n",fs->write_cntr->value);
da2d47
 			}
da2d47
 	
da2d47
 			if (strstr( buffer, "read_bytes" )) {
da2d47
-			  sscanf(buffer,"%*s %*d %*s %*s %*d %*d %lld",&fs->read_cntr->value);
da2d47
-			  SUBDBG("Read %lld read_bytes\n",fs->read_cntr->value);
da2d47
+			  sscanf(buffer,"%*s %*d %*s %*s %*d %*d %llu",&fs->read_cntr->value);
da2d47
+			  SUBDBG("Read %llu read_bytes\n",fs->read_cntr->value);
da2d47
 			}
da2d47
 		  }
da2d47
 		  fclose(fff);
da2d47
@@ -352,8 +352,8 @@ read_lustre_counter( )
da2d47
 			if (fgets(buffer,BUFSIZ,fff)==NULL) break;
da2d47
 	
da2d47
 			if (strstr( buffer, "read but discarded")) {
da2d47
-			   sscanf(buffer,"%*s %*s %*s %lld",&fs->readahead_cntr->value);
da2d47
-			   SUBDBG("Read %lld discared\n",fs->readahead_cntr->value);
da2d47
+			   sscanf(buffer,"%*s %*s %*s %llu",&fs->readahead_cntr->value);
da2d47
+			   SUBDBG("Read %llu discared\n",fs->readahead_cntr->value);
da2d47
 			   break;
da2d47
 			}
da2d47
 	  	  }
da2d47
diff --git a/src/components/net/linux-net.c b/src/components/net/linux-net.c
da2d47
index ad15d84..ba7563c 100644
da2d47
--- a/src/components/net/linux-net.c
da2d47
+++ b/src/components/net/linux-net.c
da2d47
@@ -240,7 +240,7 @@ read_net_counters( long long *values )
da2d47
             SUBDBG("Interface <%s> not found\n", ifname);
da2d47
         } else {
da2d47
             nf = sscanf( data,
da2d47
-                "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
da2d47
+                "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld\n",
da2d47
                 &values[if_bidx + 0],  &values[if_bidx + 1],
da2d47
                 &values[if_bidx + 2],  &values[if_bidx + 3],
da2d47
                 &values[if_bidx + 4],  &values[if_bidx + 5],
da2d47
@@ -251,7 +251,7 @@ read_net_counters( long long *values )
da2d47
                 &values[if_bidx + 14], &values[if_bidx + 15]);
da2d47
 
da2d47
             SUBDBG("\nRead "
da2d47
-                "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
da2d47
+                "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld\n",
da2d47
                 values[if_bidx + 0],  values[if_bidx + 1],
da2d47
                 values[if_bidx + 2],  values[if_bidx + 3],
da2d47
                 values[if_bidx + 4],  values[if_bidx + 5],
da2d47
commit c810cd0d90baead96838145004545cc156b7ab77
da2d47
Author: James Ralph <ralph@icl.utk.edu>
da2d47
Date:   Tue Aug 13 14:13:55 2013 -0400
da2d47
da2d47
    Close resource leaks
da2d47
    
da2d47
    User dcb reported several resource leaks in trac bug #184.
da2d47
    --------------------
da2d47
    I just ran the static analysis checker "cppcheck" over the source
da2d47
    code of papi-5.2.0
da2d47
    
da2d47
    It said
da2d47
    
da2d47
    1. [linux-memory.c:711]: (error) Resource leak: sys_cpu
da2d47
    
da2d47
    2. [papi_preset.c:735]: (error) Resource leak: fp
da2d47
    
da2d47
    3. [components/micpower/linux-micpower.c:166]: (error) Resource leak: fp
da2d47
    
da2d47
    I've checked them all and they all look like resource leaks to me.
da2d47
    
da2d47
    Suggest code rework.
da2d47
    ----------------------------------
da2d47
da2d47
diff --git a/src/components/micpower/linux-micpower.c b/src/components/micpower/linux-micpower.c
da2d47
index 896e75f..4da4577 100644
da2d47
--- a/src/components/micpower/linux-micpower.c
da2d47
+++ b/src/components/micpower/linux-micpower.c
da2d47
@@ -163,6 +163,7 @@ read_sysfs_file( long long* counts)
da2d47
 				retval&= fscanf(fp, "%lld %lld %lld", &counts[i], &counts[i+1], &counts[i+2] );
da2d47
 		}
da2d47
 
da2d47
+		fclose(fp);
da2d47
 		return retval;
da2d47
 }
da2d47
 
da2d47
diff --git a/src/linux-memory.c b/src/linux-memory.c
da2d47
index 6c69fbd..bf6c420 100644
da2d47
--- a/src/linux-memory.c
da2d47
+++ b/src/linux-memory.c
da2d47
@@ -707,6 +707,7 @@ sparc_sysfs_cpu_attr( char *name, char **result )
da2d47
 			return 0;
da2d47
 		}
da2d47
 	}
da2d47
+	closedir( sys_cpu );
da2d47
 	return -1;
da2d47
 }
da2d47
 
da2d47
diff --git a/src/papi_preset.c b/src/papi_preset.c
da2d47
index 9485793..603c8df 100644
da2d47
--- a/src/papi_preset.c
da2d47
+++ b/src/papi_preset.c
da2d47
@@ -732,12 +732,14 @@ _xml_papi_hwi_setup_all_presets( char *arch, hwi_dev_notes_t * notes )
da2d47
 
da2d47
 	if ( !p ) {
da2d47
 		PAPIERROR( "Couldn't allocate memory for XML parser." );
da2d47
+		fclose(fp);
da2d47
 		return ( PAPI_ESYS );
da2d47
 	}
da2d47
 	XML_SetElementHandler( p, _xml_start, _xml_end );
da2d47
 	XML_SetCharacterDataHandler( p, _xml_content );
da2d47
 	if ( fp == NULL ) {
da2d47
 		PAPIERROR( "Error opening Preset XML file." );
da2d47
+		fclose(fp);
da2d47
 		return ( PAPI_ESYS );
da2d47
 	}
da2d47
 
da2d47
@@ -749,11 +751,13 @@ _xml_papi_hwi_setup_all_presets( char *arch, hwi_dev_notes_t * notes )
da2d47
 
da2d47
 		if ( buffer == NULL ) {
da2d47
 			PAPIERROR( "Couldn't allocate memory for XML buffer." );
da2d47
+			fclose(fp);
da2d47
 			return ( PAPI_ESYS );
da2d47
 		}
da2d47
 		len = fread( buffer, 1, BUFFSIZE, fp );
da2d47
 		if ( ferror( fp ) ) {
da2d47
 			PAPIERROR( "XML read error." );
da2d47
+			fclose(fp);
da2d47
 			return ( PAPI_ESYS );
da2d47
 		}
da2d47
 		done = feof( fp );
da2d47
@@ -761,10 +765,13 @@ _xml_papi_hwi_setup_all_presets( char *arch, hwi_dev_notes_t * notes )
da2d47
 			PAPIERROR( "Parse error at line %d:\n%s\n",
da2d47
 					   XML_GetCurrentLineNumber( p ),
da2d47
 					   XML_ErrorString( XML_GetErrorCode( p ) ) );
da2d47
+			fclose(fp);
da2d47
 			return ( PAPI_ESYS );
da2d47
 		}
da2d47
-		if ( error )
da2d47
+		if ( error ) {
da2d47
+			fclose(fp);
da2d47
 			return ( PAPI_ESYS );
da2d47
+		}
da2d47
 	} while ( !done );
da2d47
 	XML_ParserFree( p );
da2d47
 	fclose( fp );
da2d47
commit e5b335740fad31cd230295508f2a4e9fb77a2878
da2d47
Author: James Ralph <ralph@icl.utk.edu>
da2d47
Date:   Fri Nov 8 15:15:28 2013 -0500
da2d47
da2d47
    multiplex_cost: check return value on PAPI_set_opt
da2d47
    
da2d47
    Thanks to Will Cohen for reporting based upon output of coverity.
da2d47
da2d47
diff --git a/src/utils/multiplex_cost.c b/src/utils/multiplex_cost.c
da2d47
index 8fbd7a4..2f8216a 100644
da2d47
--- a/src/utils/multiplex_cost.c
da2d47
+++ b/src/utils/multiplex_cost.c
da2d47
@@ -112,7 +112,8 @@ init_test(int SoftwareMPX, int KernelMPX, int* Events)
da2d47
   option.multiplex.eventset = SoftwareMPX;
da2d47
   option.multiplex.ns = itimer.itimer.ns;
da2d47
 
da2d47
-  PAPI_set_opt( PAPI_MULTIPLEX, &option );
da2d47
+  if ( (retval = PAPI_set_opt( PAPI_MULTIPLEX, &option )) != PAPI_OK )
da2d47
+      test_fail( __FILE__, __LINE__, "PAPI_set_opt", retval);
da2d47
 
da2d47
   for (i = 0; i < options.min - 1; i++) {
da2d47
 	if ( options.kernel_mpx ) {
da2d47
@@ -249,7 +250,8 @@ main( int argc, char **argv )
da2d47
   option.multiplex.eventset = SoftwareMPX;
da2d47
   option.multiplex.ns = itimer.itimer.ns;
da2d47
 
da2d47
-  PAPI_set_opt( PAPI_MULTIPLEX, &option );
da2d47
+  if ( PAPI_OK != (retval = PAPI_set_opt( PAPI_MULTIPLEX, &option )))
da2d47
+      test_fail( __FILE__, __LINE__, "PAPI_set_opt", retval);
da2d47
 
da2d47
   if ( !options.kernel_mpx && !options.force_sw ) {
da2d47
 	test_fail(__FILE__, __LINE__, "No tests to run.", -1);
da2d47
commit 83c31e25409040aac8178a4a3f89111efd060cc0
da2d47
Author: James Ralph <ralph@icl.utk.edu>
da2d47
Date:   Fri Nov 8 16:10:18 2013 -0500
da2d47
da2d47
    perf_event.c: Check return value of ioctl
da2d47
    
da2d47
    Thanks to Will Cohen for reporting based upon output of coverity.
da2d47
da2d47
diff --git a/src/components/perf_event/perf_event.c b/src/components/perf_event/perf_event.c
da2d47
index 4b52cce..b4b2656 100644
da2d47
--- a/src/components/perf_event/perf_event.c
da2d47
+++ b/src/components/perf_event/perf_event.c
da2d47
@@ -1909,7 +1909,9 @@ _pe_dispatch_timer( int n, hwd_siginfo_t *info, void *uc)
da2d47
     return;
da2d47
   }
da2d47
         
da2d47
-  ioctl( fd, PERF_EVENT_IOC_DISABLE, NULL );
da2d47
+  if (ioctl( fd, PERF_EVENT_IOC_DISABLE, NULL ) == -1 ) {
da2d47
+      PAPIERROR("ioctl(PERF_EVENT_IOC_DISABLE) failed.\n");
da2d47
+  }
da2d47
 
da2d47
   if ( ( thread->running_eventset[cidx]->state & PAPI_PROFILING ) && 
da2d47
        !( thread->running_eventset[cidx]->profile.flags & 
da2d47
commit 60fb1dd4497df7d0ea77d88f586142ceb3e22b32
da2d47
Author: James Ralph <ralph@icl.utk.edu>
da2d47
Date:   Thu Nov 21 13:18:49 2013 -0500
da2d47
da2d47
    command_line utility: Initialize a variable
da2d47
    
da2d47
    Initialize data_type to PAPI_DATATYPE_INT64
da2d47
    Addresses a coverity error
da2d47
    Error: COMPILER_WARNING: [#def19]
da2d47
    papi-5.2.0/src/utils/command_line.c:133:4: warning: 'data_type' may be used uninitialized in this function [-Wmaybe-uninitialized]
da2d47
    switch (data_type) {
da2d47
            ^
da2d47
da2d47
diff --git a/src/utils/command_line.c b/src/utils/command_line.c
da2d47
index 36f7df5..2f4a816 100644
da2d47
--- a/src/utils/command_line.c
da2d47
+++ b/src/utils/command_line.c
da2d47
@@ -56,7 +56,7 @@ main( int argc, char **argv )
da2d47
 	char *success;
da2d47
 	PAPI_event_info_t info;
da2d47
 	int EventSet = PAPI_NULL;
da2d47
-	int i, j, data_type, event;
da2d47
+	int i, j, event, data_type = PAPI_DATATYPE_INT64;
da2d47
 	int u_format = 0;
da2d47
 	int hex_format = 0;
da2d47
 
da2d47
commit e43b1138296866795c5db1a6dcd123d312af1b46
da2d47
Author: James Ralph <ralph@icl.utk.edu>
da2d47
Date:   Wed Jul 23 15:40:47 2014 -0400
da2d47
da2d47
    native_avail.c: Bug fixes and updates
da2d47
    
da2d47
    Thanks to Gary Mohr
da2d47
    --------------------------------------------------
da2d47
    This patch fixes a couple of problems found in the papi_native_avail program.
da2d47
    
da2d47
    First change fixes a problem introduced when the -validate option was added.  This
da2d47
    option causes events to get added to an event set but never removes them.  This change
da2d47
    will remove them if the add works.  This change also fixes a coverity detected error
da2d47
    where the return value from PAPI_destroy_eventset was being ignored.
da2d47
    
da2d47
    Second change improves the delimitor check when separating the event description from
da2d47
    the event mask description.  The previous check only looked for a colon but some of the
da2d47
    event descriptions contain a colon so descriptions would get displayed incorrectly.  The
da2d47
    new check finds the "masks:" substring which is what papi inserts to separate these two
da2d47
    descriptions.
da2d47
    
da2d47
    Third change adds code to allow the user to enter events of the form pmu:::event or
da2d47
    pmu::event when using the -e option in the program.
da2d47
da2d47
diff --git a/src/utils/native_avail.c b/src/utils/native_avail.c
da2d47
index 2073ed7..59fc1b4 100644
da2d47
--- a/src/utils/native_avail.c
da2d47
+++ b/src/utils/native_avail.c
da2d47
@@ -227,13 +232,19 @@ parse_unit_masks( PAPI_event_info_t * info )
da2d47
 	if ( ( pmask = strchr( ptr, ':' ) ) == NULL ) {
da2d47
 		return ( 0 );
da2d47
 	}
da2d47
-	memmove( info->symbol, pmask, ( strlen( pmask ) + 1 ) * sizeof ( char ) );
da2d47
-	pmask = strchr( info->long_descr, ':' );
da2d47
-	if ( pmask == NULL )
da2d47
+	memmove( info->symbol, pmask, ( strlen(pmask) + 1 ) * sizeof(char) );
da2d47
+
da2d47
+	//  The description field contains the event description followed by a tag 'masks:'
da2d47
+	//  and then the mask description (if there was a mask with this event).  The following
da2d47
+	//  code isolates the mask description part of this information.
da2d47
+
da2d47
+	pmask = strstr( info->long_descr, "masks:" );
da2d47
+	if ( pmask == NULL ) {
da2d47
 		info->long_descr[0] = 0;
da2d47
-	else
da2d47
-		memmove( info->long_descr, pmask + sizeof ( char ),
da2d47
-				 ( strlen( pmask ) + 1 ) * sizeof ( char ) );
da2d47
+	} else {
da2d47
+		pmask += 6;        // bump pointer past 'masks:' identifier in description
da2d47
+		memmove( info->long_descr, pmask, (strlen(pmask) + 1) * sizeof(char) );
da2d47
+	}
da2d47
 	return ( 1 );
da2d47
 }
da2d47
 
da2d47
@@ -295,8 +306,20 @@ main( int argc, char **argv )
da2d47
 			 "Event name:", info.symbol);
da2d47
 		 printf( "%-29s|%s|\n", "Description:", info.long_descr );
da2d47
 
da2d47
+		  /* handle the PAPI component-style events which have a component:::event type */
da2d47
+		  char *ptr;
da2d47
+		  if ((ptr=strstr(flags.name, ":::"))) {
da2d47
+		    ptr+=3;
da2d47
+		  /* handle libpfm4-style events which have a pmu::event type event name */
da2d47
+		  } else if ((ptr=strstr(flags.name, "::"))) {
da2d47
+		    ptr+=2;
da2d47
+		  }
da2d47
+		  else {
da2d47
+		    ptr=flags.name;
da2d47
+		  }
da2d47
+
da2d47
 		     /* if unit masks exist but none specified, process all */
da2d47
-		     if ( !strchr( flags.name, ':' ) ) {
da2d47
+		     if ( !strchr( ptr, ':' ) ) {
da2d47
 			if ( PAPI_enum_event( &i, PAPI_NTV_ENUM_UMASKS ) == PAPI_OK ) {
da2d47
 			   printf( "\nUnit Masks:\n" );
da2d47
 			   do {
da2d47
commit 74041b3ebcfc69575efb4ff830b9dc2f651b458b
da2d47
Author: James Ralph <ralph@icl.utk.edu>
da2d47
Date:   Fri Aug 29 14:44:08 2014 -0400
da2d47
da2d47
    event_info utility: address coverity defect
da2d47
    
da2d47
    From Gary Mohr
da2d47
    --------------
da2d47
    This patch corrects a defect reported by Coverity.  The defect reported
da2d47
    that the call to PAPI_enum_cmp_event was setting retval which was never
da2d47
    getting used before it got set again by a call to PAPI_get_event_info.
da2d47
    
da2d47
    After looking at the code, I decided that we should not be trying to get
da2d47
    the next event inside a loop that is enumerating masks for the current
da2d47
    event.  It makes more sense to break out of the loop to get masks and
da2d47
    let the outer loop that is walking the events get the next event.
da2d47
    --------------
da2d47
da2d47
diff --git a/src/utils/event_info.c b/src/utils/event_info.c
da2d47
index d1010b6..1de375f 100644
da2d47
--- a/src/utils/event_info.c
da2d47
+++ b/src/utils/event_info.c
da2d47
@@ -237,8 +237,7 @@ enum_native_events( FILE * f, int cidx)
da2d47
 		    retval = PAPI_get_event_info( k, &info );
da2d47
 		    if ( retval == PAPI_OK ) {
da2d47
 		       if ( test_event( k )!=PAPI_OK ) {
da2d47
-			 retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
da2d47
-			  continue;
da2d47
+			   break;
da2d47
 		       }
da2d47
 		       xmlize_event( f, &info, -1 );
da2d47
 		    }
da2d47
commit 07990f85c706221f41d8b27bb2aebfc6c4874dbd
da2d47
Author: James Ralph <ralph@icl.utk.edu>
da2d47
Date:   Tue Sep 2 11:54:07 2014 -0400
da2d47
da2d47
    ctests/ Address coverity reported defects
da2d47
    
da2d47
    Thanks to Gary Mohr for the patch
da2d47
    ---------------------------------
da2d47
    he contents of this patch file fix defects reported by Coverity in the
da2d47
    directory 'papi/src/ctests'.
da2d47
    
da2d47
    The defect reported in branches.c was that a comparison between
da2d47
    different kinds of data was being done.
da2d47
    
da2d47
    The defect reported in calibrate.c was that the variable
da2d47
    'papi_event_str' could end up without a null terminator.
da2d47
    
da2d47
    The defects reported in describe.c, get_event_component.c, and
da2d47
    krentel_pthreads.c were that return values from function calls were
da2d47
    being stored in a variable but never being used.
da2d47
    
da2d47
    I also did a little clean-up in describe.c.  This test had been failing
da2d47
    for me on Intel NHM and SNBEP but now it runs and reports that it
da2d47
    PASSED.
da2d47
    ---------------------------------
da2d47
da2d47
diff --git a/src/ctests/branches.c b/src/ctests/branches.c
da2d47
index 930329e..5292323 100644
da2d47
--- a/src/ctests/branches.c
da2d47
+++ b/src/ctests/branches.c
da2d47
@@ -94,7 +94,7 @@ main( int argc, char **argv )
da2d47
 	/* Find a reasonable number of iterations (each 
da2d47
 	 * event active 20 times) during the measurement
da2d47
 	 */
da2d47
-	t2 = 10000 * 20 * nevents;	/* Target: 10000 usec/multiplex, 20 repeats */
da2d47
+	t2 = (long long)(10000 * 20) * nevents;	/* Target: 10000 usec/multiplex, 20 repeats */
da2d47
 	if ( t2 > 30e6 )
da2d47
 		test_skip( __FILE__, __LINE__, "This test takes too much time",
da2d47
 				   retval );
da2d47
diff --git a/src/ctests/calibrate.c b/src/ctests/calibrate.c
da2d47
index a3dea0a..e370ba9 100644
da2d47
--- a/src/ctests/calibrate.c
da2d47
+++ b/src/ctests/calibrate.c
da2d47
@@ -160,7 +160,8 @@ main( int argc, char *argv[] )
da2d47
 				print_help( argv );
da2d47
 				exit( 1 );
da2d47
 			}
da2d47
-			strncpy( papi_event_str, argv[i + 1], sizeof ( papi_event_str ) );
da2d47
+			strncpy( papi_event_str, argv[i + 1], sizeof ( papi_event_str ) - 1);
da2d47
+			papi_event_str[sizeof ( papi_event_str )-1] = '\0';
da2d47
 			i++;
da2d47
 		} else if ( strstr( argv[i], "-d" ) )
da2d47
 			double_precision = 1;
da2d47
diff --git a/src/ctests/describe.c b/src/ctests/describe.c
da2d47
index d29bf72..f03309e 100644
da2d47
--- a/src/ctests/describe.c
da2d47
+++ b/src/ctests/describe.c
da2d47
@@ -25,7 +25,6 @@ main( int argc, char **argv )
da2d47
 	int retval;
da2d47
 	long long g1[2];
da2d47
 	int eventcode = PAPI_TOT_INS;
da2d47
-	char eventname[PAPI_MAX_STR_LEN];
da2d47
 	PAPI_event_info_t info, info1, info2;
da2d47
 
da2d47
 	tests_quiet( argc, argv );	/* Set TESTS_QUIET variable */
da2d47
@@ -52,18 +51,19 @@ main( int argc, char **argv )
da2d47
 		test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
da2d47
 
da2d47
 	/* Case 0, no info, should fail */
da2d47
-	eventname[0] = '\0';
da2d47
 	eventcode = 0;
da2d47
 /*
da2d47
    if ( ( retval = PAPI_describe_event(eventname,(int *)&eventcode,eventdesc) ) == PAPI_OK)
da2d47
      test_fail(__FILE__,__LINE__,"PAPI_describe_event",retval);	   
da2d47
 */
da2d47
+	if (!TESTS_QUIET) {
da2d47
+	    printf("This test expects a 'PAPI Error' to be returned from this PAPI call.\n");
da2d47
+	}
da2d47
 	if ( ( retval = PAPI_get_event_info( eventcode, &info ) ) == PAPI_OK )
da2d47
 		test_fail( __FILE__, __LINE__, "PAPI_get_event_info", retval );
da2d47
 
da2d47
 	/* Case 1, fill in name field. */
da2d47
 	eventcode = PAPI_TOT_INS;
da2d47
-	eventname[0] = '\0';
da2d47
 /*
da2d47
    if ( ( retval = PAPI_describe_event(eventname,(int *)&eventcode,eventdesc) ) != PAPI_OK)
da2d47
      test_fail(__FILE__,__LINE__,"PAPI_describe_event",retval);	   
da2d47
@@ -85,11 +85,9 @@ main( int argc, char **argv )
da2d47
    if ( ( retval = PAPI_describe_event(eventname,(int *)&eventcode,eventdesc) ) != PAPI_OK)
da2d47
      test_fail(__FILE__,__LINE__,"PAPI_describe_event",retval);	   
da2d47
 */
da2d47
-	strcpy( eventname, info1.symbol );
da2d47
-	if ( ( retval =
da2d47
-		   PAPI_event_name_to_code( eventname,
da2d47
-									( int * ) &eventcode ) ) != PAPI_OK )
da2d47
+	if ( ( retval = PAPI_event_name_to_code( info1.symbol, ( int * ) &eventcode ) ) != PAPI_OK ) {
da2d47
 		test_fail( __FILE__, __LINE__, "PAPI_event_name_to_code", retval );
da2d47
+	}
da2d47
 
da2d47
 	if ( eventcode != PAPI_TOT_INS )
da2d47
 		test_fail( __FILE__, __LINE__,
da2d47
diff --git a/src/ctests/get_event_component.c b/src/ctests/get_event_component.c
da2d47
index ae1bdd9..874f394 100644
da2d47
--- a/src/ctests/get_event_component.c
da2d47
+++ b/src/ctests/get_event_component.c
da2d47
@@ -42,8 +42,7 @@ main( int argc, char **argv )
da2d47
             test_fail( __FILE__, __LINE__, "PAPI_get_component_info", 2 );
da2d47
          }
da2d47
 
da2d47
-         if (cmpinfo->disabled)
da2d47
-         {
da2d47
+         if (cmpinfo->disabled && !TESTS_QUIET) {
da2d47
            printf( "Name:   %-23s %s\n", cmpinfo->name ,cmpinfo->description);
da2d47
            printf("   \\-> Disabled: %s\n",cmpinfo->disabled_reason);
da2d47
            continue;
da2d47
@@ -55,7 +54,12 @@ main( int argc, char **argv )
da2d47
        if (retval!=PAPI_OK) continue;
da2d47
 
da2d47
        do {
da2d47
-          retval = PAPI_get_event_info( i, &info );
da2d47
+	   if (PAPI_get_event_info( i, &info ) != PAPI_OK) {
da2d47
+	       if (!TESTS_QUIET) {
da2d47
+		   printf("Getting information about event: %#x failed\n", i);
da2d47
+	       }
da2d47
+	       continue;
da2d47
+	   }
da2d47
 	  our_cid=PAPI_get_event_component(i);
da2d47
 
da2d47
 	  if (our_cid!=cid) {
da2d47
diff --git a/src/ctests/krentel_pthreads.c b/src/ctests/krentel_pthreads.c
da2d47
index a8b97ff..2417976 100644
da2d47
--- a/src/ctests/krentel_pthreads.c
da2d47
+++ b/src/ctests/krentel_pthreads.c
da2d47
@@ -125,11 +125,18 @@ my_thread( void *v )
da2d47
 	}
da2d47
 
da2d47
 	PAPI_stop( EventSet, &value );
da2d47
-	PAPI_remove_event( EventSet, EVENT );
da2d47
-	PAPI_destroy_eventset( &EventSet );
da2d47
+	retval = PAPI_remove_event( EventSet, EVENT );
da2d47
+	if ( PAPI_OK != retval ) {
da2d47
+	    test_fail( __FILE__, __LINE__, "PAPI_remove_event", retval );
da2d47
+	}
da2d47
+	retval = PAPI_destroy_eventset( &EventSet );
da2d47
+	if ( PAPI_OK != retval ) {
da2d47
+	    test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
da2d47
+	}
da2d47
 	retval = PAPI_unregister_thread(  );
da2d47
-	if ( retval != PAPI_OK )
da2d47
+	if ( PAPI_OK != retval ) {
da2d47
 		test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
da2d47
+	}
da2d47
 	return ( NULL );
da2d47
 }
da2d47
 
da2d47
commit 266c61a4d4e5beee43cce7e3ef1d64da202c0b09
da2d47
Author: James Ralph <ralph@icl.utk.edu>
da2d47
Date:   Fri Sep 19 17:46:42 2014 -0400
da2d47
da2d47
    Address coverity reported issues in src/
da2d47
    
da2d47
    Thanks to Gary Mohr
da2d47
    -------------------
da2d47
    Changes in this patch file:
da2d47
    
da2d47
    linux-common.c:  Add code to insure that cpu info vendor_string and
da2d47
        model_string buffers are NULL terminated strings.  Also insure that
da2d47
        the value which gets read into mdi->exe_info.fullname gets NULL
da2d47
        terminated. This makes it safe to use the  'strxxx' functions on the
da2d47
        value (which is done immediately after it is read in).
da2d47
    
da2d47
    papi_hl.c:  Fix call to _hl_rate_calls() where the third argument was
da2d47
        not the correct data type.
da2d47
    
da2d47
    papi_internal.c:  Add code to insure that event info name, short_desc,
da2d47
        and long_desc buffers are NULL terminated strings.
da2d47
    
da2d47
    papi_user_events.c:  While processing define symbols, insure that the
da2d47
        'local_line', 'name', and 'value'
da2d47
        buffers get NULL terminated (so we can safely use
da2d47
    	    'strxxx' functions on them).
da2d47
        Insure that the 'symbol' field in the user defined
da2d47
        event ends up NULL terminated.
da2d47
        Rearrange code to avoid falling through from one
da2d47
        case to the next in a switch statement.
da2d47
        Coverity flagged falling out the bottom of a case
da2d47
        statement as a potential defect but it
da2d47
        was doing what it should.
da2d47
    
da2d47
    sw_multiplex.c:  Unnecessary test.  The value of ESI can not be NULL
da2d47
        when this code is reached.
da2d47
    
da2d47
    x86_cpuid_info.c:  The variable need_leaf4 is set but not used.  The
da2d47
        only place it gets set returns without
da2d47
        checking its value.  The place that checks its value
da2d47
        never could have set its value non-zero.
da2d47
da2d47
diff --git a/src/linux-common.c b/src/linux-common.c
da2d47
index 4adc232..614a83c 100644
da2d47
--- a/src/linux-common.c
da2d47
+++ b/src/linux-common.c
da2d47
@@ -171,6 +171,7 @@ int
da2d47
 _linux_get_cpu_info( PAPI_hw_info_t *hwinfo, int *cpuinfo_mhz )
da2d47
 {
da2d47
     int tmp, retval = PAPI_OK;
da2d47
+    unsigned int strSize;
da2d47
     char maxargs[PAPI_HUGE_STR_LEN], *t, *s;
da2d47
     float mhz = 0.0;
da2d47
     FILE *f;
da2d47
@@ -197,14 +198,17 @@ _linux_get_cpu_info( PAPI_hw_info_t *hwinfo, int *cpuinfo_mhz )
da2d47
        /* Vendor Name and Vendor Code */
da2d47
     rewind( f );
da2d47
     s = search_cpu_info( f, "vendor_id", maxargs );
da2d47
+    strSize = sizeof(hwinfo->vendor_string);
da2d47
     if ( s && ( t = strchr( s + 2, '\n' ) ) ) {
da2d47
        *t = '\0';
da2d47
+       if (strlen(s+2) >= strSize-1)     s[strSize+1] = '\0';
da2d47
        strcpy( hwinfo->vendor_string, s + 2 );
da2d47
     } else {
da2d47
        rewind( f );
da2d47
        s = search_cpu_info( f, "vendor", maxargs );
da2d47
        if ( s && ( t = strchr( s + 2, '\n' ) ) ) {
da2d47
 	  *t = '\0';
da2d47
+     if (strlen(s+2) >= strSize-1)     s[strSize+1] = '\0';
da2d47
 	  strcpy( hwinfo->vendor_string, s + 2 );
da2d47
        } else {
da2d47
 	  rewind( f );
da2d47
@@ -212,6 +216,7 @@ _linux_get_cpu_info( PAPI_hw_info_t *hwinfo, int *cpuinfo_mhz )
da2d47
 	  if ( s && ( t = strchr( s + 2, '\n' ) ) ) {
da2d47
 	     *t = '\0';
da2d47
 	     s = strtok( s + 2, " " );
da2d47
+        if (strlen(s) >= strSize-1)     s[strSize-1] = '\0';
da2d47
 	     strcpy( hwinfo->vendor_string, s );
da2d47
 	  } else {
da2d47
 	     rewind( f );
da2d47
@@ -258,14 +263,17 @@ _linux_get_cpu_info( PAPI_hw_info_t *hwinfo, int *cpuinfo_mhz )
da2d47
        /* Model Name */
da2d47
     rewind( f );
da2d47
     s = search_cpu_info( f, "model name", maxargs );
da2d47
+    strSize = sizeof(hwinfo->model_string);
da2d47
     if ( s && ( t = strchr( s + 2, '\n' ) ) ) {
da2d47
        *t = '\0';
da2d47
+       if (strlen(s+2) >= strSize-1)     s[strSize+1] = '\0';
da2d47
        strcpy( hwinfo->model_string, s + 2 );
da2d47
     } else {
da2d47
        rewind( f );
da2d47
        s = search_cpu_info( f, "family", maxargs );
da2d47
        if ( s && ( t = strchr( s + 2, '\n' ) ) ) {
da2d47
 	  *t = '\0';
da2d47
+     if (strlen(s+2) >= strSize-1)     s[strSize+1] = '\0';
da2d47
 	  strcpy( hwinfo->model_string, s + 2 );
da2d47
        } else {
da2d47
 	  rewind( f );
da2d47
@@ -274,6 +282,7 @@ _linux_get_cpu_info( PAPI_hw_info_t *hwinfo, int *cpuinfo_mhz )
da2d47
 	     *t = '\0';
da2d47
 	     strtok( s + 2, " " );
da2d47
 	     s = strtok( NULL, " " );
da2d47
+        if (strlen(s) >= strSize-1)     s[strSize-1] = '\0';
da2d47
 	     strcpy( hwinfo->model_string, s );
da2d47
 	  } else {
da2d47
 	     rewind( f );
da2d47
@@ -282,6 +291,7 @@ _linux_get_cpu_info( PAPI_hw_info_t *hwinfo, int *cpuinfo_mhz )
da2d47
 		*t = '\0';
da2d47
 		/* get just the first token */
da2d47
 		s = strtok( s + 2, " " );
da2d47
+	if (strlen(s) >= strSize-1)     s[strSize-1] = '\0';
da2d47
 		strcpy( hwinfo->model_string, s );
da2d47
 	     }
da2d47
 	  }
da2d47
@@ -444,15 +454,18 @@ _linux_get_system_info( papi_mdi_t *mdi ) {
da2d47
 	mdi->pid = pid;
da2d47
 
da2d47
 	sprintf( maxargs, "/proc/%d/exe", ( int ) pid );
da2d47
-	if ( readlink( maxargs, mdi->exe_info.fullname, PAPI_HUGE_STR_LEN ) < 0 ) {
da2d47
+   if ( (retval = readlink( maxargs, mdi->exe_info.fullname, PAPI_HUGE_STR_LEN-1 )) < 0 ) {
da2d47
 		PAPIERROR( "readlink(%s) returned < 0", maxargs );
da2d47
 		return PAPI_ESYS;
da2d47
 	}
da2d47
+   if (retval > PAPI_HUGE_STR_LEN-1)   retval=PAPI_HUGE_STR_LEN-1;
da2d47
+   mdi->exe_info.fullname[retval] = '\0';
da2d47
 
da2d47
 	/* Careful, basename can modify it's argument */
da2d47
 
da2d47
 	strcpy( maxargs, mdi->exe_info.fullname );
da2d47
-	strcpy( mdi->exe_info.address_info.name, basename( maxargs ) );
da2d47
+   strncpy( mdi->exe_info.address_info.name, basename( maxargs ), PAPI_HUGE_STR_LEN-1);
da2d47
+   mdi->exe_info.address_info.name[PAPI_HUGE_STR_LEN-1] = '\0';
da2d47
 
da2d47
 	SUBDBG( "Executable is %s\n", mdi->exe_info.address_info.name );
da2d47
 	SUBDBG( "Full Executable is %s\n", mdi->exe_info.fullname );
da2d47
diff --git a/src/papi_hl.c b/src/papi_hl.c
da2d47
index 19111e7..4fcfe23 100644
da2d47
--- a/src/papi_hl.c
da2d47
+++ b/src/papi_hl.c
da2d47
@@ -204,13 +204,13 @@ int
da2d47
 PAPI_flips( float *rtime, float *ptime, long long *flpins, float *mflips )
da2d47
 {
da2d47
 	int retval;
da2d47
-	int events = PAPI_FP_INS;
da2d47
+   int events[1] = {PAPI_FP_INS};
da2d47
 	long long values = 0;
da2d47
 
da2d47
 	if ( rtime == NULL || ptime == NULL || flpins == NULL || mflips == NULL )
da2d47
 		return PAPI_EINVAL;
da2d47
 
da2d47
-	retval = _hl_rate_calls( rtime, ptime, &events, &values, flpins, mflips, HL_FLIP );
da2d47
+   retval = _hl_rate_calls( rtime, ptime, events, &values, flpins, mflips, HL_FLIP );
da2d47
 	return ( retval );
da2d47
 }
da2d47
 
da2d47
@@ -259,13 +259,13 @@ int
da2d47
 PAPI_flops( float *rtime, float *ptime, long long *flpops, float *mflops )
da2d47
 {
da2d47
 	int retval;
da2d47
-	int events = PAPI_FP_OPS;
da2d47
+   int events[1] = {PAPI_FP_OPS};
da2d47
 	long long values = 0;
da2d47
 
da2d47
 	if ( rtime == NULL || ptime == NULL || flpops == NULL || mflops == NULL )
da2d47
 		return PAPI_EINVAL;
da2d47
 
da2d47
-	retval = _hl_rate_calls( rtime, ptime, &events, &values, flpops, mflops, HL_FLOP );
da2d47
+   retval = _hl_rate_calls( rtime, ptime, events, &values, flpops, mflops, HL_FLOP );
da2d47
 	return ( retval );
da2d47
 }
da2d47
 
da2d47
diff --git a/src/papi_internal.c b/src/papi_internal.c
da2d47
index d354b76..3c51717 100644
da2d47
--- a/src/papi_internal.c
da2d47
+++ b/src/papi_internal.c
da2d47
@@ -2162,20 +2162,22 @@ _papi_hwi_get_preset_event_info( int EventCode, PAPI_event_info_t * info )
da2d47
 	unsigned int j;
da2d47
 
da2d47
 	if ( _papi_hwi_presets[i].symbol ) {	/* if the event is in the preset table */
da2d47
-	   /* set whole structure to 0 */
da2d47
+      // since we are setting the whole structure to zero the strncpy calls below will 
da2d47
+      // be leaving NULL terminates strings as long as they copy 1 less byte than the 
da2d47
+      // buffer size of the field.
da2d47
 	   memset( info, 0, sizeof ( PAPI_event_info_t ) );
da2d47
 
da2d47
 	   info->event_code = ( unsigned int ) EventCode;
da2d47
 	   strncpy( info->symbol, _papi_hwi_presets[i].symbol,
da2d47
-		    sizeof(info->symbol));
da2d47
+	    sizeof(info->symbol)-1);
da2d47
 
da2d47
 	   if ( _papi_hwi_presets[i].short_descr != NULL )
da2d47
 	      strncpy( info->short_descr, _papi_hwi_presets[i].short_descr,
da2d47
-				          sizeof ( info->short_descr ) );
da2d47
+		          sizeof ( info->short_descr )-1 );
da2d47
 
da2d47
 	   if ( _papi_hwi_presets[i].long_descr != NULL )
da2d47
 	      strncpy( info->long_descr,  _papi_hwi_presets[i].long_descr,
da2d47
-				          sizeof ( info->long_descr ) );
da2d47
+		          sizeof ( info->long_descr )-1 );
da2d47
 
da2d47
 	   info->event_type = _papi_hwi_presets[i].event_type;
da2d47
 	   info->count = _papi_hwi_presets[i].count;
da2d47
@@ -2185,17 +2187,17 @@ _papi_hwi_get_preset_event_info( int EventCode, PAPI_event_info_t * info )
da2d47
 
da2d47
 	   if ( _papi_hwi_presets[i].postfix != NULL )
da2d47
 	      strncpy( info->postfix, _papi_hwi_presets[i].postfix,
da2d47
-				          sizeof ( info->postfix ) );
da2d47
+		          sizeof ( info->postfix )-1 );
da2d47
 
da2d47
 	   for(j=0;j < info->count; j++) {
da2d47
 	      info->code[j]=_papi_hwi_presets[i].code[j];
da2d47
 	      strncpy(info->name[j], _papi_hwi_presets[i].name[j],
da2d47
-		      sizeof(info->name[j]));
da2d47
+	      sizeof(info->name[j])-1);
da2d47
 	   }
da2d47
 
da2d47
 	   if ( _papi_hwi_presets[i].note != NULL ) {
da2d47
 	      strncpy( info->note, _papi_hwi_presets[i].note,
da2d47
-				          sizeof ( info->note ) );
da2d47
+		          sizeof ( info->note )-1 );
da2d47
 	   }
da2d47
 
da2d47
 	   return PAPI_OK;
da2d47
diff --git a/src/papi_user_events.c b/src/papi_user_events.c
da2d47
index 04fc4af..b1f124a 100644
da2d47
--- a/src/papi_user_events.c
da2d47
+++ b/src/papi_user_events.c
da2d47
@@ -246,10 +246,11 @@ get_event_line( char **place, FILE * table, char **tmp_perfmon_events_table )
da2d47
 
da2d47
 int add_define( char *line, list_t* LIST ) {
da2d47
   char *t;
da2d47
-  char local_line[USER_EVENT_OPERATION_LEN];
da2d47
+  char local_line[USER_EVENT_OPERATION_LEN+1];
da2d47
   list_t *temp;
da2d47
 
da2d47
   strncpy( local_line, line, USER_EVENT_OPERATION_LEN );
da2d47
+  local_line[USER_EVENT_OPERATION_LEN] = '\0';
da2d47
 
da2d47
   temp = (list_t*)papi_malloc(sizeof(list_t));
da2d47
 
da2d47
@@ -262,12 +263,14 @@ int add_define( char *line, list_t* LIST ) {
da2d47
   
da2d47
   /* next token should be the name */
da2d47
   t = strtok(NULL, " ");
da2d47
-  strncpy( temp->name, t, PAPI_MIN_STR_LEN);
da2d47
+  strncpy( temp->name, t, PAPI_MIN_STR_LEN-1);
da2d47
+  temp->name[PAPI_MIN_STR_LEN-1] = '\0';
da2d47
 
da2d47
   /* next token should be the value */
da2d47
   t = strtok(NULL," ");
da2d47
   t[strlen(t)] = '\0';
da2d47
-  strncpy( temp->value, t, PAPI_MIN_STR_LEN);
da2d47
+  strncpy( temp->value, t, PAPI_MIN_STR_LEN-1);
da2d47
+  temp->value[PAPI_MIN_STR_LEN-1] = '\0';
da2d47
 
da2d47
   temp->next = LIST->next;
da2d47
   LIST->next = temp;
da2d47
@@ -395,12 +398,15 @@ check_preset_events (char *target, user_defined_event_t* ue, int* msi)
da2d47
 		strcat(ue->operation, temp);
da2d47
 		ue->events[ue->count++] = _papi_hwi_presets[j].code[0];
da2d47
 	  } else {
da2d47
-		op = '-';
da2d47
 		switch ( _papi_hwi_presets[j].derived_int ) {
da2d47
 		  case DERIVED_ADD:
da2d47
 		  case DERIVED_ADD_PS:
da2d47
-			op = '+';
da2d47
 		  case DERIVED_SUB:
da2d47
+	    if (_papi_hwi_presets[j].derived_int == DERIVED_SUB) {
da2d47
+		op = '-';
da2d47
+	    } else {
da2d47
+		op = '+';
da2d47
+	    }
da2d47
 			for ( k = 0; k < (int) _papi_hwi_presets[j].count; k++) {
da2d47
 			  ue->events[ue->count++] = _papi_hwi_presets[j].code[k];
da2d47
 			  if (k%2)
da2d47
@@ -574,7 +580,8 @@ load_user_event_table( char *file_name)
da2d47
 	  goto nextline;
da2d47
 	} 
da2d47
 
da2d47
-	strncpy(foo->symbol, t, PAPI_MIN_STR_LEN);
da2d47
+   // the entire structure was zeroed so if we only copy one less that what fits in the 'symbol' buffer, it will insure that this buffer is NULL terminated
da2d47
+   strncpy(foo->symbol, t, PAPI_MIN_STR_LEN-1);
da2d47
 #ifdef SHOW_LOADS
da2d47
 	INTDBG("Found a user event named %s\n", foo->symbol );
da2d47
 #endif
da2d47
diff --git a/src/sw_multiplex.c b/src/sw_multiplex.c
da2d47
index 22db6c2..4b2109c 100644
da2d47
--- a/src/sw_multiplex.c
da2d47
+++ b/src/sw_multiplex.c
da2d47
@@ -1136,8 +1136,6 @@ mpx_check( int EventSet )
da2d47
 	if ( strcmp( _papi_hwi_system_info.hw_info.model_string, "POWER6" ) == 0 ) {
da2d47
 		unsigned int chk_domain =
da2d47
 			PAPI_DOM_USER + PAPI_DOM_KERNEL + PAPI_DOM_SUPERVISOR;
da2d47
-		if ( ESI == NULL )
da2d47
-			return ( PAPI_ENOEVST );
da2d47
 
da2d47
 		if ( ( ESI->domain.domain & chk_domain ) != chk_domain ) {
da2d47
 			PAPIERROR
da2d47
diff --git a/src/x86_cpuid_info.c b/src/x86_cpuid_info.c
da2d47
index dacc021..2527f3c 100644
da2d47
--- a/src/x86_cpuid_info.c
da2d47
+++ b/src/x86_cpuid_info.c
da2d47
@@ -1416,8 +1416,6 @@ init_intel_leaf2( PAPI_mh_info_t * mh_info , int *num_levels)
da2d47
 	int size;						   /* size of the descriptor table */
da2d47
 	int last_level = 0;				   /* how many levels in the cache hierarchy */
da2d47
 
da2d47
-	int need_leaf4=0;
da2d47
-
da2d47
 	/* All of Intel's cache info is in 1 call to cpuid
da2d47
 	 * however it is a table lookup :(
da2d47
 	 */
da2d47
@@ -1459,7 +1457,6 @@ init_intel_leaf2( PAPI_mh_info_t * mh_info , int *num_levels)
da2d47
 				if ( i ) {	 /* skip the low order byte in eax [0]; it's the count (see above) */
da2d47
 				   if ( reg.descrip[i] == 0xff ) {
da2d47
 				      MEMDBG("Warning! PAPI x86_cache: must implement cpuid leaf 4\n");
da2d47
-				      need_leaf4=1;
da2d47
 				      return PAPI_ENOSUPP;
da2d47
 				      /* we might continue instead */
da2d47
 				      /* in order to get TLB info  */
da2d47
@@ -1480,9 +1477,6 @@ init_intel_leaf2( PAPI_mh_info_t * mh_info , int *num_levels)
da2d47
 early_exit:
da2d47
 	MEMDBG( "# of Levels: %d\n", last_level );
da2d47
 	*num_levels=last_level;
da2d47
-	if (need_leaf4) {
da2d47
-	   return PAPI_ENOSUPP;
da2d47
-	}
da2d47
 	return PAPI_OK;
da2d47
 }
da2d47