Skip to content

Commit

Permalink
fix bug misreporting initial cell length if file size < BOM length (3…
Browse files Browse the repository at this point in the history
… bytes) (#111)

* fix bug in first cell size if file size < BOM length (3 bytes) (issue #110) and add related test
  • Loading branch information
liquidaty authored Mar 2, 2023
1 parent 7974ce9 commit f77bc9e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion examples/lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ help:

build: simple print_my_column parse_by_chunk pull

test: test-eol
test: test-eol test-tiny

test-tiny: build/simple${EXE}
@[ "`echo '' | $< - 2>&1`" = "" ] && ${TEST_PASS} || ${TEST_FAIL}

test-eol: test-eol-1 test-eol-2 test-eol-3 test-eol-4

Expand Down
9 changes: 6 additions & 3 deletions src/zsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,16 @@ enum zsv_status zsv_parse_more(struct zsv_scanner *scanner) {
#endif
size_t bom_len = strlen(ZSV_BOM);
scanner->checked_bom = 1;
if(scanner->read(scanner->buff.buff, 1, bom_len, scanner->in) == bom_len
if((bytes_read = scanner->read(scanner->buff.buff, 1, bom_len, scanner->in)) == bom_len
&& !memcmp(scanner->buff.buff, ZSV_BOM, bom_len)) {
// have bom. disregard what we just read
bytes_read = scanner->read(scanner->buff.buff, 1, capacity, scanner->in);
scanner->had_bom = 1;
} else // no BOM. keep the bytes we just read
bytes_read = bom_len + scanner->read(scanner->buff.buff + bom_len, 1, capacity - bom_len, scanner->in);
} else { // no BOM. keep the bytes we just read
// bytes_read = bom_len + scanner->read(scanner->buff.buff + bom_len, 1, capacity - bom_len, scanner->in);
if(bytes_read == bom_len) // maybe we only read < 3 bytes
bytes_read += scanner->read(scanner->buff.buff + bom_len, 1, capacity - bom_len, scanner->in);
}
} else // already checked bom. read as usual
bytes_read = scanner->read(scanner->buff.buff + scanner->partial_row_length, 1,
capacity, scanner->in);
Expand Down

0 comments on commit f77bc9e

Please sign in to comment.