Skip to content

Commit 7f98286

Browse files
author
wdsmini
committed
fs: accept all valid utf8 values in fast paths
Accept UTF8 and UTF-8 (uppercase) in fs.readFileSync and fs.writeFileSync fast paths, in addition to utf8 and utf-8. Fixes: #49888
1 parent 9fc6b64 commit 7f98286

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

lib/fs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ function tryReadSync(fd, isUserFd, buffer, pos, len) {
429429
function readFileSync(path, options) {
430430
options = getOptions(options, { flag: 'r' });
431431

432-
if (options.encoding === 'utf8' || options.encoding === 'utf-8') {
432+
if (options.encoding === 'utf8' || options.encoding === 'utf-8' ||
433+
options.encoding === 'UTF8' || options.encoding === 'UTF-8') {
433434
if (!isInt32(path)) {
434435
path = getValidatedPath(path);
435436
}
@@ -2390,7 +2391,8 @@ function writeFileSync(path, data, options) {
23902391
const flag = options.flag || 'w';
23912392

23922393
// C++ fast path for string data and UTF8 encoding
2393-
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
2394+
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8' ||
2395+
options.encoding === 'UTF8' || options.encoding === 'UTF-8')) {
23942396
if (!isInt32(path)) {
23952397
path = getValidatedPath(path);
23962398
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
// This test ensures that fs.readFileSync and fs.writeFileSync
4+
// accept all valid UTF8 encoding variants (utf8, utf-8, UTF8, UTF-8).
5+
// Refs: https://github.com/nodejs/node/issues/49888
6+
7+
const common = require('../common');
8+
const tmpdir = require('../../test/common/tmpdir');
9+
const assert = require('assert');
10+
const fs = require('fs');
11+
const path = require('path');
12+
13+
tmpdir.refresh();
14+
15+
const testContent = 'Hello, World! 你好,世界!';
16+
const encodings = ['utf8', 'utf-8', 'UTF8', 'UTF-8'];
17+
18+
// Test writeFileSync and readFileSync with different UTF8 variants
19+
for (const encoding of encodings) {
20+
const testFile = tmpdir.resolve(`test_utf8_fast_path_${encoding}.txt`);
21+
22+
// Test writeFileSync
23+
fs.writeFileSync(testFile, testContent, { encoding });
24+
25+
// Test readFileSync
26+
const result = fs.readFileSync(testFile, { encoding });
27+
assert.strictEqual(result, testContent,
28+
`readFileSync should return correct content for encoding "${encoding}"`);
29+
}
30+
31+
// Test with file descriptor
32+
for (const encoding of encodings) {
33+
const testFile = tmpdir.resolve(`test_utf8_fast_path_fd_${encoding}.txt`);
34+
35+
// Write with fd
36+
const fdWrite = fs.openSync(testFile, 'w');
37+
fs.writeFileSync(fdWrite, testContent, { encoding });
38+
fs.closeSync(fdWrite);
39+
40+
// Read with fd
41+
const fdRead = fs.openSync(testFile, 'r');
42+
const result = fs.readFileSync(fdRead, { encoding });
43+
fs.closeSync(fdRead);
44+
45+
assert.strictEqual(result, testContent,
46+
`readFileSync with fd should return correct content for encoding "${encoding}"`);
47+
}
48+
49+
console.log('All UTF8 fast path casing tests passed!');

0 commit comments

Comments
 (0)