Skip to content

Commit cc729e8

Browse files
deepview-autofixclaudeChALkeR
authored
lib: fix evenRound sign for fractional values in (-1, 1)
`evenRound` derived the rounding direction from `MathSign(i)` where `i = integerPart(x)`. For any `x` whose truncated integer part is zero (e.g. `0.7`, `-0.7`), `sign` was `0`, so the non-halfway branch returned `i + sign === 0` instead of rounding away from zero. This produced incorrect `[Clamp]` WebIDL integer conversions for fractional inputs in `(-1, 0) ∪ (0, 1)` that are not exactly `±0.5`, observable e.g. via `new Blob([Uint8Array.of(1,2,3)]).slice(-0.9999999)` returning the whole blob instead of a 1-byte slice. Use `MathSign(x)` so the rounding direction follows the sign of the original value. Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: DeepView Autofix <276251120+deepview-autofix@users.noreply.github.com> Co-Authored-By: Nikita Skovoroda <chalkerx@gmail.com> Signed-off-by: Nikita Skovoroda <chalkerx@gmail.com>
1 parent 14e16db commit cc729e8

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

lib/internal/webidl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function evenRound(x) {
8080
// Convert -0 to +0.
8181
const i = integerPart(x) + 0;
8282
const reminder = MathAbs(x % 1);
83-
const sign = MathSign(i);
83+
const sign = MathSign(x);
8484
if (reminder === 0.5) {
8585
return i % 2 === 0 ? i : i + sign;
8686
}

test/parallel/test-internal-webidl-converttoint.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ assert.strictEqual(evenRound(3.4), 3);
1313
assert.strictEqual(evenRound(4.6), 5);
1414
assert.strictEqual(evenRound(5), 5);
1515
assert.strictEqual(evenRound(6), 6);
16+
// Fractional values in (-1, 0) ∪ (0, 1) whose truncated integer part is 0.
17+
assert.strictEqual(evenRound(0.7), 1);
18+
assert.strictEqual(evenRound(-0.7), -1);
19+
assert.strictEqual(evenRound(0.3), 0);
20+
assert.strictEqual(evenRound(-0.3), 0);
21+
assert.strictEqual(convertToInt('x', -0.9999999, 64, { signed: true, clamp: true }), -1);
22+
assert.strictEqual(convertToInt('x', 0.9999999, 64, { clamp: true }), 1);
1623

1724
// https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
1825
assert.strictEqual(convertToInt('x', 0, 64), 0);

0 commit comments

Comments
 (0)