fix rrdlabels traversal (#17292)
Costa Tsaousis committed
Mar 29, 2024 at 17:51 UTC
609c235fc42e7bb543b344dabcb83815c85bc5dc
1 file changed
+28
-21
src/database/rrdlabels.c
+28
-21
@@ -977,7 +977,26 @@ int rrdlabels_walkthrough_read(RRDLABELS *labels, int (*callback)(const char *na
977
lfe_start_read(labels, lb, ls)
978
{
979
ret = callback(string2str(lb->index.key), string2str(lb->index.value), ls, data);
980
- if (ret != 0)
980
+ if (ret < 0)
981
+ break;
982
+ }
983
+ lfe_done(labels);
984
+
985
+ return ret;
986
+}
987
+
988
+static SIMPLE_PATTERN_RESULT rrdlabels_walkthrough_read_sp(RRDLABELS *labels, SIMPLE_PATTERN_RESULT (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data)
989
+{
990
+ SIMPLE_PATTERN_RESULT ret = SP_NOT_MATCHED;
991
+
992
+ if(unlikely(!labels || !callback)) return 0;
993
+
994
+ RRDLABEL *lb;
995
+ RRDLABEL_SRC ls;
996
+ lfe_start_read(labels, lb, ls)
997
+ {
998
+ ret = callback(string2str(lb->index.key), string2str(lb->index.value), ls, data);
999
+ if (ret != SP_NOT_MATCHED)
1000
break;
1001
}
1002
lfe_done(labels);
@@ -1119,21 +1138,16 @@ struct simple_pattern_match_name_value {
1138
char equal;
1139
};
1140
1122
-static int simple_pattern_match_name_only_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
1141
+static SIMPLE_PATTERN_RESULT simple_pattern_match_name_only_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
1142
struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
1143
(void)value;
1144
1145
// we return -1 to stop the walkthrough on first match
1146
t->searches++;
1128
- SIMPLE_PATTERN_RESULT rc = simple_pattern_matches_extract(t->pattern, name, NULL, 0);
1129
-
1130
- if(rc == SP_MATCHED_NEGATIVE)
1131
- return -1;
1132
-
1133
- return rc == SP_MATCHED_POSITIVE;
1147
+ return simple_pattern_matches_extract(t->pattern, name, NULL, 0);
1148
}
1149
1136
-static int simple_pattern_match_name_and_value_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
1150
+static SIMPLE_PATTERN_RESULT simple_pattern_match_name_and_value_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
1151
struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
1152
1153
// we return -1 to stop the walkthrough on first match
@@ -1157,12 +1171,7 @@ static int simple_pattern_match_name_and_value_callback(const char *name, const
1171
*dst = '\0';
1172
1173
t->searches++;
1160
- SIMPLE_PATTERN_RESULT rc = simple_pattern_matches_length_extract(t->pattern, tmp, dst - tmp, NULL, 0);
1161
-
1162
- if(rc == SP_MATCHED_NEGATIVE)
1163
- return -1;
1164
-
1165
- return rc == SP_MATCHED_POSITIVE ? 1 : 0;
1174
+ return simple_pattern_matches_length_extract(t->pattern, tmp, dst - tmp, NULL, 0);
1175
}
1176
1177
SIMPLE_PATTERN_RESULT rrdlabels_match_simple_pattern_parsed(RRDLABELS *labels, SIMPLE_PATTERN *pattern, char equal, size_t *searches) {
@@ -1174,15 +1183,12 @@ SIMPLE_PATTERN_RESULT rrdlabels_match_simple_pattern_parsed(RRDLABELS *labels, S
1183
.equal = equal
1184
};
1185
1177
- int ret = rrdlabels_walkthrough_read(labels, equal?simple_pattern_match_name_and_value_callback:simple_pattern_match_name_only_callback, &t);
1186
+ SIMPLE_PATTERN_RESULT ret = rrdlabels_walkthrough_read_sp(labels, equal?simple_pattern_match_name_and_value_callback:simple_pattern_match_name_only_callback, &t);
1187
1188
if(searches)
1189
*searches = t.searches;
1190
1182
- if(ret < 0)
1183
- return SP_MATCHED_NEGATIVE;
1184
-
1185
- return (ret > 0)?SP_MATCHED_POSITIVE:SP_NOT_MATCHED;
1191
+ return ret;
1192
}
1193
1194
bool rrdlabels_match_simple_pattern(RRDLABELS *labels, const char *simple_pattern_txt) {
@@ -1686,8 +1692,9 @@ static int rrdlabels_walkthrough_index_read(RRDLABELS *labels, int (*callback)(c
1692
lfe_start_read(labels, lb, ls)
1693
{
1694
ret = callback(string2str(lb->index.key), string2str(lb->index.value), ls, index, data);
1689
- if (ret != 0)
1695
+ if (ret < 0)
1696
break;
1697
+
1698
index++;
1699
}
1700
lfe_done(labels);