nodejs / nodejs/node

buffer.toString() fails when buffer.length >= 32 and null-terminated on big-endian

Open
#64,807 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

linux s390x
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

v24.18.0, v26.5.0

Platform
Linux zelenka 6.12.96+deb13-s390x #1 SMP Debian 6.12.96-1 (2026-07-20) s390x GNU/Linux
Subsystem

StringBytes

What steps will reproduce the bug?

Run these commands

node -p 'const b=Buffer.alloc(32); b.write("\u3000",0,"utf8");b.toString().charCodeAt(0);'
node -p 'Buffer.from("\u3000").toString().charCodeAt(0);'
How often does it reproduce? Is there a required condition?

Always reproduced on big-endian.

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

both commands should return 12288

What do you see instead?

On big-endian architecture (e.g. s390x) the first call returns 0 and the last 12288.

Additional information

Tested with the executables provided by nodejs.org.
v22.23.1 is not affected.
Also, this gives the correct result on big-endian:

node -p 'const b=Buffer.alloc(31); b.write("\u3000",0,"utf8");b.toString().charCodeAt(0);'

So the suspected commits are the ones adding the special cases for buflen >= 32 here:
https://github.com/nodejs/node/blob/68130800586634f17812f3b0837d869a36725e98/src/string_bytes.cc#L751
and here
https://github.com/nodejs/node/blob/68130800586634f17812f3b0837d869a36725e98/src/string_bytes.cc#L625

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the two reproduction commands on a big-endian system and compare them with the 31-byte case. Then inspect src/string_bytes.cc around the referenced lines 625 and 751, focusing on the special handling for buflen >= 32. Done means both commands return 12288 on big-endian without regressing the existing 31-byte behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, nodejs
Domain
backend, operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.