Skip to content

buffer: remove unreachable overflow check in atob#60161

Open
haramj wants to merge 6 commits into
nodejs:mainfrom
haramj:haramjeong-patch-251008
Open

buffer: remove unreachable overflow check in atob#60161
haramj wants to merge 6 commits into
nodejs:mainfrom
haramj:haramjeong-patch-251008

Conversation

@haramj
Copy link
Copy Markdown
Member

@haramj haramj commented Oct 8, 2025

This commit resolves a TODO comment in lib/buffer.js by changing the error thrown by atob() during a potential overflow condition.

Modified lib/buffer.js to instantiate and throw new ERR_INVALID_ARG_VALUE('input', result, 'The string to be decoded is too long.'); for the overflow case (result === -3).

This change makes the atob implementation more robust and developer-friendly.
@anonrig

->

The case for a result of -3 was intended to handle a potential
buffer overflow error. However, the underlying C++ implementation using
simdutf::base64_to_binary does not perform bounds-checking and therefore
can never return an OUTPUT_BUFFER_TOO_SMALL error.

This makes the case -3: block unreachable code. This commit removes
the dead code for correctness and clarity.

@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. needs-ci PRs that need a full CI run. labels Oct 8, 2025
@Renegade334
Copy link
Copy Markdown
Member

simdutf::base64_to_binary can never return OUTPUT_BUFFER_TOO_SMALL, as it doesn't perform bounds-checking of its target. This should just be removed in favour of an assertion in case of an unrecognised error.

@haramj
Copy link
Copy Markdown
Member Author

haramj commented Oct 12, 2025

simdutf::base64_to_binary can never return OUTPUT_BUFFER_TOO_SMALL, as it doesn't perform bounds-checking of its target. This should just be removed in favour of an assertion in case of an unrecognised error.
@Renegade334 I've pushed a new commit. Please let me know if any other changes are needed.

@haramj haramj changed the title buffer: throw RangeError on atob overflow buffer: remove unreachable overflow check in atob Oct 12, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.57%. Comparing base (b13f24c) to head (f5c78ba).
⚠️ Report is 34 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #60161      +/-   ##
==========================================
+ Coverage   88.53%   88.57%   +0.03%     
==========================================
  Files         703      704       +1     
  Lines      207997   208125     +128     
  Branches    40015    40012       -3     
==========================================
+ Hits       184150   184338     +188     
+ Misses      15864    15815      -49     
+ Partials     7983     7972      -11     
Files with missing lines Coverage Δ
lib/buffer.js 100.00% <100.00%> (ø)

... and 51 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread lib/buffer.js
@haramj
Copy link
Copy Markdown
Member Author

haramj commented Jun 5, 2026

@nodejs/buffer please take a look at this PR when you have some time?

Copy link
Copy Markdown
Member

@Renegade334 Renegade334 left a comment

Choose a reason for hiding this comment

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

I think the comment at the corresponding location in the C++ source should be changed to match:

node/src/node_buffer.cc

Lines 1470 to 1477 in be7ea27

// Default value is: "possible overflow"
int32_t error_code = -3;
if (result.error == simdutf::error_code::INVALID_BASE64_CHARACTER) {
error_code = -2;
} else if (result.error == simdutf::error_code::BASE64_INPUT_REMAINDER) {
error_code = -1;
}

Just needs to comment that if the error code case is not recognised, then the function will return the "unrecognised error" value -3 to JS.

@haramj
Copy link
Copy Markdown
Member Author

haramj commented Jun 6, 2026

I think the comment at the corresponding location in the C++ source should be changed to match:

node/src/node_buffer.cc

Lines 1470 to 1477 in be7ea27

// Default value is: "possible overflow"
int32_t error_code = -3;
if (result.error == simdutf::error_code::INVALID_BASE64_CHARACTER) {
error_code = -2;
} else if (result.error == simdutf::error_code::BASE64_INPUT_REMAINDER) {
error_code = -1;
}

Just needs to comment that if the error code case is not recognised, then the function will return the "unrecognised error" value -3 to JS.

Thanks, updated the C++ comments to describe -3 as the unrecognized simdutf error value returned to JS.

haramj and others added 6 commits June 6, 2026 20:25
Signed-off-by: haramjeong <04harams77@gmail.com>
Signed-off-by: haramjeong <04harams77@gmail.com>
Signed-off-by: haramjeong <04harams77@gmail.com>
Signed-off-by: haramjeong <04harams77@gmail.com>
Signed-off-by: haramjeong <04harams77@gmail.com>
Signed-off-by: haramjeong <04harams77@gmail.com>
@haramj haramj force-pushed the haramjeong-patch-251008 branch from 0c07ec6 to ca418cc Compare June 6, 2026 11:26
@haramj
Copy link
Copy Markdown
Member Author

haramj commented Jun 6, 2026

@anonrig Could you also take a look since this addresses the atob() TODO? Thanks!

@Renegade334 Renegade334 added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Jun 6, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 6, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@haramj haramj added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Jun 7, 2026
@haramj
Copy link
Copy Markdown
Member Author

haramj commented Jun 7, 2026

It looks like the Jenkins failures are due to flaky tests rather than issues introduced by this PR.
Could someone please re-run the CI? thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

buffer Issues and PRs related to the buffer subsystem. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants