GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fs: fix readFileSync utf8 overflow on short reads · nodejs/node@19cae95

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_file.cc
  • test/parallel
    • test-fs-readfilesync-pipe-short-reads.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_file.cc‎

Copy file name to clipboardExpand all lines: src/node_file.cc
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,9 +3525,10 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
35253525
if (static_cast<size_t>(r) < sizeof(buffer)) {
35263526
continue;
35273527
}
3528-
// Switch to the heap buffer.
3528+
// Switch to the heap buffer. A pipe can deliver more than kMinChunk in
3529+
// short reads before the first full one.
35293530
uv_fs_req_cleanup(&req);
3530-
big_cap = kMinChunk;
3531+
big_cap = std::max(kMinChunk, result.size());
35313532
big = UncheckedMalloc<char>(big_cap);
35323533
if (big == nullptr) {
35333534
FS_SYNC_TRACE_END(read);
Collapse file

‎test/parallel/test-fs-readfilesync-pipe-short-reads.js‎

Copy file name to clipboard
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
// fs.readFileSync(fd, 'utf8') collects short reads in a growing string and
3+
// only moves to a heap buffer at the first full 8 KiB read. On a pipe, more
4+
// than 64 KiB (the heap buffer's first size) can arrive in short reads before
5+
// that happens.
6+
// Refs: https://github.com/nodejs/node/issues/66341
7+
const common = require('../common');
8+
9+
if (!common.isLinux)
10+
common.skip('Paces the writes with /proc/<pid>/io.');
11+
12+
const assert = require('assert');
13+
const { spawn } = require('child_process');
14+
const fs = require('fs');
15+
16+
const kShortWrite = 4000;
17+
const kShortWrites = 20;
18+
const kTail = 100_000;
19+
const expected = 'x'.repeat(kShortWrite * kShortWrites) + 'y'.repeat(kTail);
20+
21+
if (process.argv[2] === 'child') {
22+
fs.writeSync(1, 'ready\n');
23+
assert.strictEqual(fs.readFileSync(0, 'utf8'), expected);
24+
return;
25+
}
26+
27+
// Bytes the process has received from read() and similar calls.
28+
function readBytes(pid) {
29+
const io = fs.readFileSync(`/proc/${pid}/io`, 'latin1');
30+
return Number(/^rchar: (\d+)$/m.exec(io)[1]);
31+
}
32+
33+
const child = spawn(process.execPath, [__filename, 'child'], {
34+
stdio: ['pipe', 'pipe', 'inherit'],
35+
});
36+
37+
child.stdout.once('data', common.mustCall(() => {
38+
// Each short write waits until the child, blocked in readFileSync(), has
39+
// read everything sent before it, so no two of them can be merged into a
40+
// full read.
41+
const start = readBytes(child.pid);
42+
let sent = 0;
43+
function writeNext() {
44+
if (readBytes(child.pid) - start < sent) {
45+
setTimeout(writeNext, 1);
46+
} else if (sent < kShortWrite * kShortWrites) {
47+
child.stdin.write('x'.repeat(kShortWrite));
48+
sent += kShortWrite;
49+
setTimeout(writeNext, 1);
50+
} else {
51+
child.stdin.end('y'.repeat(kTail));
52+
}
53+
}
54+
writeNext();
55+
}));
56+
57+
child.on('exit', common.mustCall((code, signal) => {
58+
assert.deepStrictEqual({ code, signal }, { code: 0, signal: null });
59+
}));

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.