diff --git a/src/components/coretemp/linux-coretemp.c b/src/components/coretemp/linux-coretemp.c index 5acb98ab5..f7f47e768 100644 --- a/src/components/coretemp/linux-coretemp.c +++ b/src/components/coretemp/linux-coretemp.c @@ -106,6 +106,7 @@ generateEventList(char *base_dir) struct dirent *hwmonx; int i,pathnum; int retlen; + int err = PAPI_OK; #define NUM_PATHS 2 char paths[NUM_PATHS][PATH_MAX]={ @@ -133,6 +134,7 @@ generateEventList(char *base_dir) base_dir, hwmonx->d_name,paths[pathnum]); if (retlen <= 0 || PATH_MAX <= retlen) { SUBDBG("Path length is too long.\n"); + closedir(dir); return PAPI_EINVAL; } SUBDBG("Trying to open %s\n",path); @@ -146,7 +148,8 @@ generateEventList(char *base_dir) retlen = snprintf(filename, PAPI_MAX_STR_LEN, "%s/name",path); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Module name too long.\n"); - return PAPI_EINVAL; + err = PAPI_EINVAL; + goto done_error; } fff=fopen(filename,"r"); if (fff==NULL) { @@ -176,7 +179,8 @@ generateEventList(char *base_dir) path,i); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Failed to construct location label.\n"); - return PAPI_EINVAL; + err = PAPI_EINVAL; + goto done_error; } fff=fopen(filename,"r"); if (fff==NULL) { @@ -194,7 +198,8 @@ generateEventList(char *base_dir) path,i); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Failed input temperature string.\n"); - return PAPI_EINVAL; + err = PAPI_EINVAL; + goto done_error; } fff=fopen(filename,"r"); if (fff==NULL) continue; @@ -203,9 +208,8 @@ generateEventList(char *base_dir) retlen = snprintf(name, PAPI_MAX_STR_LEN, "%s:in%i_input", hwmonx->d_name, i); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Unable to generate name %s:in%i_input\n", hwmonx->d_name, i); - closedir(dir); - closedir(d); - return ( PAPI_EINVAL ); + err = PAPI_EINVAL; + goto done_error; } snprintf(units, PAPI_MIN_STR_LEN, "V"); @@ -214,10 +218,12 @@ generateEventList(char *base_dir) location); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("snprintf failed.\n"); - return PAPI_EINVAL; + err = PAPI_EINVAL; + goto done_error; } if (insert_in_list(name,units,description,filename)!=PAPI_OK) { + err = PAPI_ECMP; goto done_error; } @@ -236,7 +242,8 @@ generateEventList(char *base_dir) path,i); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Location label string failed.\n"); - return PAPI_EINVAL; + err = PAPI_EINVAL; + goto done_error; } fff=fopen(filename,"r"); if (fff==NULL) { @@ -254,7 +261,8 @@ generateEventList(char *base_dir) path,i); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Input temperature string failed.\n"); - return PAPI_EINVAL; + err = PAPI_EINVAL; + goto done_error; } fff=fopen(filename,"r"); if (fff==NULL) continue; @@ -263,9 +271,8 @@ generateEventList(char *base_dir) retlen = snprintf(name, PAPI_MAX_STR_LEN, "%s:temp%i_input", hwmonx->d_name, i); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Unable to generate name %s:temp%i_input\n", hwmonx->d_name, i); - closedir(d); - closedir(dir); - return ( PAPI_EINVAL ); + err = PAPI_EINVAL; + goto done_error; } snprintf(units, PAPI_MIN_STR_LEN, "degrees C"); @@ -274,10 +281,12 @@ generateEventList(char *base_dir) location); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("snprintf failed.\n"); - return PAPI_EINVAL; + err = PAPI_EINVAL; + goto done_error; } if (insert_in_list(name,units,description,filename)!=PAPI_OK) { + err = PAPI_ECMP; goto done_error; } @@ -295,7 +304,8 @@ generateEventList(char *base_dir) path,i); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Failed to write fan label string.\n"); - return PAPI_EINVAL; + err = PAPI_EINVAL; + goto done_error; } fff=fopen(filename,"r"); if (fff==NULL) { @@ -312,9 +322,8 @@ generateEventList(char *base_dir) retlen = snprintf(filename, PAPI_MAX_STR_LEN, "%s/fan%d_input", path,i); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Unable to generate filename %s/fan%d_input\n", path,i); - closedir(d); - closedir(dir); - return ( PAPI_EINVAL ); + err = PAPI_EINVAL; + goto done_error; } fff=fopen(filename,"r"); @@ -324,9 +333,8 @@ generateEventList(char *base_dir) retlen = snprintf(name, PAPI_MAX_STR_LEN, "%s:fan%i_input", hwmonx->d_name, i); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("Unable to generate name %s:fan%i_input\n", hwmonx->d_name, i); - closedir(d); - closedir(dir); - return ( PAPI_EINVAL ); + err = PAPI_EINVAL; + goto done_error; } snprintf(units, PAPI_MIN_STR_LEN, "RPM"); @@ -335,10 +343,12 @@ generateEventList(char *base_dir) location); if (retlen <= 0 || PAPI_MAX_STR_LEN <= retlen) { SUBDBG("snprintf failed.\n"); - return PAPI_EINVAL; + err = PAPI_EINVAL; + goto done_error; } if (insert_in_list(name,units,description,filename)!=PAPI_OK) { + err = PAPI_ECMP; goto done_error; } @@ -356,7 +366,7 @@ generateEventList(char *base_dir) done_error: closedir(d); closedir(dir); - return PAPI_ECMP; + return err; } static long long diff --git a/src/components/pcp/linux-pcp.c b/src/components/pcp/linux-pcp.c index 5c3ddc248..fce881035 100644 --- a/src/components/pcp/linux-pcp.c +++ b/src/components/pcp/linux-pcp.c @@ -857,6 +857,9 @@ static int _pcp_init_component(int cidx) { int retval = PAPI_OK; char *reason = _papi_hwd[cidx]->cmp_info.disabled_reason; // For error messages. + char **allNames = NULL; + pmID *allPMID = NULL; + pmResult *allFetch = NULL; // result of pmFetch. int rLen = PAPI_HUGE_STR_LEN; // Most I will print. reason[rLen-1]=0; // last resort terminator. @@ -941,17 +944,24 @@ static int _pcp_init_component(int cidx) } int i,j,k; - char **allNames=calloc(sEventCount, sizeof(char*)); // Make an array for all names. + allNames=calloc(sEventCount, sizeof(char*)); // Make an array for all names. + if (allNames == NULL) { + snprintf(reason, rLen, "memory alloc denied for allNames; " + "size=%i.\n", sEventCount); + int strErr=snprintf(reason, rLen, "Could not allocate %lu bytes of memory for allNames.", sEventCount*sizeof(allNames)); + if (strErr > rLen) HANDLE_STRING_ERROR; + retval = PAPI_ENOMEM; + goto fn_fail; + } for (i=0; i rLen) HANDLE_STRING_ERROR; retval = PAPI_ENOMEM; goto fn_fail; @@ -1003,7 +1013,6 @@ static int _pcp_init_component(int cidx) for (i=0; icmp_info.disabled = retval; return retval; fn_fail: diff --git a/src/components/powercap/linux-powercap.c b/src/components/powercap/linux-powercap.c index 5d72f739a..d15269e81 100644 --- a/src/components/powercap/linux-powercap.c +++ b/src/components/powercap/linux-powercap.c @@ -134,6 +134,10 @@ static char * _local_strlcpy( char *dst, const char *src, size_t size ) static long long read_powercap_value( int index ) { int sz = pread(event_fds[index], read_buff, PAPI_MAX_STR_LEN, 0); + if (sz == -1) { + perror("Error in pread(): "); + return (long long) 0; + } read_buff[sz] = '\0'; return atoll(read_buff); @@ -207,6 +211,7 @@ static int _powercap_init_component( int cidx ) if (event_fd == -1) { break; } int sz = pread(event_fd, read_buff, PAPI_MAX_STR_LEN, 0); + if (sz == -1) { HANDLE_STRING_ERROR; } read_buff[sz] = '\0'; close(event_fd); @@ -254,6 +259,7 @@ static int _powercap_init_component( int cidx ) if(powercap_ntv_events[num_events].type == PKG_NAME) { int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0); + if (sz == -1) HANDLE_STRING_ERROR; read_buff[sz] = '\0'; strErr=snprintf(powercap_ntv_events[num_events].description, sizeof(powercap_ntv_events[num_events].description), "%s", read_buff); powercap_ntv_events[num_events].description[sizeof(powercap_ntv_events[num_events].description)-1]=0; @@ -262,6 +268,7 @@ static int _powercap_init_component( int cidx ) if(powercap_ntv_events[num_events].type == PKG_MAX_ENERGY_RANGE) { int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0); + if (sz == -1) HANDLE_STRING_ERROR; read_buff[sz] = '\0'; max_pkg_energy_count = atoll(read_buff); } @@ -302,6 +309,7 @@ static int _powercap_init_component( int cidx ) if(powercap_ntv_events[num_events].type == COMPONENT_NAME) { int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0); + if (sz == -1) HANDLE_STRING_ERROR; read_buff[sz] = '\0'; strErr=snprintf(powercap_ntv_events[num_events].description, sizeof(powercap_ntv_events[num_events].description), "%s", read_buff); powercap_ntv_events[num_events].description[sizeof(powercap_ntv_events[num_events].description)-1]=0; @@ -310,6 +318,7 @@ static int _powercap_init_component( int cidx ) if(powercap_ntv_events[num_events].type == COMPONENT_MAX_ENERGY_RANGE) { int sz = pread(event_fds[num_events], read_buff, PAPI_MAX_STR_LEN, 0); + if (sz == -1) HANDLE_STRING_ERROR; read_buff[sz] = '\0'; max_component_energy_count = atoll(read_buff); } diff --git a/src/linux-memory.c b/src/linux-memory.c index 67a0ae12f..bb2d401f5 100644 --- a/src/linux-memory.c +++ b/src/linux-memory.c @@ -1215,7 +1215,7 @@ generic_get_memory_info( PAPI_hw_info_t *hw_info ) { int type=0,level,result; - int size,line_size,associativity,sets; + int size,line_size=0,associativity=0,sets=0; int write_policy,allocation_policy; DIR *dir; FILE *fff; diff --git a/src/papi.c b/src/papi.c index 8aff08061..b65418500 100644 --- a/src/papi.c +++ b/src/papi.c @@ -1614,6 +1614,7 @@ PAPI_event_name_to_code( const char *in, int *out ) char *evt_base_name = strtok(evt_name_copy, ":"); if( NULL == evt_base_name ) { PAPIERROR("Failed to allocate space for base name of native event used in preset.\n"); + free(evt_name_copy); papi_return( PAPI_EINVAL ); } diff --git a/src/papi_internal.c b/src/papi_internal.c index 197974101..63a92ae43 100644 --- a/src/papi_internal.c +++ b/src/papi_internal.c @@ -373,6 +373,10 @@ _papi_hwi_add_native_event(int cidx, int ntv_event, int ntv_idx, const char *eve _papi_native_events[num_native_events].ntv_idx=ntv_idx; if (event_name != NULL) { _papi_native_events[num_native_events].evt_name=strdup(event_name); + if (_papi_native_events[num_native_events].evt_name == NULL) { + new_native_event=PAPI_ENOMEM; + goto native_alloc_early_out; + } } else { _papi_native_events[num_native_events].evt_name=NULL; } @@ -641,6 +645,9 @@ PAPIWARN( char *format, ... ) /* Construct fully qualified event names for the native events in a preset. */ int construct_qualified_event(hwi_presets_t *prstPtr) { + int ret = PAPI_OK; + char *tmpEvent = NULL; + char *tmpQuals = NULL; int j; for(j = 0; j < prstPtr->count; j++ ) { @@ -652,12 +659,13 @@ construct_qualified_event(hwi_presets_t *prstPtr) { strLenSum += baseLen; /* Allocate space for constructing fully qualified event. */ - char *tmpEvent = (char*)malloc(strLenSum*sizeof(char)); - char *tmpQuals = (char*)malloc(strLenSum*sizeof(char)); + tmpEvent = (char*)malloc(strLenSum*sizeof(char)); + tmpQuals = (char*)malloc(strLenSum*sizeof(char)); if( NULL == tmpQuals || NULL == tmpEvent ) { - SUBDBG("EXIT: Could not allocate memory.\n"); - return PAPI_ENOMEM; + PAPIERROR("Could not allocate memory for tmpQuals and tmpEvents.\n"); + ret = PAPI_ENOMEM; + goto done; } /* Print the basename to a string. */ @@ -665,7 +673,8 @@ construct_qualified_event(hwi_presets_t *prstPtr) { if( status < 0 || status >= baseLen ) { PAPIERROR("Event basename %s was truncated to %s in derived event %s", prstPtr->base_name[j], tmpEvent, prstPtr->symbol); - return PAPI_ENOMEM; + ret = PAPI_ENOMEM; + goto done; } /* Concatenate the qualifiers onto the string. */ @@ -677,41 +686,67 @@ construct_qualified_event(hwi_presets_t *prstPtr) { if( status < 0 || status >= strLenSum ) { PAPIERROR("Event %s with qualifiers was truncated to %s in derived event %s", prstPtr->base_name[j], tmpEvent, prstPtr->symbol); - return PAPI_ENOMEM; + ret = PAPI_ENOMEM; + goto done; } /* Set the new name, which includes the qualifiers. */ free(prstPtr->name[j]); prstPtr->name[j] = strdup(tmpEvent); + if (prstPtr->name[j] == NULL) { + PAPIERROR("Could not allocate memory for event name %s.", tmpEvent); + ret = PAPI_ENOMEM; + goto done; + } /* Set the corresponding new code. */ status = _papi_hwi_native_name_to_code( tmpEvent, &(prstPtr->code[j]) ); if( PAPI_OK != status ) { PAPIERROR("Failed to get code for native event %s used in derived event %s\n", tmpEvent, prstPtr->symbol); - return PAPI_EINVAL; + ret = PAPI_EINVAL; } - - /* Free dynamically allocated memory. */ - free(tmpQuals); - free(tmpEvent); } - return PAPI_OK; +done: + /* Free dynamically allocated memory. */ + free(tmpQuals); + free(tmpEvent); + return ret; } /* Overwrite qualifiers in the preset struct based on those provided in the input string. */ int overwrite_qualifiers(hwi_presets_t *prstPtr, const char *in, int is_preset) { - + int ret = PAPI_OK; char *qualDelim = ":"; - char **providedQuals = (char**)malloc(sizeof(char*)*(prstPtr->num_quals)); + char **providedQuals = NULL; int numProvidedQuals = 0; int k; + char *givenName = NULL; + + providedQuals = (char**)calloc(prstPtr->num_quals, sizeof(char*)); + if (providedQuals == NULL) { + PAPIERROR("Could not allocate %lu bytes of memory for providedQuals.", + sizeof(char*)*(prstPtr->num_quals)); + ret = PAPI_ENOMEM; + goto done; + } + for (k = 0; k < prstPtr->num_quals; k++){ providedQuals[k] = (char*)malloc(sizeof(char)*(PAPI_MAX_STR_LEN+1)); + PAPIERROR("Could not allocate %lu bytes of memory for providedQuals element.", + sizeof(char)*(PAPI_MAX_STR_LEN+1)); + ret = PAPI_ENOMEM; + goto done; + } + + givenName = strdup(in); + if (givenName == NULL) { + PAPIERROR("Could not allocate memory for givenName %s.", in); + ret = PAPI_ENOMEM; + goto done; } - char *givenName = strdup(in); char *qualName = strtok(givenName, ":"); qualName = strtok(NULL, ":"); @@ -726,7 +761,8 @@ overwrite_qualifiers(hwi_presets_t *prstPtr, const char *in, int is_preset) { int status = snprintf(providedQuals[k], qualLen, "%s%s", qualDelim, qualName); if( status < 0 || status >= qualLen ) { PAPIERROR("Failed to make copy of qualifier %s", qualName); - return PAPI_ENOMEM; + ret = PAPI_ENOMEM; + goto done; } k++; numProvidedQuals++; @@ -742,11 +778,24 @@ overwrite_qualifiers(hwi_presets_t *prstPtr, const char *in, int is_preset) { /* For each qualifier provided. */ for (k = 0; k < numProvidedQuals; k++) { wholeQual1 = strdup(providedQuals[k]); + if (wholeQual1 == NULL) { + PAPIERROR("Failed to make copy of qualifier %s", providedQuals[k]); + ret = PAPI_ENOMEM; + goto done; + } + matchQual1 = strtok(wholeQual1, "="); /* For each qualifier in the preset struct. */ for (l = 0; l < prstPtr->num_quals; l++) { wholeQual2 = strdup(prstPtr->quals[l]); + if (wholeQual2 == NULL) { + PAPIERROR("Failed to make copy of qualifier %s", providedQuals[k]); + free(wholeQual1); + ret = PAPI_ENOMEM; + goto done; + } + matchQual2 = strtok(wholeQual2, "="); if( strcmp(matchQual1, matchQual2) == 0 ) { breakFlag = 1; @@ -761,17 +810,23 @@ overwrite_qualifiers(hwi_presets_t *prstPtr, const char *in, int is_preset) { if( breakFlag ) { free(prstPtr->quals[l]); prstPtr->quals[l] = strdup(providedQuals[k]); + if (prstPtr->quals[l] == NULL) { + PAPIERROR("Failed to make copy of qualifier %s", providedQuals[k]); + ret = PAPI_ENOMEM; + goto done; + } breakFlag = 0; } } +done: free(givenName); for (k = 0; k < prstPtr->num_quals; k++){ free(providedQuals[k]); } free(providedQuals); - return PAPI_OK; + return ret; } /* Return index of first non-perf_event component's preset. */ @@ -815,7 +870,7 @@ get_preset_cmp( unsigned int *index ) { hwi_presets_t* get_preset( int event_code ) { unsigned int preset_index = ( event_code & PAPI_PRESET_AND_MASK ); - hwi_presets_t *_papi_hwi_list; + hwi_presets_t *_papi_hwi_list = NULL; int i = get_preset_cmp(&preset_index); if( i == PAPI_EINVAL ) { @@ -828,6 +883,7 @@ get_preset( int event_code ) { _papi_hwi_list = _papi_hwi_comp_presets[i]; } + assert(_papi_hwi_list != NULL); return &_papi_hwi_list[preset_index]; } @@ -2752,6 +2808,10 @@ _papi_hwi_native_name_to_code( const char *in, int *out ) } full_event_name = strdup(in); + if (full_event_name == NULL) { + INTDBG("EXIT: PAPI_EINVAL\n"); + return PAPI_EINVAL; + } in = _papi_hwi_strip_component_prefix(in); diff --git a/src/papi_preset.c b/src/papi_preset.c index 3522c3b68..6df98faff 100644 --- a/src/papi_preset.c +++ b/src/papi_preset.c @@ -989,9 +989,7 @@ papi_load_derived_events (char *pmu_str, int pmu_type, int cidx, int preset_flag int get_events = 0; /* only process derived events after CPU type they apply to is identified */ int found_events = 0; /* flag to track if event definitions (PRESETS) are found since last CPU declaration */ int breakAfter = 0; /* flag to break parsing events file if component 'arch' has already been parsed */ -#ifdef PAPI_DATADIR - char path[PATH_MAX]; -#endif + char path[PATH_MAX]; if (preset_flag) { @@ -1007,10 +1005,11 @@ papi_load_derived_events (char *pmu_str, int pmu_type, int cidx, int preset_flag else { #ifdef PAPI_DATADIR sprintf( path, "%s/%s", PAPI_DATADIR, PAPI_EVENT_FILE ); - event_file_path = path; #else - event_file_path = PAPI_EVENT_FILE; + sprintf( path, "%s", PAPI_EVENT_FILE ); #endif + + event_file_path = path; } event_type_bits = PAPI_PRESET_MASK; results = &_papi_hwi_presets[0]; @@ -1350,9 +1349,7 @@ papi_load_derived_events_component (char *comp_str, char *arch_str, int cidx) { int found_events = 0; /* flag to track if event definitions (PRESETS) are found since last CPU declaration */ int breakAfter = 0; /* flag to break parsing events file if component 'arch' has already been parsed */ int status = 0; -#ifdef PAPI_DATADIR - char path[PATH_MAX]; -#endif + char path[PATH_MAX]; /* try the environment variable first */ @@ -1367,10 +1364,10 @@ papi_load_derived_events_component (char *comp_str, char *arch_str, int cidx) { else { #ifdef PAPI_DATADIR sprintf( path, "%s/%s", PAPI_DATADIR, PAPI_EVENT_FILE ); - event_file_path = path; #else - event_file_path = PAPI_EVENT_FILE; + sprintf( path, "%s", PAPI_EVENT_FILE ); #endif + event_file_path = path; } event_type_bits = PAPI_PRESET_MASK; results = &_papi_hwi_comp_presets[cidx][0]; diff --git a/src/sde_lib/sde_lib_datastructures.c b/src/sde_lib/sde_lib_datastructures.c index d66cb93d6..0a93ffbcf 100644 --- a/src/sde_lib/sde_lib_datastructures.c +++ b/src/sde_lib/sde_lib_datastructures.c @@ -264,6 +264,11 @@ int exp_container_insert_element(recorder_data_t *exp_container, size_t typesize offset = 0; chunk += 1; // we need to allocate the next chunk from the last one we found. + if ( chunk >= EXP_CONTAINER_ENTRIES ) { + SDE_ERROR("exp_container_insert_element(): Exponential container at max capacity. Unable to insert element."); + return SDE_EINVAL; + } + new_segment_size = ((long long)1<ptr_array[chunk] = malloc(new_segment_size*typesize); exp_container->total_entries += new_segment_size; diff --git a/src/utils/papi_avail.c b/src/utils/papi_avail.c index f828e45b2..9fce8521b 100644 --- a/src/utils/papi_avail.c +++ b/src/utils/papi_avail.c @@ -356,8 +356,10 @@ int is_preset_event_available(char *name) { if ( info.count ) { if ( (check_counter && checkCounter (event_code)) || !check_counter) { - if (strcmp(info.symbol, basename) == 0) + if (strcmp(info.symbol, basename) == 0) { + free(localname); return 1; + } } } }