diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/fieldstat.cpp | 39 | ||||
| -rw-r--r-- | src/fieldstat_dynamic.cpp | 8 | ||||
| -rw-r--r-- | src/fieldstat_internal.h | 4 |
3 files changed, 45 insertions, 6 deletions
diff --git a/src/fieldstat.cpp b/src/fieldstat.cpp index 4be0ce2..2126d82 100644 --- a/src/fieldstat.cpp +++ b/src/fieldstat.cpp @@ -35,9 +35,14 @@ char* __str_dup(const char* str) int is_valid_field_name(const char* name) { - const char* reserverd="|:\n\r. \t<>[]#!@"; + const char* reserverd="|:\n\r \t<>[]#!@"; unsigned int i=0,j=0; - for(i=0;i<strlen(name);i++) + unsigned int len=strlen(name); + if(len == 0) + { + return 0; + } + for(i=0;i<len;i++) { for(j=0;j<strlen(reserverd);j++) if(name[i]==reserverd[j]) @@ -88,6 +93,30 @@ int is_valid_tags(const struct fieldstat_tag *tags, size_t n_tags) return 1; } +void escaping_special_chars(char* str) +{ + const char *escaped = "."; + unsigned i = 0, j = 0; + unsigned int len = strlen(str); + if(len == 0) + { + return; + } + + for(i = 0; i < len; i++) + { + for(j = 0; j < strlen(escaped); j++) + { + if(str[i] == escaped[j]) + { + str[i] = '_'; + } + } + } + +} + + void get_current_table_line_cnt(struct fieldstat_instance *instance, int n_table, int *tables_line_cnt) { for(int i = 0; i < n_table; i++) @@ -157,6 +186,7 @@ struct metric * metric_new(enum field_type type, const char *field_name, const s { struct metric *metric = (struct metric*)calloc(1, sizeof(struct metric)); metric->field_name = __str_dup(field_name); + escaping_special_chars(metric->field_name); metric->field_type = type; metric->is_ratio = 0; metric->output_scaling = 1; @@ -597,7 +627,7 @@ struct fieldstat_instance * fieldstat_instance_new(const char *name) } instance = (struct fieldstat_instance *)calloc(1, sizeof(struct fieldstat_instance)); - strcpy(instance->name, name); + strncpy(instance->name, name, strlen(name)); instance->running = 0; instance->output_interval_ms = 2000; instance->background_thread_disable = 0; @@ -671,11 +701,13 @@ struct table_metric* table_metric_new(const char *name, const char *column_name[ table_metric->column_cnt = (int)n_column; table_metric->name = __str_dup(name); + escaping_special_chars(table_metric->name); for(i = 0; i < (int)n_column; i++) { table_metric->column_name[i] = __str_dup(column_name[i]); table_metric->column_type[i] = column_type[i]; + escaping_special_chars(table_metric->column_name[i]); } return table_metric; } @@ -792,6 +824,7 @@ struct table_line *table_line_new(const char *name, const struct fieldstat_tag t struct table_line *table_line = (struct table_line *)calloc(1, sizeof(struct table_line)); table_line->name = __str_dup(name); + escaping_special_chars(table_line->name); table_line->n_tag = n_tag; add_tags_to_metric(tags, n_tag, table_line->tag_key, table_line->tag_value); diff --git a/src/fieldstat_dynamic.cpp b/src/fieldstat_dynamic.cpp index 597ee3b..dd583e6 100644 --- a/src/fieldstat_dynamic.cpp +++ b/src/fieldstat_dynamic.cpp @@ -16,7 +16,8 @@ struct fieldstat_dynamic_instance * fieldstat_dynamic_instance_new(const char *n instance = (struct fieldstat_dynamic_instance *)calloc(1, sizeof(struct fieldstat_dynamic_instance)); - strcpy(instance->name, name); + strncpy(instance->name, name, strlen(name)); + instance->running = 0; instance->output_interval_ms = 2000; instance->background_thread_disable = 0; @@ -242,8 +243,11 @@ static int build_dynamic_metric_key(int table_id, const char *field_name, const int i = 0; int used_len = 0; struct fieldstat_tag *tag = NULL; + char unescaped_field_name[256] = {0}; + strncpy(unescaped_field_name, field_name, sizeof(unescaped_field_name) - 1); + escaping_special_chars(unescaped_field_name); - used_len += snprintf(out_key + used_len, out_key_size - used_len, "%d%s", table_id, field_name); + used_len += snprintf(out_key + used_len, out_key_size - used_len, "%d%s", table_id, unescaped_field_name); for(i = 0; i < (int)n_tags; i++) { diff --git a/src/fieldstat_internal.h b/src/fieldstat_internal.h index db191c0..4ea31d4 100644 --- a/src/fieldstat_internal.h +++ b/src/fieldstat_internal.h @@ -273,4 +273,6 @@ struct table_line ** read_table_line_slot(struct table_metric *table, int line_i void fieldstat_global_disable_prometheus_endpoint(); int enable_line_protocol_output(struct line_protocol_output *line_protocol_output, const char *ip, unsigned short port); -void disable_line_protocol_output(struct line_protocol_output *line_protocol_output);
\ No newline at end of file +void disable_line_protocol_output(struct line_protocol_output *line_protocol_output); + +void escaping_special_chars(char* str);
\ No newline at end of file |
