Skip to content

Commit

Permalink
test-csv: test handling of trailing NUL byte
Browse files Browse the repository at this point in the history
Data after a NUL byte should be ignored.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
  • Loading branch information
xypron authored and vathpela committed Sep 8, 2021
1 parent ecaf92a commit 58e8dce
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ parse_csv_data(char *data, char *data_end, size_t n_columns, list_t *list)

end = data_end;
max = (uintptr_t)end - (uintptr_t)line + (end > line ? 1 : 0);
/* Skip the delimiter(s) of the previous line */
while (max && found) {
found = false;
for (delim = &delims[0]; max && *delim; delim++) {
Expand All @@ -87,6 +88,7 @@ parse_csv_data(char *data, char *data_end, size_t n_columns, list_t *list)
}
}
}
/* Find the first delimiter of the current line */
for (delim = &delims[0]; *delim; delim++) {
char *tmp = strnchrnul(line, max, *delim);
if (tmp < end)
Expand Down
63 changes: 63 additions & 0 deletions test-csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,68 @@ test_csv_2(void)
return -1;
}

int
test_csv_3(void)
{
char csv[] =
"a,b,c,d,e,f,g,h\n"
"a,b,c\n"
"\n"
"\n"
"a,b,c,d,e,f,g,h\n"
"a,b,c\0x,y\0z\0";
struct test_entry test_entries[]= {
{ 7, { "a", "b", "c", "d", "e", "f", "g" } },
{ 3, { "a", "b", "c", NULL, NULL, NULL, NULL } },
{ 7, { "a", "b", "c", "d", "e", "f", "g" } },
{ 3, { "a", "b", "c", NULL, NULL, NULL, NULL } },
};
list_t entry_list;
size_t i;
char *current, *end;
list_t *pos = NULL;
EFI_STATUS efi_status;

INIT_LIST_HEAD(&entry_list);
assert_equal_return(list_size(&entry_list), 0, -1,
"got %d expected %d\n");

current = csv;
end = csv + sizeof(csv) - 1;

efi_status = parse_csv_data(current, end, 7, &entry_list);
assert_equal_return(efi_status, EFI_SUCCESS, -1, "got %x expected %x\n");

i = 0;
list_for_each(pos, &entry_list) {
struct csv_row *csv_row;
struct test_entry *test_entry = &test_entries[i++];
size_t j;

assert_goto(i > 0 && i <= 4, fail, "got %d expected 0 to 4\n", i);

csv_row = list_entry(pos, struct csv_row, list);

assert_equal_goto(csv_row->n_columns, test_entry->n_columns,
fail, "got %d expected %d\n");
for (j = 0; j < csv_row->n_columns; j++) {
assert_equal_goto(strcmp(csv_row->columns[j],
test_entry->columns[j]), 0,
fail, "got %d expected %d\n");
}
}

assert_equal_return(list_size(&entry_list), 4, -1,
"got %d expected %d\n");
free_csv_list(&entry_list);
assert_equal_return(list_size(&entry_list), 0, -1,
"got %d expected %d\n");
return 0;
fail:
free_csv_list(&entry_list);
return -1;
}

int
test_simple_sbat_csv(void)
{
Expand Down Expand Up @@ -456,6 +518,7 @@ main(void)
test(test_csv_0);
test(test_csv_1);
test(test_csv_2);
test(test_csv_3);
test(test_simple_sbat_csv);
test(test_csv_simple_fuzz, random_bin, random_bin_len, false);
for (i = 0; i < random_bin_len; i++) {
Expand Down

0 comments on commit 58e8dce

Please sign in to comment.