Skip to content

Commit 6807c19

Browse files
committed
lib: cmetrics: upgrade to v2.1.4
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent 6372ddb commit 6807c19

3 files changed

Lines changed: 86 additions & 19 deletions

File tree

lib/cmetrics/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
66
# CMetrics Version
77
set(CMT_VERSION_MAJOR 2)
88
set(CMT_VERSION_MINOR 1)
9-
set(CMT_VERSION_PATCH 3)
9+
set(CMT_VERSION_PATCH 4)
1010
set(CMT_VERSION_STR "${CMT_VERSION_MAJOR}.${CMT_VERSION_MINOR}.${CMT_VERSION_PATCH}")
1111

1212
# Include helpers

lib/cmetrics/src/cmt_decode_prometheus.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,16 @@ int cmt_decode_prometheus_create(
124124

125125
result = cmt_decode_prometheus_parse(scanner, &context);
126126

127-
if (result == 0) {
127+
if (context.errcode) {
128+
result = context.errcode;
129+
cmt_destroy(cmt);
130+
reset_context(&context, true);
131+
}
132+
else if (result == 0) {
128133
*out_cmt = cmt;
129134
}
130135
else {
131136
cmt_destroy(cmt);
132-
if (context.errcode) {
133-
result = context.errcode;
134-
}
135137
reset_context(&context, true);
136138
}
137139

lib/cmetrics/src/cmt_decode_prometheus.l

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,79 @@
77
%{
88

99
#include <cmetrics/cmt_decode_prometheus.h>
10+
#include <stdio.h>
1011

1112
#define STRBUF_RET \
1213
yylval->str = context->strbuf; \
1314
context->strbuf = NULL
1415

16+
static void set_allocation_error(struct cmt_decode_prometheus_context *context)
17+
{
18+
context->errcode = CMT_DECODE_PROMETHEUS_ALLOCATION_ERROR;
19+
20+
if (context->opts.errbuf != NULL && context->opts.errbuf_size > 0) {
21+
snprintf(context->opts.errbuf,
22+
context->opts.errbuf_size - 1,
23+
"memory allocation failed");
24+
}
25+
}
26+
27+
static int reset_strbuf(struct cmt_decode_prometheus_context *context)
28+
{
29+
if (context->strbuf != NULL) {
30+
cfl_sds_destroy(context->strbuf);
31+
}
32+
33+
context->strbuf = cfl_sds_create_size(256);
34+
if (context->strbuf == NULL) {
35+
set_allocation_error(context);
36+
37+
return -1;
38+
}
39+
40+
return 0;
41+
}
42+
43+
static int append_strbuf(struct cmt_decode_prometheus_context *context,
44+
const char *text, int length)
45+
{
46+
cfl_sds_t result;
47+
48+
result = cfl_sds_cat(context->strbuf, text, length);
49+
if (result == NULL) {
50+
set_allocation_error(context);
51+
52+
return -1;
53+
}
54+
55+
context->strbuf = result;
56+
57+
return 0;
58+
}
59+
60+
#define STRBUF_CREATE() \
61+
do { \
62+
if (reset_strbuf(context) != 0) { \
63+
return 0; \
64+
} \
65+
} while (0)
66+
67+
#define STRBUF_APPEND(text, length) \
68+
do { \
69+
if (append_strbuf(context, (text), (length)) != 0) { \
70+
return 0; \
71+
} \
72+
} while (0)
73+
74+
#define SET_STR_TOKEN() \
75+
do { \
76+
yylval->str = cfl_sds_create(yytext); \
77+
if (yylval->str == NULL) { \
78+
set_allocation_error(context); \
79+
return 0; \
80+
} \
81+
} while (0)
82+
1583
%}
1684

1785
/* here we define some states that allow us to create rules only
@@ -74,7 +142,7 @@
74142

75143
<HELPTAG,TYPETAG>[^ \t]+ {
76144
// The next token will be the metric name
77-
yylval->str = cfl_sds_create(yytext);
145+
SET_STR_TOKEN();
78146
return YYSTATE == HELPTAG ? HELP : TYPE;
79147
}
80148

@@ -86,7 +154,7 @@
86154
// separate start condition for this to handle "\\" and "\n" escapes
87155
// more easily.
88156
BEGIN(INHELPTAG);
89-
context->strbuf = cfl_sds_create_size(256);
157+
STRBUF_CREATE();
90158
}
91159
else {
92160
// For TYPETAG we enter INTYPETAG start condition to check only valid
@@ -107,17 +175,17 @@
107175

108176
<INHELPTAG>\\n {
109177
// Process linefeed escape sequence
110-
context->strbuf = cfl_sds_cat(context->strbuf, "\n", 1);
178+
STRBUF_APPEND("\n", 1);
111179
}
112180

113181
<INHELPTAG>\\\\ {
114182
// Process backslack escape sequence
115-
context->strbuf = cfl_sds_cat(context->strbuf, "\\", 1);
183+
STRBUF_APPEND("\\", 1);
116184
}
117185

118186
<INHELPTAG>[^\r\n\\]+ {
119187
// Put everything that is not a backslash or a line feed into strbuf
120-
context->strbuf = cfl_sds_cat(context->strbuf, yytext, yyleng);
188+
STRBUF_APPEND(yytext, yyleng);
121189
}
122190

123191
<INTYPETAG>counter {
@@ -146,26 +214,23 @@
146214

147215
["] {
148216
BEGIN(INQUOTE);
149-
if (context->strbuf != NULL) {
150-
cfl_sds_destroy(context->strbuf);
151-
}
152-
context->strbuf = cfl_sds_create_size(256);
217+
STRBUF_CREATE();
153218
}
154219

155220
<INQUOTE>[\\]["] {
156-
context->strbuf = cfl_sds_cat(context->strbuf, "\"", 1);
221+
STRBUF_APPEND("\"", 1);
157222
}
158223

159224
<INQUOTE>\\n {
160-
context->strbuf = cfl_sds_cat(context->strbuf, "\n", 1);
225+
STRBUF_APPEND("\n", 1);
161226
}
162227

163228
<INQUOTE>\\\\ {
164-
context->strbuf = cfl_sds_cat(context->strbuf, "\\", 1);
229+
STRBUF_APPEND("\\", 1);
165230
}
166231

167232
<INQUOTE>[^\r\n\\"]+ {
168-
context->strbuf = cfl_sds_cat(context->strbuf, yytext, yyleng);
233+
STRBUF_APPEND(yytext, yyleng);
169234
}
170235

171236
<INQUOTE>["] {
@@ -180,7 +245,7 @@
180245
}
181246

182247
[a-zA-Z_][a-zA-Z_0-9]* {
183-
yylval->str = cfl_sds_create(yytext);
248+
SET_STR_TOKEN();
184249
return IDENTIFIER;
185250
}
186251

0 commit comments

Comments
 (0)