1
0
Fork 0
mirror of https://github.com/anrieff/libcpuid synced 2025-01-23 20:06:41 +00:00

Report memory allocation failures without segfaulting. (#160)

This commit is contained in:
emixa-d 2022-01-23 01:38:40 +01:00 committed by GitHub
parent 2e61160983
commit bca7a19279
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 5 deletions

View file

@ -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) 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); l = (int) strlen(csv);
n = 0; n = 0;
for (i = 0; i < l; i++) if (csv[i] == ',') n++; for (i = 0; i < l; i++) if (csv[i] == ',') n++;
n++; n++;
list->num_entries = n;
list->names = (char**) malloc(sizeof(char*) * 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; last = -1;
n = 0; n = 0;
for (i = 0; i <= l; i++) if (i == l || csv[i] == ',') { for (i = 0; i <= l; i++) if (i == l || csv[i] == ',') {
list->names[n] = (char*) malloc(i - last); 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); memcpy(list->names[n], &csv[last + 1], i - last - 1);
list->names[n][i - last - 1] = '\0'; list->names[n][i - last - 1] = '\0';
n++; n++;
@ -799,6 +812,9 @@ void cpuid_get_cpu_list(cpu_vendor_t vendor, struct cpu_list_t* list)
break; break;
default: default:
warnf("Unknown vendor passed to cpuid_get_cpu_list()\n"); warnf("Unknown vendor passed to cpuid_get_cpu_list()\n");
set_error(ERR_INVRANGE);
list->num_entries = 0;
list->names = NULL;
break; break;
} }
} }

View file

@ -1010,7 +1010,9 @@ struct cpu_list_t {
* order of the parts. * order of the parts.
* *
* @param vendor the vendor to be queried * @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 * NOTE: As the memory is dynamically allocated, be sure to call
* cpuid_free_cpu_list() after you're done with the data * cpuid_free_cpu_list() after you're done with the data
* @see cpu_list_t * @see cpu_list_t

View file

@ -141,6 +141,11 @@ void generic_get_cpu_list(const struct match_entry_t* matchtable, int count,
int i, j, n, good; int i, j, n, good;
n = 0; n = 0;
list->names = (char**) malloc(sizeof(char*) * count); 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++) { for (i = 0; i < count; i++) {
if (strstr(matchtable[i].name, "Unknown")) continue; if (strstr(matchtable[i].name, "Unknown")) continue;
good = 1; good = 1;
@ -151,10 +156,21 @@ void generic_get_cpu_list(const struct match_entry_t* matchtable, int count,
} }
if (!good) continue; if (!good) continue;
#if defined(_MSC_VER) #if defined(_MSC_VER)
list->names[n++] = _strdup(matchtable[i].name); list->names[n] = _strdup(matchtable[i].name);
#else #else
list->names[n++] = strdup(matchtable[i].name); list->names[n] = strdup(matchtable[i].name);
#endif #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; list->num_entries = n;
} }

View file

@ -92,6 +92,10 @@ struct msr_driver_t* cpu_msr_driver_open_core(unsigned core_num)
return NULL; return NULL;
} }
handle = (struct msr_driver_t*) malloc(sizeof(struct msr_driver_t)); handle = (struct msr_driver_t*) malloc(sizeof(struct msr_driver_t));
if (!handle) {
set_error(ERR_NO_MEM);
return NULL;
}
handle->fd = fd; handle->fd = fd;
return handle; return handle;
} }
@ -176,6 +180,10 @@ struct msr_driver_t* cpu_msr_driver_open_core(unsigned core_num)
return NULL; return NULL;
} }
handle = (struct msr_driver_t*) malloc(sizeof(struct msr_driver_t)); handle = (struct msr_driver_t*) malloc(sizeof(struct msr_driver_t));
if (!handle) {
set_error(ERR_NO_MEM);
return NULL;
}
handle->fd = fd; handle->fd = fd;
return handle; return handle;
} }