Commit 5bd98310 authored by Hendryk Bockelmann's avatar Hendryk Bockelmann
Browse files

add local memcpy of attribute key-value pair to prevent dangling poiunter

parent 4b265d3b
......@@ -333,11 +333,17 @@ void sct_add_report_attribute_int(const char *key, int val) {
sct_attribute_type *a;
if ( !(a = (sct_attribute_type*) malloc(sizeof(sct_attribute_type))) )
sct_abort("memory allocation failed", __FILE__, __LINE__);
a->key = key;
a->type = SCT_INT;
int *i = (int*) malloc(sizeof(int));
*i = val;
a->value = i;
char *k = (char*) malloc(strlen(key)+1);
memcpy(k, key, strlen(key)+1);
a->key = k;
int *v = (int*) malloc(sizeof(int));
*v = val;
a->value = v;
a->next = attribute_table->first;
attribute_table->first = a;
}
......@@ -348,11 +354,17 @@ void sct_add_report_attribute_long(const char *key, long val) {
sct_attribute_type *a;
if ( !(a = (sct_attribute_type*) malloc(sizeof(sct_attribute_type))) )
sct_abort("memory allocation failed", __FILE__, __LINE__);
a->key = key;
a->type = SCT_LONG;
long *i = (long*) malloc(sizeof(long));
*i = val;
a->value = i;
char *k = (char*) malloc(strlen(key)+1);
memcpy(k, key, strlen(key)+1);
a->key = k;
long *v = (long*) malloc(sizeof(long));
*v = val;
a->value = v;
a->next = attribute_table->first;
attribute_table->first = a;
}
......@@ -363,11 +375,17 @@ void sct_add_report_attribute_float(const char *key, float val) {
sct_attribute_type *a;
if ( !(a = (sct_attribute_type*) malloc(sizeof(sct_attribute_type))) )
sct_abort("memory allocation failed", __FILE__, __LINE__);
a->key = key;
a->type = SCT_FLOAT;
float *i = (float*) malloc(sizeof(float));
*i = val;
a->value = i;
char *k = (char*) malloc(strlen(key)+1);
memcpy(k, key, strlen(key)+1);
a->key = k;
float *v = (float*) malloc(sizeof(float));
*v = val;
a->value = v;
a->next = attribute_table->first;
attribute_table->first = a;
}
......@@ -378,11 +396,17 @@ void sct_add_report_attribute_double(const char *key, double val) {
sct_attribute_type *a;
if ( !(a = (sct_attribute_type*) malloc(sizeof(sct_attribute_type))) )
sct_abort("memory allocation failed", __FILE__, __LINE__);
a->key = key;
a->type = SCT_DOUBLE;
double *i = (double*) malloc(sizeof(double));
*i = val;
a->value = i;
char *k = (char*) malloc(strlen(key)+1);
memcpy(k, key, strlen(key)+1);
a->key = k;
double *v = (double*) malloc(sizeof(double));
*v = val;
a->value = v;
a->next = attribute_table->first;
attribute_table->first = a;
}
......@@ -393,9 +417,17 @@ void sct_add_report_attribute_string(const char *key, char *val) {
sct_attribute_type *a;
if ( !(a = (sct_attribute_type*) malloc(sizeof(sct_attribute_type))) )
sct_abort("memory allocation failed", __FILE__, __LINE__);
a->key = key;
a->type = SCT_STRING;
a->value = val;
char *k = (char*) malloc(strlen(key)+1);
memcpy(k, key, strlen(key)+1);
a->key = k;
char *v = (char*) malloc(strlen(val)+1);
memcpy(v, val, strlen(val)+1);
a->value = v;
a->next = attribute_table->first;
attribute_table->first = a;
}
......@@ -405,10 +437,8 @@ void sct_internal_free_attribute_table(void) {
sct_attribute_type *a, *nexta;
for (a = attribute_table->first; nexta != NULL; a = nexta) {
nexta = a->next;
if (a->type == SCT_INT ||
a->type == SCT_LONG ||
a->type == SCT_FLOAT ||
a->type == SCT_DOUBLE) free(a->value);
free(a->key);
free(a->value);
free(a);
}
free(attribute_table);
......
......@@ -132,7 +132,7 @@ typedef enum {
/*! \brief attribute type to hold a generic key-value pair to be used for reporting */
typedef struct sct_attribute {
const char* key;
char* key;
void* value;
sct_attribute_value_type type;
struct sct_attribute *next;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment