| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -515,13 +515,12 @@ function defaultCloseCallback(err) { | |||
| 515 | 515 | * @returns {void} | |
| 516 | 516 | */ | |
| 517 | 517 | function close(fd, callback = defaultCloseCallback) { | |
| 518 | - fd = getValidatedFd(fd); | ||
| 519 | 518 | if (callback !== defaultCloseCallback) | |
| 520 | 519 | callback = makeCallback(callback); | |
| 521 | 520 | ||
| 522 | 521 | const req = new FSReqCallback(); | |
| 523 | 522 | req.oncomplete = callback; | |
| 524 | - binding.close(fd, req); | ||
| 523 | + binding.close(getValidatedFd(fd), req); | ||
| 525 | 524 | } | |
| 526 | 525 | ||
| 527 | 526 | /** | |
@@ -530,9 +529,7 @@ function close(fd, callback = defaultCloseCallback) { | |||
| 530 | 529 | * @returns {void} | |
| 531 | 530 | */ | |
| 532 | 531 | function closeSync(fd) { | |
| 533 | - fd = getValidatedFd(fd); | ||
| 534 | - | ||
| 535 | - binding.close(fd); | ||
| 532 | + binding.close(getValidatedFd(fd)); | ||
| 536 | 533 | } | |
| 537 | 534 | ||
| 538 | 535 | /** | |
@@ -1106,7 +1103,6 @@ function ftruncate(fd, len = 0, callback) { | |||
| 1106 | 1103 | callback = len; | |
| 1107 | 1104 | len = 0; | |
| 1108 | 1105 | } | |
| 1109 | - fd = getValidatedFd(fd); | ||
| 1110 | 1106 | validateInteger(len, 'len'); | |
| 1111 | 1107 | len = MathMax(0, len); | |
| 1112 | 1108 | callback = makeCallback(callback); | |
@@ -1123,7 +1119,6 @@ function ftruncate(fd, len = 0, callback) { | |||
| 1123 | 1119 | * @returns {void} | |
| 1124 | 1120 | */ | |
| 1125 | 1121 | function ftruncateSync(fd, len = 0) { | |
| 1126 | - fd = getValidatedFd(fd); | ||
| 1127 | 1122 | validateInteger(len, 'len'); | |
| 1128 | 1123 | len = MathMax(0, len); | |
| 1129 | 1124 | binding.ftruncate(fd, len); | |
@@ -1275,7 +1270,6 @@ function rmSync(path, options) { | |||
| 1275 | 1270 | * @returns {void} | |
| 1276 | 1271 | */ | |
| 1277 | 1272 | function fdatasync(fd, callback) { | |
| 1278 | - fd = getValidatedFd(fd); | ||
| 1279 | 1273 | const req = new FSReqCallback(); | |
| 1280 | 1274 | req.oncomplete = makeCallback(callback); | |
| 1281 | 1275 | binding.fdatasync(fd, req); | |
@@ -1289,7 +1283,6 @@ function fdatasync(fd, callback) { | |||
| 1289 | 1283 | * @returns {void} | |
| 1290 | 1284 | */ | |
| 1291 | 1285 | function fdatasyncSync(fd) { | |
| 1292 | - fd = getValidatedFd(fd); | ||
| 1293 | 1286 | binding.fdatasync(fd); | |
| 1294 | 1287 | } | |
| 1295 | 1288 | ||
@@ -1301,7 +1294,6 @@ function fdatasyncSync(fd) { | |||
| 1301 | 1294 | * @returns {void} | |
| 1302 | 1295 | */ | |
| 1303 | 1296 | function fsync(fd, callback) { | |
| 1304 | - fd = getValidatedFd(fd); | ||
| 1305 | 1297 | const req = new FSReqCallback(); | |
| 1306 | 1298 | req.oncomplete = makeCallback(callback); | |
| 1307 | 1299 | binding.fsync(fd, req); | |
@@ -1314,7 +1306,6 @@ function fsync(fd, callback) { | |||
| 1314 | 1306 | * @returns {void} | |
| 1315 | 1307 | */ | |
| 1316 | 1308 | function fsyncSync(fd) { | |
| 1317 | - fd = getValidatedFd(fd); | ||
| 1318 | 1309 | binding.fsync(fd); | |
| 1319 | 1310 | } | |
| 1320 | 1311 | ||
@@ -1535,7 +1526,6 @@ function fstat(fd, options = { bigint: false }, callback) { | |||
| 1535 | 1526 | callback = options; | |
| 1536 | 1527 | options = kEmptyObject; | |
| 1537 | 1528 | } | |
| 1538 | - fd = getValidatedFd(fd); | ||
| 1539 | 1529 | callback = makeStatsCallback(callback); | |
| 1540 | 1530 | ||
| 1541 | 1531 | const req = new FSReqCallback(options.bigint); | |
@@ -1618,7 +1608,6 @@ function statfs(path, options = { bigint: false }, callback) { | |||
| 1618 | 1608 | * @returns {Stats | undefined} | |
| 1619 | 1609 | */ | |
| 1620 | 1610 | function fstatSync(fd, options = { bigint: false }) { | |
| 1621 | - fd = getValidatedFd(fd); | ||
| 1622 | 1611 | const stats = binding.fstat(fd, options.bigint, undefined, false); | |
| 1623 | 1612 | if (stats === undefined) { | |
| 1624 | 1613 | return; | |
@@ -1884,7 +1873,6 @@ function unlinkSync(path) { | |||
| 1884 | 1873 | * @returns {void} | |
| 1885 | 1874 | */ | |
| 1886 | 1875 | function fchmod(fd, mode, callback) { | |
| 1887 | - fd = getValidatedFd(fd); | ||
| 1888 | 1876 | mode = parseFileMode(mode, 'mode'); | |
| 1889 | 1877 | callback = makeCallback(callback); | |
| 1890 | 1878 | ||
@@ -1901,7 +1889,7 @@ function fchmod(fd, mode, callback) { | |||
| 1901 | 1889 | */ | |
| 1902 | 1890 | function fchmodSync(fd, mode) { | |
| 1903 | 1891 | binding.fchmod( | |
| 1904 | - getValidatedFd(fd), | ||
| 1892 | + fd, | ||
| 1905 | 1893 | parseFileMode(mode, 'mode'), | |
| 1906 | 1894 | ); | |
| 1907 | 1895 | } | |
@@ -2029,14 +2017,13 @@ function lchownSync(path, uid, gid) { | |||
| 2029 | 2017 | * @returns {void} | |
| 2030 | 2018 | */ | |
| 2031 | 2019 | function fchown(fd, uid, gid, callback) { | |
| 2032 | - fd = getValidatedFd(fd); | ||
| 2033 | 2020 | validateInteger(uid, 'uid', -1, kMaxUserId); | |
| 2034 | 2021 | validateInteger(gid, 'gid', -1, kMaxUserId); | |
| 2035 | 2022 | callback = makeCallback(callback); | |
| 2036 | 2023 | ||
| 2037 | 2024 | const req = new FSReqCallback(); | |
| 2038 | 2025 | req.oncomplete = callback; | |
| 2039 | - binding.fchown(fd, uid, gid, req); | ||
| 2026 | + binding.fchown(getValidatedFd(fd), uid, gid, req); | ||
| 2040 | 2027 | } | |
| 2041 | 2028 | ||
| 2042 | 2029 | /** | |
@@ -2047,11 +2034,10 @@ function fchown(fd, uid, gid, callback) { | |||
| 2047 | 2034 | * @returns {void} | |
| 2048 | 2035 | */ | |
| 2049 | 2036 | function fchownSync(fd, uid, gid) { | |
| 2050 | - fd = getValidatedFd(fd); | ||
| 2051 | 2037 | validateInteger(uid, 'uid', -1, kMaxUserId); | |
| 2052 | 2038 | validateInteger(gid, 'gid', -1, kMaxUserId); | |
| 2053 | 2039 | ||
| 2054 | - binding.fchown(fd, uid, gid); | ||
| 2040 | + binding.fchown(getValidatedFd(fd), uid, gid); | ||
| 2055 | 2041 | } | |
| 2056 | 2042 | ||
| 2057 | 2043 | /** | |
@@ -2141,7 +2127,6 @@ function utimesSync(path, atime, mtime) { | |||
| 2141 | 2127 | * @returns {void} | |
| 2142 | 2128 | */ | |
| 2143 | 2129 | function futimes(fd, atime, mtime, callback) { | |
| 2144 | - fd = getValidatedFd(fd); | ||
| 2145 | 2130 | atime = toUnixTimestamp(atime, 'atime'); | |
| 2146 | 2131 | mtime = toUnixTimestamp(mtime, 'mtime'); | |
| 2147 | 2132 | callback = makeCallback(callback); | |
@@ -2162,7 +2147,7 @@ function futimes(fd, atime, mtime, callback) { | |||
| 2162 | 2147 | */ | |
| 2163 | 2148 | function futimesSync(fd, atime, mtime) { | |
| 2164 | 2149 | binding.futimes( | |
| 2165 | - getValidatedFd(fd), | ||
| 2150 | + fd, | ||
| 2166 | 2151 | toUnixTimestamp(atime, 'atime'), | |
| 2167 | 2152 | toUnixTimestamp(mtime, 'mtime'), | |
| 2168 | 2153 | ); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1242,8 +1242,10 @@ static void FStat(const FunctionCallbackInfo<Value>& args) { | |||
| 1242 | 1242 | const int argc = args.Length(); | |
| 1243 | 1243 | CHECK_GE(argc, 2); | |
| 1244 | 1244 | ||
| 1245 | - CHECK(args[0]->IsInt32()); | ||
| 1246 | - int fd = args[0].As<Int32>()->Value(); | ||
| 1245 | + int fd; | ||
| 1246 | + if (!GetValidatedFd(env, args[0]).To(&fd)) { | ||
| 1247 | + return; | ||
| 1248 | + } | ||
| 1247 | 1249 | ||
| 1248 | 1250 | bool use_bigint = args[1]->IsTrue(); | |
| 1249 | 1251 | if (!args[2]->IsUndefined()) { // fstat(fd, use_bigint, req) | |
@@ -1493,18 +1495,20 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) { | |||
| 1493 | 1495 | const int argc = args.Length(); | |
| 1494 | 1496 | CHECK_GE(argc, 2); | |
| 1495 | 1497 | ||
| 1496 | - CHECK(args[0]->IsInt32()); | ||
| 1497 | - const int fd = args[0].As<Int32>()->Value(); | ||
| 1498 | + int fd; | ||
| 1499 | + if (!GetValidatedFd(env, args[0]).To(&fd)) { | ||
| 1500 | + return; | ||
| 1501 | + } | ||
| 1498 | 1502 | ||
| 1499 | 1503 | CHECK(IsSafeJsInt(args[1])); | |
| 1500 | 1504 | const int64_t len = args[1].As<Integer>()->Value(); | |
| 1501 | 1505 | ||
| 1502 | - if (argc > 2) { | ||
| 1506 | + if (argc > 2) { // ftruncate(fd, len, req) | ||
| 1503 | 1507 | FSReqBase* req_wrap_async = GetReqWrap(args, 2); | |
| 1504 | 1508 | FS_ASYNC_TRACE_BEGIN0(UV_FS_FTRUNCATE, req_wrap_async) | |
| 1505 | 1509 | AsyncCall(env, req_wrap_async, args, "ftruncate", UTF8, AfterNoArgs, | |
| 1506 | 1510 | uv_fs_ftruncate, fd, len); | |
| 1507 | - } else { | ||
| 1511 | + } else { // ftruncate(fd, len) | ||
| 1508 | 1512 | FSReqWrapSync req_wrap_sync("ftruncate"); | |
| 1509 | 1513 | FS_SYNC_TRACE_BEGIN(ftruncate); | |
| 1510 | 1514 | SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_ftruncate, fd, len); | |
@@ -1518,8 +1522,10 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) { | |||
| 1518 | 1522 | const int argc = args.Length(); | |
| 1519 | 1523 | CHECK_GE(argc, 1); | |
| 1520 | 1524 | ||
| 1521 | - CHECK(args[0]->IsInt32()); | ||
| 1522 | - const int fd = args[0].As<Int32>()->Value(); | ||
| 1525 | + int fd; | ||
| 1526 | + if (!GetValidatedFd(env, args[0]).To(&fd)) { | ||
| 1527 | + return; | ||
| 1528 | + } | ||
| 1523 | 1529 | ||
| 1524 | 1530 | if (argc > 1) { // fdatasync(fd, req) | |
| 1525 | 1531 | FSReqBase* req_wrap_async = GetReqWrap(args, 1); | |
@@ -1541,8 +1547,10 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) { | |||
| 1541 | 1547 | const int argc = args.Length(); | |
| 1542 | 1548 | CHECK_GE(argc, 1); | |
| 1543 | 1549 | ||
| 1544 | - CHECK(args[0]->IsInt32()); | ||
| 1545 | - const int fd = args[0].As<Int32>()->Value(); | ||
| 1550 | + int fd; | ||
| 1551 | + if (!GetValidatedFd(env, args[0]).To(&fd)) { | ||
| 1552 | + return; | ||
| 1553 | + } | ||
| 1546 | 1554 | ||
| 1547 | 1555 | if (argc > 1) { | |
| 1548 | 1556 | FSReqBase* req_wrap_async = GetReqWrap(args, 1); | |
@@ -2615,8 +2623,10 @@ static void FChmod(const FunctionCallbackInfo<Value>& args) { | |||
| 2615 | 2623 | const int argc = args.Length(); | |
| 2616 | 2624 | CHECK_GE(argc, 2); | |
| 2617 | 2625 | ||
| 2618 | - CHECK(args[0]->IsInt32()); | ||
| 2619 | - const int fd = args[0].As<Int32>()->Value(); | ||
| 2626 | + int fd; | ||
| 2627 | + if (!GetValidatedFd(env, args[0]).To(&fd)) { | ||
| 2628 | + return; | ||
| 2629 | + } | ||
| 2620 | 2630 | ||
| 2621 | 2631 | CHECK(args[1]->IsInt32()); | |
| 2622 | 2632 | const int mode = args[1].As<Int32>()->Value(); | |
@@ -2771,8 +2781,10 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) { | |||
| 2771 | 2781 | const int argc = args.Length(); | |
| 2772 | 2782 | CHECK_GE(argc, 3); | |
| 2773 | 2783 | ||
| 2774 | - CHECK(args[0]->IsInt32()); | ||
| 2775 | - const int fd = args[0].As<Int32>()->Value(); | ||
| 2784 | + int fd; | ||
| 2785 | + if (!GetValidatedFd(env, args[0]).To(&fd)) { | ||
| 2786 | + return; | ||
| 2787 | + } | ||
| 2776 | 2788 | ||
| 2777 | 2789 | CHECK(args[1]->IsNumber()); | |
| 2778 | 2790 | const double atime = args[1].As<Number>()->Value(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,6 +20,7 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | 22 | #include "util.h" // NOLINT(build/include_inline) | |
| 23 | + #include <cmath> | ||
| 23 | 24 | #include "util-inl.h" | |
| 24 | 25 | ||
| 25 | 26 | #include "debug_utils-inl.h" | |
@@ -31,6 +32,7 @@ | |||
| 31 | 32 | #include "node_v8_platform-inl.h" | |
| 32 | 33 | #include "string_bytes.h" | |
| 33 | 34 | #include "uv.h" | |
| 35 | + #include "v8-value.h" | ||
| 34 | 36 | ||
| 35 | 37 | #ifdef _WIN32 | |
| 36 | 38 | #include <io.h> // _S_IREAD _S_IWRITE | |
@@ -702,4 +704,87 @@ RAIIIsolate::RAIIIsolate(const SnapshotData* data) | |||
| 702 | 704 | ||
| 703 | 705 | RAIIIsolate::~RAIIIsolate() {} | |
| 704 | 706 | ||
| 707 | + // Returns a string representation of the input value, including type. | ||
| 708 | + // JavaScript implementation is available in lib/internal/errors.js | ||
| 709 | + std::string DetermineSpecificErrorType(Environment* env, | ||
| 710 | + v8::Local<v8::Value> input) { | ||
| 711 | + if (input->IsFunction()) { | ||
| 712 | + return "function"; | ||
| 713 | + } else if (input->IsString()) { | ||
| 714 | + auto value = Utf8Value(env->isolate(), input).ToString(); | ||
| 715 | + if (value.size() > 28) { | ||
| 716 | + value = value.substr(0, 25) + "..."; | ||
| 717 | + } | ||
| 718 | + if (value.find('\'') == std::string::npos) { | ||
| 719 | + return SPrintF("type string ('%s')", value); | ||
| 720 | + } | ||
| 721 | + | ||
| 722 | + // Stringify the input value. | ||
| 723 | + Local<String> stringified = | ||
| 724 | + v8::JSON::Stringify(env->context(), input).ToLocalChecked(); | ||
| 725 | + Utf8Value stringified_value(env->isolate(), stringified); | ||
| 726 | + return SPrintF("type string (%s)", stringified_value.out()); | ||
| 727 | + } else if (input->IsObject()) { | ||
| 728 | + v8::Local<v8::String> constructor_name = | ||
| 729 | + input.As<v8::Object>()->GetConstructorName(); | ||
| 730 | + Utf8Value name(env->isolate(), constructor_name); | ||
| 731 | + return SPrintF("an instance of %s", name.out()); | ||
| 732 | + } | ||
| 733 | + | ||
| 734 | + Utf8Value utf8_value(env->isolate(), | ||
| 735 | + input->ToString(env->context()).ToLocalChecked()); | ||
| 736 | + | ||
| 737 | + if (input->IsNumber() || input->IsInt32() || input->IsUint32()) { | ||
| 738 | + auto value = input.As<v8::Number>()->Value(); | ||
| 739 | + if (std::isnan(value)) { | ||
| 740 | + return "type number (NaN)"; | ||
| 741 | + } else if (std::isinf(value)) { | ||
| 742 | + return "type number (Infinity)"; | ||
| 743 | + } | ||
| 744 | + return SPrintF("type number (%s)", utf8_value.out()); | ||
| 745 | + } else if (input->IsBigInt() || input->IsBoolean() || input->IsSymbol()) { | ||
| 746 | + Utf8Value type(env->isolate(), input->TypeOf(env->isolate())); | ||
| 747 | + return SPrintF("type %s (%s)", type.out(), utf8_value.out()); | ||
| 748 | + } | ||
| 749 | + | ||
| 750 | + // For example: null, undefined | ||
| 751 | + return utf8_value.ToString(); | ||
| 752 | + } | ||
| 753 | + | ||
| 754 | + v8::Maybe<int32_t> GetValidatedFd(Environment* env, | ||
| 755 | + v8::Local<v8::Value> input) { | ||
| 756 | + if (!input->IsInt32() && !input->IsNumber()) { | ||
| 757 | + std::string error_type = node::DetermineSpecificErrorType(env, input); | ||
| 758 | + THROW_ERR_INVALID_ARG_TYPE(env, | ||
| 759 | + "The \"fd\" argument must be of type " | ||
| 760 | + "number. Received %s", | ||
| 761 | + error_type.c_str()); | ||
| 762 | + return v8::Nothing<int32_t>(); | ||
| 763 | + } | ||
| 764 | + | ||
| 765 | + const double fd = input.As<v8::Number>()->Value(); | ||
| 766 | + const bool is_out_of_range = fd < 0 || fd > INT32_MAX; | ||
| 767 | + | ||
| 768 | + if (is_out_of_range || !IsSafeJsInt(input)) { | ||
| 769 | + Utf8Value utf8_value( | ||
| 770 | + env->isolate(), input->ToDetailString(env->context()).ToLocalChecked()); | ||
| 771 | + if (is_out_of_range && !std::isinf(fd)) { | ||
| 772 | + THROW_ERR_OUT_OF_RANGE(env, | ||
| 773 | + "The value of \"fd\" is out of range. " | ||
| 774 | + "It must be >= 0 && <= %s. Received %s", | ||
| 775 | + std::to_string(INT32_MAX), | ||
| 776 | + utf8_value.out()); | ||
| 777 | + } else { | ||
| 778 | + THROW_ERR_OUT_OF_RANGE( | ||
| 779 | + env, | ||
| 780 | + "The value of \"fd\" is out of range. It must be an integer. " | ||
| 781 | + "Received %s", | ||
| 782 | + utf8_value.out()); | ||
| 783 | + } | ||
| 784 | + return v8::Nothing<int32_t>(); | ||
| 785 | + } | ||
| 786 | + | ||
| 787 | + return v8::Just(static_cast<int32_t>(fd)); | ||
| 788 | + } | ||
| 789 | + | ||
| 705 | 790 | } // namespace node | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1013,6 +1013,11 @@ class RAIIIsolate { | |||
| 1013 | 1013 | v8::Isolate::Scope isolate_scope_; | |
| 1014 | 1014 | }; | |
| 1015 | 1015 | ||
| 1016 | + std::string DetermineSpecificErrorType(Environment* env, | ||
| 1017 | + v8::Local<v8::Value> input); | ||
| 1018 | + | ||
| 1019 | + v8::Maybe<int32_t> GetValidatedFd(Environment* env, v8::Local<v8::Value> input); | ||
| 1020 | + | ||
| 1016 | 1021 | } // namespace node | |
| 1017 | 1022 | ||
| 1018 | 1023 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| Back | FazBrowse Home | New Git URL |
0 commit comments