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

bytes/equals incorrectly handles subarrays #3603

Closed
Tracked by #4629
tjjfvi opened this issue Sep 1, 2023 · 4 comments · Fixed by #4630
Closed
Tracked by #4629

bytes/equals incorrectly handles subarrays #3603

tjjfvi opened this issue Sep 1, 2023 · 4 comments · Fixed by #4630
Labels
bug Something isn't working

Comments

@tjjfvi
Copy link
Contributor

tjjfvi commented Sep 1, 2023

import { assert, assertEquals } from "jsr:@std/assert"
import { equals } from "jsr:@std/bytes"

const a = new Uint8Array(1001).subarray(1)
const b = new Uint8Array(1000)

a[0] = 123
b[0] = 123

assertEquals(a, b) // ok
assert(equals(a, b)) // error; false negative
import { assert, assertNotEquals } from "jsr:@std/assert"
import { equals } from "jsr:@std/bytes"

const a = new Uint8Array(1001).subarray(1)
const b = new Uint8Array(1000)

a[999] = 123

assertNotEquals(a, b) // ok
assert(!equals(a, b)) // error; false positive

The equals32Bit implementation does not account for subarrays.

@kt3k kt3k added the bug Something isn't working label Sep 4, 2023
@kt3k
Copy link
Member

kt3k commented Sep 4, 2023

Maybe equals32Bit is only usable when both a.byteOffset and b.byteOffset are multiple of 4.

cc @Aplet123

@Aplet123
Copy link
Contributor

Aplet123 commented Sep 4, 2023

I think it would take some benchmarking to see if it's better to do the 8 bit approach when byteOffset isn't a multiple of 4 vs constructing a new Uint8Array from the sliced one so there's no longer any byte offset.

@tjjfvi
Copy link
Contributor Author

tjjfvi commented Sep 4, 2023

equals32Bit could also be usable when a.byteOffset % 4 === b.byteOffset % 4 with some small tweaks.

@iuioiua
Copy link
Collaborator

iuioiua commented Apr 24, 2024

Performance fixes applied in #4635.

@iuioiua iuioiua closed this as completed Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants