| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a3c0014 commit 5e3b4d6
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -541,8 +541,11 @@ function write(fd, buffer, offset, length, position, callback) { | |||
| 541 | 541 | ||
| 542 | 542 | if (isArrayBufferView(buffer)) { | |
| 543 | 543 | callback = maybeCallback(callback || position || length || offset); | |
| 544 | - if (typeof offset !== 'number') | ||
| 544 | + if (offset == null || typeof offset === 'function') { | ||
| 545 | 545 | offset = 0; | |
| 546 | + } else { | ||
| 547 | + validateSafeInteger(offset, 'offset'); | ||
| 548 | + } | ||
| 546 | 549 | if (typeof length !== 'number') | |
| 547 | 550 | length = buffer.length - offset; | |
| 548 | 551 | if (typeof position !== 'number') | |
@@ -580,8 +583,11 @@ function writeSync(fd, buffer, offset, length, position) { | |||
| 580 | 583 | if (isArrayBufferView(buffer)) { | |
| 581 | 584 | if (position === undefined) | |
| 582 | 585 | position = null; | |
| 583 | - if (typeof offset !== 'number') | ||
| 586 | + if (offset == null) { | ||
| 584 | 587 | offset = 0; | |
| 588 | + } else { | ||
| 589 | + validateSafeInteger(offset, 'offset'); | ||
| 590 | + } | ||
| 585 | 591 | if (typeof length !== 'number') | |
| 586 | 592 | length = buffer.byteLength - offset; | |
| 587 | 593 | validateOffsetLengthWrite(offset, length, buffer.byteLength); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -240,8 +240,11 @@ async function write(handle, buffer, offset, length, position) { | |||
| 240 | 240 | return { bytesWritten: 0, buffer }; | |
| 241 | 241 | ||
| 242 | 242 | if (isUint8Array(buffer)) { | |
| 243 | - if (typeof offset !== 'number') | ||
| 243 | + if (offset == null) { | ||
| 244 | 244 | offset = 0; | |
| 245 | + } else { | ||
| 246 | + validateSafeInteger(offset, 'offset'); | ||
| 247 | + } | ||
| 245 | 248 | if (typeof length !== 'number') | |
| 246 | 249 | length = buffer.length - offset; | |
| 247 | 250 | if (typeof position !== 'number') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { Object, Reflect } = primordials; | |
| 4 | 4 | ||
| 5 | - const { Buffer, kMaxLength } = require('buffer'); | ||
| 5 | + const { Buffer } = require('buffer'); | ||
| 6 | 6 | const { | |
| 7 | 7 | codes: { | |
| 8 | 8 | ERR_FS_INVALID_SYMLINK_TYPE, | |
@@ -476,9 +476,8 @@ const validateOffsetLengthWrite = hideStackFrames( | |||
| 476 | 476 | throw new ERR_OUT_OF_RANGE('offset', `<= ${byteLength}`, offset); | |
| 477 | 477 | } | |
| 478 | 478 | ||
| 479 | - const max = byteLength > kMaxLength ? kMaxLength : byteLength; | ||
| 480 | - if (length > max - offset) { | ||
| 481 | - throw new ERR_OUT_OF_RANGE('length', `<= ${max - offset}`, length); | ||
| 479 | + if (length > byteLength - offset) { | ||
| 480 | + throw new ERR_OUT_OF_RANGE('length', `<= ${byteLength - offset}`, length); | ||
| 482 | 481 | } | |
| 483 | 482 | } | |
| 484 | 483 | ); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -88,7 +88,7 @@ constexpr char kPathSeparator = '/'; | |||
| 88 | 88 | const char* const kPathSeparator = "\\/"; | |
| 89 | 89 | #endif | |
| 90 | 90 | ||
| 91 | - #define GET_OFFSET(a) ((a)->IsNumber() ? (a).As<Integer>()->Value() : -1) | ||
| 91 | + #define GET_OFFSET(a) (IsSafeJsInt(a) ? (a).As<Integer>()->Value() : -1) | ||
| 92 | 92 | #define TRACE_NAME(name) "fs.sync." #name | |
| 93 | 93 | #define GET_TRACE_ENABLED \ | |
| 94 | 94 | (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \ | |
@@ -1669,9 +1669,11 @@ static void WriteBuffer(const FunctionCallbackInfo<Value>& args) { | |||
| 1669 | 1669 | char* buffer_data = Buffer::Data(buffer_obj); | |
| 1670 | 1670 | size_t buffer_length = Buffer::Length(buffer_obj); | |
| 1671 | 1671 | ||
| 1672 | - CHECK(args[2]->IsInt32()); | ||
| 1673 | - const size_t off = static_cast<size_t>(args[2].As<Int32>()->Value()); | ||
| 1674 | - CHECK_LE(off, buffer_length); | ||
| 1672 | + CHECK(IsSafeJsInt(args[2])); | ||
| 1673 | + const int64_t off_64 = args[2].As<Integer>()->Value(); | ||
| 1674 | + CHECK_GE(off_64, 0); | ||
| 1675 | + CHECK_LE(static_cast<uint64_t>(off_64), buffer_length); | ||
| 1676 | + const size_t off = static_cast<size_t>(off_64); | ||
| 1675 | 1677 | ||
| 1676 | 1678 | CHECK(args[3]->IsInt32()); | |
| 1677 | 1679 | const size_t len = static_cast<size_t>(args[3].As<Int32>()->Value()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,22 +22,6 @@ const { kMaxLength } = require('buffer'); | |||
| 22 | 22 | ); | |
| 23 | 23 | } | |
| 24 | 24 | ||
| 25 | - // RangeError when byteLength > kMaxLength, and length > kMaxLength - offset . | ||
| 26 | - { | ||
| 27 | - const offset = kMaxLength; | ||
| 28 | - const length = 100; | ||
| 29 | - const byteLength = kMaxLength + 1; | ||
| 30 | - common.expectsError( | ||
| 31 | - () => validateOffsetLengthWrite(offset, length, byteLength), | ||
| 32 | - { | ||
| 33 | - code: 'ERR_OUT_OF_RANGE', | ||
| 34 | - type: RangeError, | ||
| 35 | - message: 'The value of "length" is out of range. ' + | ||
| 36 | - `It must be <= ${kMaxLength - offset}. Received ${length}` | ||
| 37 | - } | ||
| 38 | - ); | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | 25 | // RangeError when byteLength < kMaxLength, and length > byteLength - offset . | |
| 42 | 26 | { | |
| 43 | 27 | const offset = kMaxLength - 150; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -148,3 +148,27 @@ tmpdir.refresh(); | |||
| 148 | 148 | fs.write(fd, Uint8Array.from(expected), cb); | |
| 149 | 149 | })); | |
| 150 | 150 | } | |
| 151 | + | ||
| 152 | + // fs.write with invalid offset type | ||
| 153 | + { | ||
| 154 | + const filename = path.join(tmpdir.path, 'write7.txt'); | ||
| 155 | + fs.open(filename, 'w', 0o644, common.mustCall((err, fd) => { | ||
| 156 | + assert.ifError(err); | ||
| 157 | + | ||
| 158 | + assert.throws(() => { | ||
| 159 | + fs.write(fd, | ||
| 160 | + Buffer.from('abcd'), | ||
| 161 | + NaN, | ||
| 162 | + expected.length, | ||
| 163 | + 0, | ||
| 164 | + common.mustNotCall()); | ||
| 165 | + }, { | ||
| 166 | + code: 'ERR_OUT_OF_RANGE', | ||
| 167 | + name: 'RangeError', | ||
| 168 | + message: 'The value of "offset" is out of range. ' + | ||
| 169 | + 'It must be an integer. Received NaN' | ||
| 170 | + }); | ||
| 171 | + | ||
| 172 | + fs.closeSync(fd); | ||
| 173 | + })); | ||
| 174 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments