diff --git a/libcpuid/cpuid_main.c b/libcpuid/cpuid_main.c index 0e16e50..705efb9 100644 --- a/libcpuid/cpuid_main.c +++ b/libcpuid/cpuid_main.c @@ -344,17 +344,30 @@ static int cpuid_basic_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* dat static void make_list_from_string(const char* csv, struct cpu_list_t* list) { - int i, n, l, last; + int i, j, n, l, last; l = (int) strlen(csv); n = 0; for (i = 0; i < l; i++) if (csv[i] == ',') n++; n++; - list->num_entries = n; list->names = (char**) malloc(sizeof(char*) * n); + if (!list->names) { /* Memory allocation failed */ + list->num_entries = 0; + set_error(ERR_NO_MEM); + return; + } + list->num_entries = n; last = -1; n = 0; for (i = 0; i <= l; i++) if (i == l || csv[i] == ',') { list->names[n] = (char*) malloc(i - last); + if (!list->names[n]) { /* Memory allocation failed */ + set_error(ERR_NO_MEM); + for (j = 0; j < n; j++) free(list->names[j]); + free(list->names); + list->num_entries = 0; + list->names = NULL; + return; + } memcpy(list->names[n], &csv[last + 1], i - last - 1); list->names[n][i - last - 1] = '\0'; n++; @@ -799,6 +812,9 @@ void cpuid_get_cpu_list(cpu_vendor_t vendor, struct cpu_list_t* list) break; default: warnf("Unknown vendor passed to cpuid_get_cpu_list()\n"); + set_error(ERR_INVRANGE); + list->num_entries = 0; + list->names = NULL; break; } } diff --git a/libcpuid/libcpuid.h b/libcpuid/libcpuid.h index 3179e0c..b59761d 100644 --- a/libcpuid/libcpuid.h +++ b/libcpuid/libcpuid.h @@ -1010,7 +1010,9 @@ struct cpu_list_t { * order of the parts. * * @param vendor the vendor to be queried - * @param list [out] the resulting list will be written here. + * @param list [out] the resulting list will be written here. On failure, + * num_entries is set to zero and names to NULL. The error message can be + * obtained by calling \ref cpuid_error. @see cpu_error_t * NOTE: As the memory is dynamically allocated, be sure to call * cpuid_free_cpu_list() after you're done with the data * @see cpu_list_t diff --git a/libcpuid/libcpuid_util.c b/libcpuid/libcpuid_util.c index 9bed613..9addab4 100644 --- a/libcpuid/libcpuid_util.c +++ b/libcpuid/libcpuid_util.c @@ -141,6 +141,11 @@ void generic_get_cpu_list(const struct match_entry_t* matchtable, int count, int i, j, n, good; n = 0; list->names = (char**) malloc(sizeof(char*) * count); + if (!list->names) { /* Memory allocation failure */ + set_error(ERR_NO_MEM); + list->num_entries = 0; + return; + } for (i = 0; i < count; i++) { if (strstr(matchtable[i].name, "Unknown")) continue; good = 1; @@ -151,10 +156,21 @@ void generic_get_cpu_list(const struct match_entry_t* matchtable, int count, } if (!good) continue; #if defined(_MSC_VER) - list->names[n++] = _strdup(matchtable[i].name); + list->names[n] = _strdup(matchtable[i].name); #else - list->names[n++] = strdup(matchtable[i].name); + list->names[n] = strdup(matchtable[i].name); #endif + if (!list->names[n]) { /* Memory allocation failure */ + set_error(ERR_NO_MEM); + list->num_entries = 0; + for (j = 0; j < n; j++) { + free(list->names[j]); + } + free(list->names); + list->names = NULL; + return; + } + n++; } list->num_entries = n; } diff --git a/libcpuid/rdmsr.c b/libcpuid/rdmsr.c index bedf09e..84765c1 100644 --- a/libcpuid/rdmsr.c +++ b/libcpuid/rdmsr.c @@ -92,6 +92,10 @@ struct msr_driver_t* cpu_msr_driver_open_core(unsigned core_num) return NULL; } handle = (struct msr_driver_t*) malloc(sizeof(struct msr_driver_t)); + if (!handle) { + set_error(ERR_NO_MEM); + return NULL; + } handle->fd = fd; return handle; } @@ -176,6 +180,10 @@ struct msr_driver_t* cpu_msr_driver_open_core(unsigned core_num) return NULL; } handle = (struct msr_driver_t*) malloc(sizeof(struct msr_driver_t)); + if (!handle) { + set_error(ERR_NO_MEM); + return NULL; + } handle->fd = fd; return handle; }