Skip to content

Fix parsing stream with first token size >= parser's chunk size - #45

Merged
bzick merged 3 commits into
bzick:masterfrom
n-peugnet:issue-44-long-first-token
Jul 17, 2026
Merged

Fix parsing stream with first token size >= parser's chunk size#45
bzick merged 3 commits into
bzick:masterfrom
n-peugnet:issue-44-long-first-token

Conversation

@n-peugnet

@n-peugnet n-peugnet commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

When (first token size) >= (parser's chunk size), the next chunks are
loaded during the parsing of the first token. This triggers the end of
the parsing loop, leaving the first token as the only one parsed, and
thus having its "next" field set to nil. This in turn, lead
Stream.GoNext() to detect the end of the stream.

To fix this, Stream.GoNext() now starts by checking the next field of
the current token, parsing the current chunk if needed, before deciding
if the end of the stream is reached.

NextToken() also assumed that the next token was always parsed, which is
not the case. Add a new ensureParseNext() function that can be used in
both GoNext() and NextToken(), and that makes sure the next token is
parsed if the end of the stream has not been reached yet.

Tests have been added to prevent regressions.

Fixes #44

@n-peugnet
n-peugnet force-pushed the issue-44-long-first-token branch 3 times, most recently from 84c07ed to eab3a71 Compare July 7, 2026 13:47
@n-peugnet n-peugnet changed the title Failing test for issue 44 Fix parsing stream with first token >= to parser's buffer Jul 7, 2026
@n-peugnet
n-peugnet marked this pull request as ready for review July 7, 2026 16:30
@n-peugnet n-peugnet changed the title Fix parsing stream with first token >= to parser's buffer Fix parsing stream with first token >= to parser's chunk size-4 Jul 7, 2026
@n-peugnet n-peugnet changed the title Fix parsing stream with first token >= to parser's chunk size-4 Fix parsing stream with first token size > parser's chunk size-4 Jul 7, 2026
@n-peugnet
n-peugnet force-pushed the issue-44-long-first-token branch from 2e9b9e9 to 99801b8 Compare July 12, 2026 11:22
Comment thread stream_test.go Outdated
defer stream.Close()

require.True(t, stream.IsValid())
assert.True(t, stream.CurrentToken().IsKeyword())

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use require instead of assert

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@n-peugnet
n-peugnet force-pushed the issue-44-long-first-token branch from 99801b8 to fda5579 Compare July 13, 2026 09:15
Comment thread stream_test.go

// TestIssue44 The parser stops parsing the stream after the first token if
// its size is very close to the size of the parser's buffer.
func TestIssue44(t *testing.T) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At fisrt, thanks for the PR. The fix itself looks correct.

One small thing: I ran TestIssue44 andTestIssue44NextToken on master and they pass there too - so they don't actually catch the bug yet. The reason is the input is exactly 43 bytes with bufferSize = 43, so preload() reads everything in one shot and the first parse() already emits both tokens before any GoNext(). The bug only shows up when the reader has more data than fits in one buffer

Could you tweak the test data so it fails without the fix? Something like bufSize=20 with "this_token_is_exactly_40_characters_long 67 next_keyword_here 99" could reproduce it - 1 token instead of 4 on master, all 4 with your fix

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, this is because you added the fast path for ASCII, and ensureBytes(4) is not always called anymore. I will updates the tests and commit messages

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just made the changes and updated the commits and PR description and title

When (first token size) >= (parser's chunk size), the next chunks are
loaded during the parsing of the first token. This triggers the end of
the parsing loop, leaving the frist token as the only one parsed, and
thus having its "next" field set to nil. This in turn, lead
Stream.GoNext() to detect the end of the stream.

To fix this, Stream.GoNext() now starts by checking the next field of
the current token, parsing the current chunk if needed, before deciding
if the end of the stream is reached.

Fixes bzick#44
NextToken() also assumed that the next token was always parsed, which is
not the case. Add a new ensureParseNext() function that can be used in
both GoNext() and NextToken(), and that makes sure the next token is
parsed if the end of the stream has not been reached yet.
@n-peugnet
n-peugnet force-pushed the issue-44-long-first-token branch from fda5579 to 2a49f87 Compare July 15, 2026 21:10
@n-peugnet n-peugnet changed the title Fix parsing stream with first token size > parser's chunk size-4 Fix parsing stream with first token size > parser's chunk size Jul 15, 2026
@n-peugnet n-peugnet changed the title Fix parsing stream with first token size > parser's chunk size Fix parsing stream with first token size >= parser's chunk size Jul 15, 2026
@bzick
bzick merged commit 9c01dd5 into bzick:master Jul 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stops parsing after first token if its size is very close to the size of the parser's buffer

2 participants