Skip to content

x.json2: fix decode_number not initializing value to zero#25675

Merged
spytheman merged 2 commits into
vlang:masterfrom
chethanyadav456:fix-issue-25641-u16-json-decode
Nov 6, 2025
Merged

x.json2: fix decode_number not initializing value to zero#25675
spytheman merged 2 commits into
vlang:masterfrom
chethanyadav456:fix-issue-25641-u16-json-decode

Conversation

@chethanyadav456

Copy link
Copy Markdown
Contributor

Fixes issue #25641 where u16 and other integer types were incorrectly decoded from JSON. The bug occurred because decode_number accumulated digits without first initializing the target value to zero.

When decoding struct fields, if the struct had a pre-existing value, the decoder would append digits to that value instead of replacing it. For example, decoding 25 into a field that already contained 25 would result in 2525.

The fix adds explicit zero initialization at the start of both positive and negative number decoding paths before accumulating digits.

Changes:

  • Initialize *val = 0 before processing positive integers
  • Initialize *val = 0 before processing negative integers
  • Add test case to verify the fix and prevent regression
Screenshot 2025-11-05 at 9 09 55 PM

Fixes issue vlang#25641 where u16 and other integer types were incorrectly
decoded from JSON. The bug occurred because decode_number accumulated
digits without first initializing the target value to zero.

When decoding struct fields, if the struct had a pre-existing value,
the decoder would append digits to that value instead of replacing it.
For example, decoding 25 into a field that already contained 25 would
result in 2525.

The fix adds explicit zero initialization at the start of both positive
and negative number decoding paths before accumulating digits.

Changes:
- Initialize *val = 0 before processing positive integers
- Initialize *val = 0 before processing negative integers
- Add test case to verify the fix and prevent regression
@chethanyadav456 chethanyadav456 force-pushed the fix-issue-25641-u16-json-decode branch from ae2886f to 140b0b7 Compare November 5, 2025 17:05
@chethanyadav456

Copy link
Copy Markdown
Contributor Author

Fixed the formatting issue. The test file has been reformatted with v fmt and force-pushed. The CI should pass now.

Comment thread vlib/x/json2/decode.v Outdated
@spytheman spytheman changed the title json2: fix decode_number not initializing value to zero x.json2: fix decode_number not initializing value to zero Nov 6, 2025
@spytheman

Copy link
Copy Markdown
Contributor

Thank you @chethanyadav456 🙇🏻 .

@spytheman spytheman merged commit a8b0e1d into vlang:master Nov 6, 2025
79 of 80 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.

2 participants