Skip to content
Myr

Strip UTF-8 BOM at the start of each input file (#3657)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The UTF-8 BOM was stripped only at the start of the parser stream, but jq feeds all input files into a single parser, so a BOM at the start of the second and later files reached the parser as content and failed with a confusing "Invalid numeric literal" error. Strip it in the file readers instead, at the start of each input file including stdin, and in jv_load_file. This also strips the BOM for raw input, so -R and --rawfile no longer yield a leading U+FEFF, and a BOM at the start of a -f program file or an imported module is now accepted rather than reported as a syntax error. In exchange, "\uFEFF1" | fromjson is now a parse error, since a U+FEFF inside a string is content rather than an encoding marker, and the dedicated "Malformed BOM" error is gone.

authored by itchyny · committed by GitHub · parent 17b87d2 · Browse files · 5 files+34 added−35 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+34 added−35 removed

  • tests:+11 added−7 removed
  • other:+23 added−28 removed

5 files+34 added−35 removed

modified:src/jv_file.c+13 added-2 removed
41 unmodified lines
42 // To avoid mangling UTF-8 multi-byte sequences that cross the end of our read43 // buffer, we need to be able to read the remainder of a sequence and add that44 // before appending.45 char buf[4096 + 4];46 int at_file_start = 1;47 while (!feof(file) && !ferror(file)) {48 size_t n = fread(buf, 1, sizeof(buf) - 4, file);49 if (n == 0)50 continue;
4 unmodified lines
55 !feof(file) && !ferror(file)) {56 n += fread(end, 1, len, file);57 }5859 char *p = buf;60 if (at_file_start) {61 // Strip UTF-8 BOM at the start of the file62 if (n >= 3 && !memcmp(buf, "\xEF\xBB\xBF", 3)) {63 p += 3;64 n -= 3;65 }66 at_file_start = 0;67 }6869 if (raw) {59 data = jv_string_append_buf(data, buf, n);70 data = jv_string_append_buf(data, p, n);71 if (!jv_is_valid(data))72 break;73 } else {63 jv_parser_set_buf(parser, buf, n, !feof(file));74 jv_parser_set_buf(parser, p, n, !feof(file));75 jv value;76 while (jv_is_valid((value = jv_parser_next(parser))))77 data = jv_array_append(data, value);78 if (jv_invalid_has_msg(jv_copy(value))) {
17 unmodified lines
modified:src/jv_parse.c-26 removed
35 unmodified lines
36 int curr_buf_length;37 int curr_buf_pos;38 int curr_buf_is_partial;39 int eof;40 unsigned bom_strip_position;4041 int flags;4243 jv* stack; // parser
44 unmodified lines
88 p->st = JV_PARSER_NORMAL;89 p->eof = 0;90 p->curr_buf = 0;91 p->curr_buf_length = p->curr_buf_pos = p->curr_buf_is_partial = 0;93 p->bom_strip_position = 0;92 p->last_ch_was_ws = 0;93 p->line = 1;94 p->column = 0;95 jvp_dtoa_context_init(&p->dtoa);
630 unmodified lines
726 parser_free(p);727 jv_mem_free(p);728}729732static const unsigned char UTF8_BOM[] = {0xEF,0xBB,0xBF};733730int jv_parser_remaining(struct jv_parser* p) {731 if (p->curr_buf == 0)732 return 0;733 return (p->curr_buf_length - p->curr_buf_pos);
1 unmodified line
735736void jv_parser_set_buf(struct jv_parser* p, const char* buf, int length, int is_partial) {737 assert((p->curr_buf == 0 || p->curr_buf_pos == p->curr_buf_length)738 && "previous buffer not exhausted");743 while (length > 0 && p->bom_strip_position < sizeof(UTF8_BOM)) {744 if ((unsigned char)*buf == UTF8_BOM[p->bom_strip_position]) {745 // matched a BOM character746 buf++;747 length--;748 p->bom_strip_position++;749 } else {750 if (p->bom_strip_position == 0) {751 // no BOM in this document752 p->bom_strip_position = sizeof(UTF8_BOM);753 } else {754 // malformed BOM (prefix present, rest missing)755 p->bom_strip_position = 0xff;756 }757 }758 }739 p->curr_buf = buf;740 p->curr_buf_length = length;741 p->curr_buf_pos = 0;742 p->curr_buf_is_partial = is_partial;
15 unmodified lines
758 if (p->eof)759 return jv_invalid();760 if (!p->curr_buf)761 return jv_invalid(); // Need a buffer782 if (p->bom_strip_position == 0xff) {783 if (!(p->flags & JV_PARSE_SEQ))784 return jv_invalid_with_msg(jv_string("Malformed BOM"));785 p->st =JV_PARSER_WAITING_FOR_RS;786 parser_reset(p);787 }762 jv value = jv_invalid();763 if ((p->flags & JV_PARSE_STREAMING) && stream_check_done(p, &value))764 return value;765 char ch;
133 unmodified lines
modified:src/util.c+10 added
191 unmodified lines
192 char buf[4096];193 size_t buf_valid_len;194 jv current_filename;195 size_t current_line;196 int at_file_start;197};198199static void fprinter(void *data, const char *fname) {200 fprintf((FILE *)data, "jq: error: Could not open file %s: %s\n", fname, strerror(errno));
76 unmodified lines
277 const char *f = next_file(state);278 if (f != NULL) {279 jv_free(state->current_filename);280 state->current_line = 0;281 state->at_file_start = 1;282 if (!strcmp(f, "-")) {283 state->current_input = stdin;284 state->current_filename = jv_string("<stdin>");285 } else {
58 unmodified lines
344 } else {345 state->buf_valid_len = (p - state->buf) + 1;346 }347 }348 if (state->at_file_start) {349 // Strip UTF-8 BOM at the start of each file350 if (state->buf_valid_len >= 3 && !memcmp(state->buf, "\xEF\xBB\xBF", 3)) {351 state->buf_valid_len -= 3;352 memmove(state->buf, state->buf + 3, state->buf_valid_len);353 }354 state->at_file_start = 0;355 }356 }357 return state->curr_file == state->nfiles && !state->current_input;358}359
909 unmodified lines
modified:tests/jq.test-7 removed
40 unmodified lines
41{"x":-1}42{"x":-1}43{"x":1}4445# The input line starts with a 0xFEFF (byte order mark) codepoint46# No, there is no reason to have a byte order mark in UTF8 text.47# But apparently people do, so jq shouldn't break on it.48.49"byte order mark"50"byte order mark"5145# We test escapes by matching them against Unicode codepoints46# FIXME: more tests needed for weird unicode stuff (e.g. utf16 pairs)47"Aa\r\n\t\b\f\u03bc"48null
2741 unmodified lines
modified:tests/shtest+11 added
55 unmodified lines
56 exit 157 fi58done5960## Test UTF-8 BOM is stripped at the start of each input file61printf '\357\273\277[{"foo": 1}\n,{"foo": 2}\n]\n' > $d/bom1.json62printf '\357\273\277[{"foo": 3}\n,{"foo": 4}\n]\n' > $d/bom2.json63$VALGRIND $Q $JQ -e '.[0].foo == 1' $d/bom1.json64$VALGRIND $Q $JQ -se '. == [[{"foo":1},{"foo":2}],[{"foo":3},{"foo":4}]]' $d/bom1.json $d/bom2.json65$VALGRIND $Q $JQ -ne '[inputs] == [[{"foo":1},{"foo":2}],[{"foo":3},{"foo":4}]]' $d/bom1.json $d/bom2.json66$VALGRIND $Q $JQ -ne '[inputs] == [[{"foo":1},{"foo":2}],[{"foo":3},{"foo":4}]]' $d/bom1.json - < $d/bom2.json67$VALGRIND $Q $JQ -Rne '[inputs] == ["[{\"foo\": 1}", ",{\"foo\": 2}", "]", "[{\"foo\": 3}", ",{\"foo\": 4}", "]"]' $d/bom1.json $d/bom2.json68$VALGRIND $Q $JQ -ne --slurpfile x $d/bom1.json '$x == [[{"foo":1},{"foo":2}]]'69$VALGRIND $Q $JQ -ne --rawfile x $d/bom1.json '$x == "[{\"foo\": 1}\n,{\"foo\": 2}\n]\n"'7071## Test JSON sequence support72cat > $d/expected <<EOF73jq: ignoring parse error: Truncated value at line 2, column 574jq: ignoring parse error: Truncated value at line 2, column 25
896 unmodified lines