Skip to content

Commit dd67608

Browse files
ChALkeRjasnell
authored andcommitted
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: nodejs-private/node-private#30 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
1 parent 2011f2c commit dd67608

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

lib/buffer.js

+15-20
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();
@@ -154,15 +157,13 @@ Buffer.alloc = function(size, fill, encoding) {
154157
return createBuffer(size);
155158
if (fill !== undefined) {
156159
// Since we are filling anyway, don't zero fill initially.
157-
flags[kNoZeroFill] = 1;
158160
// Only pay attention to encoding if it's a string. This
159161
// prevents accidentally sending in a number that would
160162
// be interpretted as a start offset.
161163
return typeof encoding === 'string' ?
162-
createBuffer(size).fill(fill, encoding) :
163-
createBuffer(size).fill(fill);
164+
createBuffer(size, true).fill(fill, encoding) :
165+
createBuffer(size, true).fill(fill);
164166
}
165-
flags[kNoZeroFill] = 0;
166167
return createBuffer(size);
167168
};
168169

@@ -182,19 +183,15 @@ Buffer.allocUnsafe = function(size) {
182183
**/
183184
Buffer.allocUnsafeSlow = function(size) {
184185
assertSize(size);
185-
if (size > 0)
186-
flags[kNoZeroFill] = 1;
187-
return createBuffer(size);
186+
return createBuffer(size, true);
188187
};
189188

190189
// If --zero-fill-buffers command line argument is set, a zero-filled
191190
// buffer is returned.
192191
function SlowBuffer(length) {
193192
if (+length != length)
194193
length = 0;
195-
if (length > 0)
196-
flags[kNoZeroFill] = 1;
197-
return createBuffer(+length);
194+
return createBuffer(+length, true);
198195
}
199196

200197
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@@ -216,9 +213,7 @@ function allocate(size) {
216213
// Even though this is checked above, the conditional is a safety net and
217214
// sanity check to prevent any subsequent typed array allocation from not
218215
// being zero filled.
219-
if (size > 0)
220-
flags[kNoZeroFill] = 1;
221-
return createBuffer(size);
216+
return createBuffer(size, true);
222217
}
223218
}
224219

0 commit comments

Comments
 (0)