Skip to content

Commit 602b910

Browse files
edsiperolegmukhin
authored andcommitted
pack: fix token count regression in JSON tokenizer and remove parser reset
This patch fixes a regression in flb_json_tokenise() where failure to reinitialize the jsmn_parser after reallocating tokens led to invalid or duplicated token counts on repeated parses. Signed-off-by: Eduardo Silva <[email protected]>
1 parent 1e32006 commit 602b910

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/flb_pack.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ int flb_json_tokenise(const char *js, size_t len,
6464

6565
ret = jsmn_parse(&state->parser, js, len,
6666
state->tokens, state->tokens_size);
67+
6768
while (ret == JSMN_ERROR_NOMEM) {
6869
/* Get current size of the array in bytes */
6970
old_size = state->tokens_size * sizeof(jsmntok_t);
@@ -79,13 +80,11 @@ int flb_json_tokenise(const char *js, size_t len,
7980
state->tokens = tmp;
8081
state->tokens_size += new_tokens;
8182

82-
/* Reset parser to reprocess the JSON data from the beginning */
83-
jsmn_init(&state->parser);
84-
8583
ret = jsmn_parse(&state->parser, js, len,
8684
state->tokens, state->tokens_size);
8785
}
8886

87+
8988
if (ret == JSMN_ERROR_INVAL) {
9089
return FLB_ERR_JSON_INVAL;
9190
}
@@ -96,7 +95,8 @@ int flb_json_tokenise(const char *js, size_t len,
9695
return FLB_ERR_JSON_PART;
9796
}
9897

99-
state->tokens_count += ret;
98+
/* always use jsmn_parser.toknext to count tokens */
99+
state->tokens_count = state->parser.toknext;
100100
return 0;
101101
}
102102

0 commit comments

Comments
 (0)