From 47e87859e279c9c5cd45c95caede137d005282b5 Mon Sep 17 00:00:00 2001 From: goingforstudying-ctrl Date: Mon, 1 Jun 2026 12:54:58 +0000 Subject: [PATCH 1/2] fs: normalize encoding before utf8 fast path check The fs.readFileSync and fs.writeFileSync fast paths for utf8 encoding only checked for 'utf8' and 'utf-8', but normalizeEncoding also accepts 'UTF8' and 'UTF-8'. This meant that using those encodings would bypass the fast path and go through the slower generic path. Fix by normalizing the encoding before checking if it's utf8. Fixes: https://github.com/nodejs/node/issues/49888 --- lib/fs.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 043e29211a1580..8247f9b8911a84 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -94,6 +94,7 @@ const { SideEffectFreeRegExpPrototypeExec, defineLazyProperties, isWindows, + normalizeEncoding, isMacOS, } = require('internal/util'); const { @@ -489,7 +490,8 @@ function readFileSync(path, options) { } options = getOptions(options, { flag: 'r' }); - if (options.encoding === 'utf8' || options.encoding === 'utf-8') { + const encoding = normalizeEncoding(options.encoding) || options.encoding; + if (encoding === 'utf8') { if (!isInt32(path)) { path = getValidatedPath(path); } @@ -2845,7 +2847,8 @@ function writeFileSync(path, data, options) { const flag = options.flag || 'w'; // C++ fast path for string data and UTF8 encoding - if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) { + const encoding = normalizeEncoding(options.encoding) || options.encoding; + if (typeof data === 'string' && encoding === 'utf8') { if (!isInt32(path)) { path = getValidatedPath(path); } From 24b8fef2fa1fc571de002b3d4aaa045f4f578953 Mon Sep 17 00:00:00 2001 From: goingforstudying-ctrl Date: Wed, 3 Jun 2026 17:11:54 -0400 Subject: [PATCH 2/2] fix: add null check for encoding before normalizeEncoding Address review feedback from mertcanaltin to handle cases where options.encoding is null/undefined. Only use the utf8 fast path when a real utf8 encoding is provided. --- lib/fs.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 8247f9b8911a84..d9677c0fd41713 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -490,8 +490,7 @@ function readFileSync(path, options) { } options = getOptions(options, { flag: 'r' }); - const encoding = normalizeEncoding(options.encoding) || options.encoding; - if (encoding === 'utf8') { + if (options.encoding && normalizeEncoding(options.encoding) === 'utf8') { if (!isInt32(path)) { path = getValidatedPath(path); } @@ -2847,8 +2846,7 @@ function writeFileSync(path, data, options) { const flag = options.flag || 'w'; // C++ fast path for string data and UTF8 encoding - const encoding = normalizeEncoding(options.encoding) || options.encoding; - if (typeof data === 'string' && encoding === 'utf8') { + if (typeof data === 'string' && options.encoding && normalizeEncoding(options.encoding) === 'utf8') { if (!isInt32(path)) { path = getValidatedPath(path); }