Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertInteger: can not decode float as int for valid input from chunked reader #476

Closed
mrVanboy opened this issue Jul 18, 2020 · 1 comment · Fixed by #477
Closed

assertInteger: can not decode float as int for valid input from chunked reader #476

mrVanboy opened this issue Jul 18, 2020 · 1 comment · Fixed by #477

Comments

@mrVanboy
Copy link
Contributor

Hi,
we found a bug during unmarshaling large responses from HTTP.Request.Body. The next conditions should be met:

  • Iterator reads data from io.Reader,
  • expected value is 0 (zero)
  • Iterator.tail == Iterator.head + 1
  • Iterator.tail < len(Iterator.buf)
  • value in the buffer after Iterator.tail is presented from the previous read and has '.' character.

Basically, let's assume that Iterator reads data from the reader which returns less data, than the size of the buffer. After a couple of reads, the buffer will have the content like: 0.0, 0.0, 0.
The first item is from the latest read, other items are from the older readers, so have:

buf: []bytes(`0.0, 0.0, 0`)
head: 0 // ---^
tail: 1 // ----^

When the Iterator.ReadInt is called, it reads zero and checks if the value is an integer by calling assertInteger() method, which currently looks like:

func (iter *Iterator) assertInteger() {
	if iter.head < len(iter.buf) && iter.buf[iter.head] == '.' {
		iter.ReportError("assertInteger", "can not decode float as int")
	}
}

The only issue is, that it checks for iter.head < len(iter.buf) instead of iter.head < iter.tail and the assertion is done against the dot . from the previous read.

The test for replicating this behavior will be provided in ongoing PR.

mrVanboy added a commit to kiwicom/jsoniter that referenced this issue Jul 18, 2020
This commit fixes bug in Iterator.assertInteger method if the next
conditions are met:
- Iterator reads data from `io.Reader`,
- expected value is `0` (zero)
- `Iterator.tail == Iterator.head + 1`
- `Iterator.tail < len(Iterator.buf)`
- value in the buffer after `Iterator.tail` is presented from the previous read and has '.' character.

Typical error which user cal see is:
- assertInteger: can not decode float as int, error found in #X byte of ...

Regression test added for checking the correct behaviour.

Fixes json-iterator#476
mrVanboy added a commit to kiwicom/jsoniter that referenced this issue Aug 5, 2020
This commit fixes bug in Iterator.assertInteger method if the next
conditions are met:
- Iterator reads data from `io.Reader`,
- expected value is `0` (zero)
- `Iterator.tail == Iterator.head + 1`
- `Iterator.tail < len(Iterator.buf)`
- value in the buffer after `Iterator.tail` is presented from the previous read and has '.' character.

Typical error which user cal see is:
- assertInteger: can not decode float as int, error found in #X byte of ...

Regression test added for checking the correct behaviour.

Fixes json-iterator#476
AllenX2018 pushed a commit that referenced this issue Aug 6, 2020
…477)

This commit fixes bug in Iterator.assertInteger method if the next
conditions are met:
- Iterator reads data from `io.Reader`,
- expected value is `0` (zero)
- `Iterator.tail == Iterator.head + 1`
- `Iterator.tail < len(Iterator.buf)`
- value in the buffer after `Iterator.tail` is presented from the previous read and has '.' character.

Typical error which user cal see is:
- assertInteger: can not decode float as int, error found in #X byte of ...

Regression test added for checking the correct behaviour.

Fixes #476
@akovalyov
Copy link

@AllenX2018 any chance this will get into the next tag?

zhenzou pushed a commit to zhenzou/jsoniter that referenced this issue Feb 2, 2022
…son-iterator#477)

This commit fixes bug in Iterator.assertInteger method if the next
conditions are met:
- Iterator reads data from `io.Reader`,
- expected value is `0` (zero)
- `Iterator.tail == Iterator.head + 1`
- `Iterator.tail < len(Iterator.buf)`
- value in the buffer after `Iterator.tail` is presented from the previous read and has '.' character.

Typical error which user cal see is:
- assertInteger: can not decode float as int, error found in #X byte of ...

Regression test added for checking the correct behaviour.

Fixes json-iterator#476
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 a pull request may close this issue.

2 participants