Skip to content

Commit

Permalink
profile: fixed potential NULL dereference (CID 516428)
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Alminana <[email protected]>
  • Loading branch information
leonardo-albertovich committed Dec 20, 2024
1 parent 97685eb commit 6adb24f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/cprof_profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void cprof_profile_destroy(struct cprof_profile *instance)

size_t cprof_profile_string_add(struct cprof_profile *profile, char *str, int str_len)
{
int alloc_size = 64;
const int alloc_size = 64;
size_t id;
size_t new_size;

Expand All @@ -239,28 +239,37 @@ size_t cprof_profile_string_add(struct cprof_profile *profile, char *str, int st
str_len = strlen(str);
}

if (!profile->string_table && str_len > 0) {
profile->string_table = malloc(alloc_size * sizeof(cfl_sds_t));
if (!profile->string_table) {
if (profile->string_table == NULL) {
profile->string_table = calloc(alloc_size, sizeof(cfl_sds_t));

if (profile->string_table == NULL) {
return -1;
}

profile->string_table_size = alloc_size;
profile->string_table_count = 0;
}

if (profile->string_table_count == 0 && str_len > 0) {
/* string_table[0] must always be "" */
profile->string_table[0] = cfl_sds_create_len("", 0);

if (!profile->string_table[0]) {
return -1;
}

profile->string_table_count = 1;
}

/* check there is enough room for a new entry */
if (profile->string_table_count >= profile->string_table_size) {
new_size = profile->string_table_size + alloc_size;
profile->string_table = realloc(profile->string_table, new_size * sizeof(cfl_sds_t));

if (!profile->string_table) {
return -1;
}

profile->string_table_size = alloc_size;
}

Expand Down

0 comments on commit 6adb24f

Please sign in to comment.