diff --git a/src/cprof_encode_opentelemetry.c b/src/cprof_encode_opentelemetry.c index 59409c2..967793c 100644 --- a/src/cprof_encode_opentelemetry.c +++ b/src/cprof_encode_opentelemetry.c @@ -2188,6 +2188,7 @@ static int pack_cprof_scope_profiles( result = pack_cprof_instrumentation_scope(&otlp_scope_profiles->scope, input_instance->scope); if (result != CPROF_ENCODE_OPENTELEMETRY_SUCCESS) { + destroy_scope_profiles(otlp_scope_profiles); return result; } } @@ -2246,6 +2247,7 @@ static int pack_cprof_resource_profiles( result = pack_cprof_resource(&otlp_resource_profiles->resource, input_instance->resource); if (result != CPROF_ENCODE_OPENTELEMETRY_SUCCESS) { + destroy_resource_profiles(otlp_resource_profiles); return result; } diff --git a/src/cprof_profile.c b/src/cprof_profile.c index 2c833a4..1cf7a22 100644 --- a/src/cprof_profile.c +++ b/src/cprof_profile.c @@ -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; @@ -239,18 +239,25 @@ 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; } @@ -258,9 +265,11 @@ size_t cprof_profile_string_add(struct cprof_profile *profile, char *str, int st 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; }