Skip to content

buffer: isUtf8 and isAscii accept detached TypedArray views #64503

Description

@Archkon

Version

latest main branch

Platform

7.1.2-arch3-1

Subsystem

buffer

What steps will reproduce the bug?

const { isUtf8, isAscii } = require('node:buffer');

const view = new Uint8Array([0xff]);
structuredClone(view.buffer, { transfer: [view.buffer] });

console.log(isUtf8(view));
console.log(isAscii(view));

How often does it reproduce? Is there a required condition?

ArrayBufferViewContents reads the view's zero byte length but does not record the detached state of its backing ArrayBuffer.

What is the expected behavior? Why is that the expected behavior?

Both functions should throw ERR_INVALID_STATE, consistent with passing a detached ArrayBuffer directly.

What do you see instead?

Both functions return true.

Additional information

Activity

  1. jasnell commented on Jul 14, 2026

    @jasnell
    Member

    It's not obvious this is a bug. In both cases, the detached ArrayBuffer just comes back as zero-length. These APIs handle zero-length just fine (e.g. isUtf8(new Uint8Array(0))).

    I do see that the docs say "Throws if the input is a detached array buffer" but I kind of view that as a doc error rather than an implementation error.

  2. Archkon commented on Jul 14, 2026

    @Archkon
    Author
  3. Renegade334 commented on Jul 14, 2026

    @Renegade334
    Member

    We also only check the detached_ property in these two methods AFAICT, all other uses of ABVC in the Buffer API just treat detached buffers as zero-length, including other string-encoding operations like Buffer.prototype.toString(). I'm ±0 on whether we should be erroring with detached backing ArrayBuffers, but it should be consistent imo

  4. Archkon commented on Jul 14, 2026

    @Archkon
    Author
  5. Archkon commented on Jul 14, 2026

    @Archkon
    Author
  6. jasnell commented on Jul 15, 2026

    @jasnell
    Member

    Ok, the implementation behavior is a bit inconsistent. I would suggest that the final isUtf8(transferedArrayBuffer) should just return true, being treated as a zero-length.

  7. Archkon commented on Jul 15, 2026

    @Archkon
    Author
  8. added
    bufferIssues and PRs related to the buffer subsystem.
    on Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions