| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 91a4cb7 commit 0bbda5e
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -453,7 +453,12 @@ function read(fd, buffer, offset, length, position, callback) { | |||
| 453 | 453 | validateBuffer(buffer); | |
| 454 | 454 | callback = maybeCallback(callback); | |
| 455 | 455 | ||
| 456 | - offset |= 0; | ||
| 456 | + if (offset == null) { | ||
| 457 | + offset = 0; | ||
| 458 | + } else { | ||
| 459 | + validateSafeInteger(offset, 'offset'); | ||
| 460 | + } | ||
| 461 | + | ||
| 457 | 462 | length |= 0; | |
| 458 | 463 | ||
| 459 | 464 | if (length === 0) { | |
@@ -490,7 +495,12 @@ function readSync(fd, buffer, offset, length, position) { | |||
| 490 | 495 | validateInt32(fd, 'fd', 0); | |
| 491 | 496 | validateBuffer(buffer); | |
| 492 | 497 | ||
| 493 | - offset |= 0; | ||
| 498 | + if (offset == null) { | ||
| 499 | + offset = 0; | ||
| 500 | + } else { | ||
| 501 | + validateSafeInteger(offset, 'offset'); | ||
| 502 | + } | ||
| 503 | + | ||
| 494 | 504 | length |= 0; | |
| 495 | 505 | ||
| 496 | 506 | if (length === 0) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -206,7 +206,12 @@ async function read(handle, buffer, offset, length, position) { | |||
| 206 | 206 | validateFileHandle(handle); | |
| 207 | 207 | validateBuffer(buffer); | |
| 208 | 208 | ||
| 209 | - offset |= 0; | ||
| 209 | + if (offset == null) { | ||
| 210 | + offset = 0; | ||
| 211 | + } else { | ||
| 212 | + validateSafeInteger(offset, 'offset'); | ||
| 213 | + } | ||
| 214 | + | ||
| 210 | 215 | length |= 0; | |
| 211 | 216 | ||
| 212 | 217 | if (length === 0) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1851,7 +1851,7 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) { | |||
| 1851 | 1851 | * | |
| 1852 | 1852 | * 0 fd int32. file descriptor | |
| 1853 | 1853 | * 1 buffer instance of Buffer | |
| 1854 | - * 2 offset int32. offset to start reading into inside buffer | ||
| 1854 | + * 2 offset int64. offset to start reading into inside buffer | ||
| 1855 | 1855 | * 3 length int32. length to read | |
| 1856 | 1856 | * 4 position int64. file position - -1 for current position | |
| 1857 | 1857 | */ | |
@@ -1869,15 +1869,17 @@ static void Read(const FunctionCallbackInfo<Value>& args) { | |||
| 1869 | 1869 | char* buffer_data = Buffer::Data(buffer_obj); | |
| 1870 | 1870 | size_t buffer_length = Buffer::Length(buffer_obj); | |
| 1871 | 1871 | ||
| 1872 | - CHECK(args[2]->IsInt32()); | ||
| 1873 | - const size_t off = static_cast<size_t>(args[2].As<Int32>()->Value()); | ||
| 1874 | - CHECK_LT(off, buffer_length); | ||
| 1872 | + CHECK(IsSafeJsInt(args[2])); | ||
| 1873 | + const int64_t off_64 = args[2].As<Integer>()->Value(); | ||
| 1874 | + CHECK_GE(off_64, 0); | ||
| 1875 | + CHECK_LT(static_cast<uint64_t>(off_64), buffer_length); | ||
| 1876 | + const size_t off = static_cast<size_t>(off_64); | ||
| 1875 | 1877 | ||
| 1876 | 1878 | CHECK(args[3]->IsInt32()); | |
| 1877 | 1879 | const size_t len = static_cast<size_t>(args[3].As<Int32>()->Value()); | |
| 1878 | 1880 | CHECK(Buffer::IsWithinBounds(off, len, buffer_length)); | |
| 1879 | 1881 | ||
| 1880 | - CHECK(args[4]->IsNumber()); | ||
| 1882 | + CHECK(IsSafeJsInt(args[4])); | ||
| 1881 | 1883 | const int64_t pos = args[4].As<Integer>()->Value(); | |
| 1882 | 1884 | ||
| 1883 | 1885 | char* buf = buffer_data + off; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ | |||
| 24 | 24 | ||
| 25 | 25 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| 26 | 26 | ||
| 27 | + #include <cmath> | ||
| 27 | 28 | #include <cstring> | |
| 28 | 29 | #include "util.h" | |
| 29 | 30 | ||
@@ -521,6 +522,17 @@ void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) { | |||
| 521 | 522 | } | |
| 522 | 523 | } | |
| 523 | 524 | ||
| 525 | + // ECMA262 20.1.2.5 | ||
| 526 | + inline bool IsSafeJsInt(v8::Local<v8::Value> v) { | ||
| 527 | + if (!v->IsNumber()) return false; | ||
| 528 | + double v_d = v.As<v8::Number>()->Value(); | ||
| 529 | + if (std::isnan(v_d)) return false; | ||
| 530 | + if (std::isinf(v_d)) return false; | ||
| 531 | + if (std::trunc(v_d) != v_d) return false; // not int | ||
| 532 | + if (std::abs(v_d) <= static_cast<double>(kMaxSafeJsInteger)) return true; | ||
| 533 | + return false; | ||
| 534 | + } | ||
| 535 | + | ||
| 524 | 536 | } // namespace node | |
| 525 | 537 | ||
| 526 | 538 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -184,6 +184,11 @@ void DumpBacktrace(FILE* fp); | |||
| 184 | 184 | #define UNREACHABLE(...) \ | |
| 185 | 185 | ERROR_AND_ABORT("Unreachable code reached" __VA_OPT__(": ") __VA_ARGS__) | |
| 186 | 186 | ||
| 187 | + // ECMA262 20.1.2.6 Number.MAX_SAFE_INTEGER (2^53-1) | ||
| 188 | + constexpr int64_t kMaxSafeJsInteger = 9007199254740991; | ||
| 189 | + | ||
| 190 | + inline bool IsSafeJsInt(v8::Local<v8::Value> v); | ||
| 191 | + | ||
| 187 | 192 | // TAILQ-style intrusive list node. | |
| 188 | 193 | template <typename T> | |
| 189 | 194 | class ListNode; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,6 +50,20 @@ assert.throws(() => { | |||
| 50 | 50 | 'Received -1' | |
| 51 | 51 | }); | |
| 52 | 52 | ||
| 53 | + assert.throws(() => { | ||
| 54 | + fs.read(fd, | ||
| 55 | + Buffer.allocUnsafe(expected.length), | ||
| 56 | + NaN, | ||
| 57 | + expected.length, | ||
| 58 | + 0, | ||
| 59 | + common.mustNotCall()); | ||
| 60 | + }, { | ||
| 61 | + code: 'ERR_OUT_OF_RANGE', | ||
| 62 | + name: 'RangeError', | ||
| 63 | + message: 'The value of "offset" is out of range. It must be an integer. ' + | ||
| 64 | + 'Received NaN' | ||
| 65 | + }); | ||
| 66 | + | ||
| 53 | 67 | assert.throws(() => { | |
| 54 | 68 | fs.read(fd, | |
| 55 | 69 | Buffer.allocUnsafe(expected.length), | |
@@ -103,6 +117,19 @@ assert.throws(() => { | |||
| 103 | 117 | 'It must be >= 0 && <= 4. Received -1' | |
| 104 | 118 | }); | |
| 105 | 119 | ||
| 120 | + assert.throws(() => { | ||
| 121 | + fs.readSync(fd, | ||
| 122 | + Buffer.allocUnsafe(expected.length), | ||
| 123 | + NaN, | ||
| 124 | + expected.length, | ||
| 125 | + 0); | ||
| 126 | + }, { | ||
| 127 | + code: 'ERR_OUT_OF_RANGE', | ||
| 128 | + name: 'RangeError', | ||
| 129 | + message: 'The value of "offset" is out of range. It must be an integer. ' + | ||
| 130 | + 'Received NaN' | ||
| 131 | + }); | ||
| 132 | + | ||
| 106 | 133 | assert.throws(() => { | |
| 107 | 134 | fs.readSync(fd, | |
| 108 | 135 | Buffer.allocUnsafe(expected.length), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments