Skip to content

TypedArray: coerce length argument once on buffer construction - #1123

Merged
xeioex merged 1 commit into
nginx:masterfrom
basavaraj-sm05:typed-array-length-coerce-once
Sep 15, 2026
Merged

xeioex merged 1 commit into
nginx:masterfrom
basavaraj-sm05:typed-array-length-coerce-once

Conversation

@basavaraj-sm05

Copy link
Copy Markdown
Contributor

Proposed changes

Constructing a typed array from an ArrayBuffer with an explicit length, such as new Int8Array(buffer, byteOffset, length), runs the length through njs_value_to_index() twice in njs_typed_array_alloc(): once before the detached-buffer check and once after it, keeping only the second result. Because ToIndex evaluates the argument's valueOf(), the length object's valueOf() fires twice, which is observable from script and departs both from the specification, where ToIndex runs once, and from the DataView constructor, which converts its length a single time. The duplicate conversion also runs after the detached-buffer check, so a valueOf() that detaches the buffer while the length is being coerced slips past the check and the constructor returns a zero-length view over the detached buffer instead of throwing. The fix removes the second conversion so the length is coerced once, before the check. A valueOf() that detaches mid-coercion is now reported, and valid construction is unchanged. I added a regression test asserting the length argument's valueOf() is invoked exactly once.

Checklist

Before creating a PR, run through this checklist and mark each as complete:

  • I have read the CONTRIBUTING document
  • If applicable, I have added tests that prove my fix is effective or that my feature works
  • If applicable, I have checked that any relevant tests pass after adding my changes

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

🎉 Thank you for your contribution! It appears you have not yet signed the F5 Contributor License Agreement (CLA), which is required for your changes to be incorporated into an F5 Open Source Software (OSS) project. Please kindly read the F5 CLA and reply on a new comment with the following text to agree:


I have hereby read the F5 CLA and agree to its terms


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@xeioex
xeioex force-pushed the typed-array-length-coerce-once branch from b2bf2dd to b5b698f Compare September 15, 2026 02:11
Previously, njs_typed_array_alloc() passed the length argument through
njs_value_to_index() twice, before and after the detached-buffer check,
and kept only the second result.  The argument's valueOf() was invoked
twice, which is observable.

The fix is to remove the second conversion.
@xeioex
xeioex force-pushed the typed-array-length-coerce-once branch from b5b698f to ecfabde Compare September 15, 2026 04:59

@xeioex xeioex left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the patch.

I simplified the commit log because it had factual inaccuracies.

Otherwise LGTM.

Please do The F5 CLA, so I will be able to merge.

@github-project-automation github-project-automation Bot moved this from New to In Review in NGINX OSS Unified Workspace Sep 15, 2026
@xeioex
xeioex merged commit fd8441b into nginx:master Sep 15, 2026
2 of 3 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in NGINX OSS Unified Workspace Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants