Skip to content

Commit

Permalink
Merge pull request #249 from MrAnno/fix-filterx-csv-race-cond
Browse files Browse the repository at this point in the history
csvparser: fix filterx race condition
  • Loading branch information
bazsi authored Aug 12, 2024
2 parents a7aec7a + f6e3895 commit 243a51d
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions modules/csvparser/filterx-func-parse-csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ _parse_list_argument(FilterXFunctionParseCSV *self, FilterXExpr *list_expr, GLis
return result;
}

static inline void
_init_scanner(FilterXFunctionParseCSV *self, GList *string_delimiters, GList *cols, const gchar *input,
CSVScanner *scanner, CSVScannerOptions *local_opts)
{
CSVScannerOptions *opts = &self->options;

if (string_delimiters || cols)
{
csv_scanner_options_copy(local_opts, &self->options);
opts = local_opts;
}

if (string_delimiters)
csv_scanner_options_set_string_delimiters(local_opts, string_delimiters);

if (cols)
csv_scanner_options_set_expected_columns(local_opts, g_list_length(cols));

csv_scanner_init(scanner, opts, input);
}

static FilterXObject *
_eval(FilterXExpr *s)
{
Expand All @@ -97,7 +118,6 @@ _eval(FilterXExpr *s)
if (!obj)
return NULL;

CSVScanner scanner;
gboolean ok = FALSE;
FilterXObject *result = NULL;
GList *cols = NULL;
Expand All @@ -114,21 +134,17 @@ _eval(FilterXExpr *s)
FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRING_DELIMITERS))
goto exit;

if (string_delimiters)
csv_scanner_options_set_string_delimiters(&self->options, string_delimiters);

if (!_parse_list_argument(self, self->columns, &cols, FILTERX_FUNC_PARSE_CSV_ARG_NAME_COLUMNS))
goto exit;

if (cols)
{
csv_scanner_options_set_expected_columns(&self->options, g_list_length(cols));
result = filterx_json_object_new_empty();
}
result = filterx_json_object_new_empty();
else
result = filterx_json_array_new_empty();

csv_scanner_init(&scanner, &self->options, input);
CSVScanner scanner;
CSVScannerOptions local_opts = {0};
_init_scanner(self, string_delimiters, cols, input, &scanner, &local_opts);

GList *col = cols;
while (csv_scanner_scan_next(&scanner))
Expand Down Expand Up @@ -163,6 +179,7 @@ _eval(FilterXExpr *s)
}

exit:
csv_scanner_options_clean(&local_opts);
if (!ok)
{
filterx_object_unref(result);
Expand Down

0 comments on commit 243a51d

Please sign in to comment.