Skip to content

Commit 35f06df

Browse files
committed
buffer: safeguard against accidental kNoZeroFill
This makes sure that `kNoZeroFill` flag is not accidentally set by moving the all the flag operations directly inside `createBuffer()`. It safeguards against logical errors like #6006. This also ensures that `kNoZeroFill` flag is always restored to 0 using a try-finally block, as it could be not restored to 0 in cases of failed or zero-size `Uint8Array` allocation. It safeguards against errors like #2930. It also makes the `size > 0` check not needed there. PR-URL: https://github.com/nodejs/node-private/pull/35 Reviewed-By: James M Snell <[email protected]>
1 parent bec5d50 commit 35f06df

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

lib/buffer.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,20 @@ Buffer.prototype.swap32 = function swap32() {
6262
const flags = bindingObj.flags;
6363
const kNoZeroFill = 0;
6464

65-
function createBuffer(size) {
66-
const ui8 = new Uint8Array(size);
67-
Object.setPrototypeOf(ui8, Buffer.prototype);
68-
return ui8;
65+
function createBuffer(size, noZeroFill) {
66+
flags[kNoZeroFill] = noZeroFill ? 1 : 0;
67+
try {
68+
const ui8 = new Uint8Array(size);
69+
Object.setPrototypeOf(ui8, Buffer.prototype);
70+
return ui8;
71+
} finally {
72+
flags[kNoZeroFill] = 0;
73+
}
6974
}
7075

7176
function createPool() {
7277
poolSize = Buffer.poolSize;
73-
if (poolSize > 0)
74-
flags[kNoZeroFill] = 1;
75-
allocPool = createBuffer(poolSize);
78+
allocPool = createBuffer(poolSize, true);
7679
poolOffset = 0;
7780
}
7881
createPool();
@@ -129,15 +132,13 @@ Buffer.alloc = function(size, fill, encoding) {
129132
return createBuffer(size);
130133
if (fill !== undefined) {
131134
// Since we are filling anyway, don't zero fill initially.
132-
flags[kNoZeroFill] = 1;
133135
// Only pay attention to encoding if it's a string. This
134136
// prevents accidentally sending in a number that would
135137
// be interpretted as a start offset.
136138
return typeof encoding === 'string' ?
137-
createBuffer(size).fill(fill, encoding) :
138-
createBuffer(size).fill(fill);
139+
createBuffer(size, true).fill(fill, encoding) :
140+
createBuffer(size, true).fill(fill);
139141
}
140-
flags[kNoZeroFill] = 0;
141142
return createBuffer(size);
142143
};
143144

@@ -154,9 +155,7 @@ Buffer.allocUnsafe = function(size) {
154155
function SlowBuffer(length) {
155156
if (+length != length)
156157
length = 0;
157-
if (length > 0)
158-
flags[kNoZeroFill] = 1;
159-
return createBuffer(+length);
158+
return createBuffer(+length, true);
160159
}
161160

162161
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@@ -178,9 +177,7 @@ function allocate(size) {
178177
// Even though this is checked above, the conditional is a safety net and
179178
// sanity check to prevent any subsequent typed array allocation from not
180179
// being zero filled.
181-
if (size > 0)
182-
flags[kNoZeroFill] = 1;
183-
return createBuffer(size);
180+
return createBuffer(size, true);
184181
}
185182
}
186183

0 commit comments

Comments
 (0)