Skip to content

Commit 94493a2

Browse files
authored
fix: edge cases parsing of DD_TAGS (#233)
1 parent 52597cf commit 94493a2

File tree

2 files changed

+83
-65
lines changed

2 files changed

+83
-65
lines changed

src/datadog/parse_util.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,10 @@ Expected<std::unordered_map<std::string, std::string>> parse_tags(
141141

142142
if (separator == std::string::npos) {
143143
key = std::string{trim(token)};
144+
if (key.empty()) continue;
144145
} else {
145146
key = std::string{trim(token.substr(0, separator))};
146-
if (key.empty()) {
147-
continue;
148-
}
147+
if (key.empty()) continue;
149148
value = std::string{trim(token.substr(separator + 1))};
150149
}
151150

@@ -183,15 +182,14 @@ Expected<std::unordered_map<std::string, std::string>> parse_tags(
183182
break;
184183
} else if (input[i] == ' ') {
185184
separator = ' ';
186-
break;
187185
}
188186
}
189187

190188
if (separator == 0) {
191189
goto capture_all;
192190
}
193191

194-
for (; i < end; ++i) {
192+
for (i = beg; i < end; ++i) {
195193
if (input[i] == separator) {
196194
auto tag = input.substr(beg, i - beg);
197195
if (tag.size() > 0) {

test/test_parse_util.cpp

Lines changed: 80 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -145,56 +145,72 @@ PARSE_UTIL_TEST("parse_tags") {
145145
};
146146

147147
auto test_case = GENERATE(values<TestCase>({
148+
{
149+
__LINE__,
150+
"space separated tags",
151+
"env:test aKey:aVal bKey:bVal cKey:",
152+
{
153+
{"env", "test"},
154+
{"aKey", "aVal"},
155+
{"bKey", "bVal"},
156+
{"cKey", ""},
157+
},
158+
},
159+
{
160+
__LINE__,
161+
"comma separated tags",
162+
"env:test,aKey:aVal,bKey:bVal,cKey:",
163+
{
164+
{"env", "test"},
165+
{"aKey", "aVal"},
166+
{"bKey", "bVal"},
167+
{"cKey", ""},
168+
},
169+
},
170+
{
171+
__LINE__,
172+
"mixed separator 1/3",
173+
"env:test,aKey:aVal bKey:bVal cKey:",
174+
{
175+
{"env", "test"},
176+
{"aKey", "aVal bKey:bVal cKey:"},
177+
},
178+
},
179+
{
180+
__LINE__,
181+
"mixed separator 2/3",
182+
"env:test bKey :bVal, dKey: dVal cKey:",
183+
{
184+
{"env", "test bKey :bVal"},
185+
{"dKey", "dVal cKey:"},
186+
},
187+
},
188+
{
189+
__LINE__,
190+
"mixed separator 3/3",
191+
"env :test, aKey : aVal bKey:bVal cKey:",
192+
{
193+
{"env", "test"},
194+
{"aKey", "aVal bKey:bVal cKey:"},
195+
},
196+
},
197+
{
198+
__LINE__,
199+
"multiple semi-colons",
200+
"env:keyWithA:Semicolon bKey:bVal cKey",
201+
{
202+
{"env", "keyWithA:Semicolon"},
203+
{"bKey", "bVal"},
204+
{"cKey", ""},
205+
},
206+
},
148207
{__LINE__,
149-
"space separated tags",
150-
"env:test aKey:aVal bKey:bVal cKey:",
208+
"mixed separator edge case",
209+
"env:keyWith: , , Lots:Of:Semicolons ",
151210
{
152-
{"env", "test"},
153-
{"aKey", "aVal"},
154-
{"bKey", "bVal"},
155-
{"cKey", ""},
211+
{"env", "keyWith:"},
212+
{"Lots", "Of:Semicolons"},
156213
}},
157-
{__LINE__,
158-
"comma separated tags",
159-
"env:test aKey:aVal bKey:bVal cKey:",
160-
{
161-
{"env", "test"},
162-
{"aKey", "aVal"},
163-
{"bKey", "bVal"},
164-
{"cKey", ""},
165-
}},
166-
{__LINE__,
167-
"mixed separator but comma first",
168-
"env:test,aKey:aVal bKey:bVal cKey:",
169-
{
170-
{"env", "test"},
171-
{"aKey", "aVal bKey:bVal cKey:"},
172-
}},
173-
{__LINE__,
174-
"mixed separator but space first",
175-
"env:test bKey :bVal dKey: dVal cKey:",
176-
{
177-
{"env", "test"},
178-
{"bKey", ""},
179-
{"dKey", ""},
180-
{"dVal", ""},
181-
{"cKey", ""},
182-
}},
183-
{__LINE__,
184-
"mixed separator but space first",
185-
"env:keyWithA:Semicolon bKey:bVal cKey",
186-
{
187-
{"env", "keyWithA:Semicolon"},
188-
{"bKey", "bVal"},
189-
{"cKey", ""},
190-
}},
191-
// {__LINE__,
192-
// "mixed separator edge case",
193-
// "env:keyWith: , , Lots:Of:Semicolons ",
194-
// {
195-
// {"env", "keyWith:"},
196-
// {"Lots", "Of:Semicolons"},
197-
// }},
198214
{__LINE__,
199215
"comma separated but some tags without value",
200216
"a:b,c,d",
@@ -210,19 +226,23 @@ PARSE_UTIL_TEST("parse_tags") {
210226
{"a", ""},
211227
{"1", ""},
212228
}},
213-
{__LINE__,
214-
"no separator",
215-
"a:b:c:d",
216-
{
217-
{"a", "b:c:d"},
218-
}},
219-
{__LINE__,
220-
"input is trimed",
221-
"key1:val1, key2 : val2 ",
222-
{
223-
{"key1", "val1"},
224-
{"key2", "val2"},
225-
}},
229+
{
230+
__LINE__,
231+
"no separator",
232+
"a:b:c:d",
233+
{
234+
{"a", "b:c:d"},
235+
},
236+
},
237+
{
238+
__LINE__,
239+
"input is trimed",
240+
"key1:val1, key2 : val2 ",
241+
{
242+
{"key1", "val1"},
243+
{"key2", "val2"},
244+
},
245+
},
226246
}));
227247

228248
CAPTURE(test_case.line);

0 commit comments

Comments
 (0)