GeezerVison is showing github.com — src: add CheckOptions to Options classes · nodejs/node@b6cd185
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node.cc +6-27Lines changed: 6 additions & 27 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2467,7 +2467,7 @@ void ProcessArgv(std::vector<std::string>* args, | 2467 | 2467 | bool is_env) { | 2468 | 2468 | // Parse a few arguments which are specific to Node. | 2469 | 2469 | std::vector<std::string> v8_args; | 2470 | | - std::string error; | | 2470 | + std::vector<std::string> errors{}; | 2471 | 2471 |
| 2472 | 2472 | { | 2473 | 2473 | // TODO(addaleax): The mutex here should ideally be held during the | @@ -2479,11 +2479,13 @@ void ProcessArgv(std::vector<std::string>* args, | 2479 | 2479 | &v8_args, | 2480 | 2480 | per_process_opts.get(), | 2481 | 2481 | is_env ? kAllowedInEnvironment : kDisallowedInEnvironment, | 2482 | | - &error); | | 2482 | + &errors); | 2483 | 2483 | } | 2484 | 2484 |
| 2485 | | - if (!error.empty()) { | 2486 | | - fprintf(stderr, "%s: %s\n", args->at(0).c_str(), error.c_str()); | | 2485 | + if (!errors.empty()) { | | 2486 | + for (auto const& error : errors) { | | 2487 | + fprintf(stderr, "%s: %s\n", args->at(0).c_str(), error.c_str()); | | 2488 | + } | 2487 | 2489 | exit(9); | 2488 | 2490 | } | 2489 | 2491 |
| @@ -2500,30 +2502,7 @@ void ProcessArgv(std::vector<std::string>* args, | 2500 | 2502 | for (const std::string& cve : per_process_opts->security_reverts) | 2501 | 2503 | Revert(cve.c_str()); | 2502 | 2504 |
| 2503 | | - // TODO(addaleax): Move this validation to the option parsers. | 2504 | 2505 | auto env_opts = per_process_opts->per_isolate->per_env; | 2505 | | - if (!env_opts->userland_loader.empty() && | 2506 | | - !env_opts->experimental_modules) { | 2507 | | - fprintf(stderr, "%s: --loader requires --experimental-modules be enabled\n", | 2508 | | - args->at(0).c_str()); | 2509 | | - exit(9); | 2510 | | - } | 2511 | | - | 2512 | | - if (env_opts->syntax_check_only && env_opts->has_eval_string) { | 2513 | | - fprintf(stderr, "%s: either --check or --eval can be used, not both\n", | 2514 | | - args->at(0).c_str()); | 2515 | | - exit(9); | 2516 | | - } | 2517 | | - | 2518 | | -#if HAVE_OPENSSL | 2519 | | - if (per_process_opts->use_openssl_ca && per_process_opts->use_bundled_ca) { | 2520 | | - fprintf(stderr, "%s: either --use-openssl-ca or --use-bundled-ca can be " | 2521 | | - "used, not both\n", | 2522 | | - args->at(0).c_str()); | 2523 | | - exit(9); | 2524 | | - } | 2525 | | -#endif | 2526 | | - | 2527 | 2506 | if (std::find(v8_args.begin(), v8_args.end(), | 2528 | 2507 | "--abort-on-uncaught-exception") != v8_args.end() || | 2529 | 2508 | std::find(v8_args.begin(), v8_args.end(), | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options-inl.h +7-6Lines changed: 7 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -270,7 +270,7 @@ void OptionsParser<Options>::Parse( | 270 | 270 | std::vector<std::string>* const v8_args, | 271 | 271 | Options* const options, | 272 | 272 | OptionEnvvarSettings required_env_settings, | 273 | | - std::string* const error) { | | 273 | + std::vector<std::string>* const errors) { | 274 | 274 | ArgsInfo args(orig_args, exec_args); | 275 | 275 |
| 276 | 276 | // The first entry is the process name. Make sure it ends up in the V8 argv, | @@ -279,7 +279,7 @@ void OptionsParser<Options>::Parse( | 279 | 279 | if (v8_args->empty()) | 280 | 280 | v8_args->push_back(args.program_name()); | 281 | 281 |
| 282 | | - while (!args.empty() && error->empty()) { | | 282 | + while (!args.empty() && errors->empty()) { | 283 | 283 | if (args.first().size() <= 1 || args.first()[0] != '-') break; | 284 | 284 |
| 285 | 285 | // We know that we're either going to consume this | @@ -288,7 +288,7 @@ void OptionsParser<Options>::Parse( | 288 | 288 |
| 289 | 289 | if (arg == "--") { | 290 | 290 | if (required_env_settings == kAllowedInEnvironment) | 291 | | - *error = NotAllowedInEnvErr("--"); | | 291 | + errors->push_back(NotAllowedInEnvErr("--")); | 292 | 292 | break; | 293 | 293 | } | 294 | 294 |
| @@ -357,7 +357,7 @@ void OptionsParser<Options>::Parse( | 357 | 357 | if ((it == options_.end() || | 358 | 358 | it->second.env_setting == kDisallowedInEnvironment) && | 359 | 359 | required_env_settings == kAllowedInEnvironment) { | 360 | | - *error = NotAllowedInEnvErr(original_name); | | 360 | + errors->push_back(NotAllowedInEnvErr(original_name)); | 361 | 361 | break; | 362 | 362 | } | 363 | 363 |
| @@ -379,7 +379,7 @@ void OptionsParser<Options>::Parse( | 379 | 379 | value = arg.substr(equals_index + 1); | 380 | 380 | if (value.empty()) { | 381 | 381 | missing_argument: | 382 | | - *error = RequiresArgumentErr(original_name); | | 382 | + errors->push_back(RequiresArgumentErr(original_name)); | 383 | 383 | break; | 384 | 384 | } | 385 | 385 | } else { | @@ -413,7 +413,7 @@ void OptionsParser<Options>::Parse( | 413 | 413 | break; | 414 | 414 | case kHostPort: | 415 | 415 | Lookup<HostPort>(info.field, options) | 416 | | - ->Update(SplitHostPort(value, error)); | | 416 | + ->Update(SplitHostPort(value, errors)); | 417 | 417 | break; | 418 | 418 | case kNoOp: | 419 | 419 | break; | @@ -424,6 +424,7 @@ void OptionsParser<Options>::Parse( | 424 | 424 | UNREACHABLE(); | 425 | 425 | } | 426 | 426 | } | | 427 | + options->CheckOptions(errors); | 427 | 428 | } | 428 | 429 |
| 429 | 430 | } // namespace options_parser | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +33-5Lines changed: 33 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,32 @@ using v8::Undefined; | 16 | 16 | using v8::Value; | 17 | 17 |
| 18 | 18 | namespace node { | | 19 | + | | 20 | +void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) { | | 21 | +#if HAVE_OPENSSL | | 22 | + if (use_openssl_ca && use_bundled_ca) { | | 23 | + errors->push_back("either --use-openssl-ca or --use-bundled-ca can be " | | 24 | + "used, not both"); | | 25 | + } | | 26 | +#endif | | 27 | + per_isolate->CheckOptions(errors); | | 28 | +} | | 29 | + | | 30 | +void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors) { | | 31 | + per_env->CheckOptions(errors); | | 32 | +} | | 33 | + | | 34 | +void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) { | | 35 | + if (!userland_loader.empty() && !experimental_modules) { | | 36 | + errors->push_back("--loader requires --experimental-modules be enabled"); | | 37 | + } | | 38 | + | | 39 | + if (syntax_check_only && has_eval_string) { | | 40 | + errors->push_back("either --check or --eval can be used, not both"); | | 41 | + } | | 42 | + debug_options->CheckOptions(errors); | | 43 | +} | | 44 | + | 19 | 45 | namespace options_parser { | 20 | 46 |
| 21 | 47 | // XXX: If you add an option here, please also add it to doc/node.1 and | @@ -307,18 +333,20 @@ inline std::string RemoveBrackets(const std::string& host) { | 307 | 333 | return host; | 308 | 334 | } | 309 | 335 |
| 310 | | -inline int ParseAndValidatePort(const std::string& port, std::string* error) { | | 336 | +inline int ParseAndValidatePort(const std::string& port, | | 337 | + std::vector<std::string>* errors) { | 311 | 338 | char* endptr; | 312 | 339 | errno = 0; | 313 | 340 | const long result = strtol(port.c_str(), &endptr, 10); // NOLINT(runtime/int) | 314 | 341 | if (errno != 0 || *endptr != '\0'|| | 315 | 342 | (result != 0 && result < 1024) || result > 65535) { | 316 | | - *error = "Port must be 0 or in range 1024 to 65535."; | | 343 | + errors->push_back(" must be 0 or in range 1024 to 65535."); | 317 | 344 | } | 318 | 345 | return static_cast<int>(result); | 319 | 346 | } | 320 | 347 |
| 321 | | -HostPort SplitHostPort(const std::string& arg, std::string* error) { | | 348 | +HostPort SplitHostPort(const std::string& arg, | | 349 | + std::vector<std::string>* errors) { | 322 | 350 | // remove_brackets only works if no port is specified | 323 | 351 | // so if it has an effect only an IPv6 address was specified. | 324 | 352 | std::string host = RemoveBrackets(arg); | @@ -334,11 +362,11 @@ HostPort SplitHostPort(const std::string& arg, std::string* error) { | 334 | 362 | return HostPort { arg, -1 }; | 335 | 363 | } | 336 | 364 | } | 337 | | - return HostPort { "", ParseAndValidatePort(arg, error) }; | | 365 | + return HostPort { "", ParseAndValidatePort(arg, errors) }; | 338 | 366 | } | 339 | 367 | // Host and port found: | 340 | 368 | return HostPort { RemoveBrackets(arg.substr(0, colon)), | 341 | | - ParseAndValidatePort(arg.substr(colon + 1), error) }; | | 369 | + ParseAndValidatePort(arg.substr(colon + 1), errors) }; | 342 | 370 | } | 343 | 371 |
| 344 | 372 | // Usage: Either: | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.h +15-6Lines changed: 15 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,11 +21,16 @@ struct HostPort { | 21 | 21 | } | 22 | 22 | }; | 23 | 23 |
| | 24 | +class Options { | | 25 | + public: | | 26 | + virtual void CheckOptions(std::vector<std::string>* errors) {} | | 27 | +}; | | 28 | + | 24 | 29 | // These options are currently essentially per-Environment, but it can be nice | 25 | 30 | // to keep them separate since they are a group of options applying to a very | 26 | 31 | // specific part of Node. It might also make more sense for them to be | 27 | 32 | // per-Isolate, rather than per-Environment. | 28 | | -class DebugOptions { | | 33 | +class DebugOptions : public Options { | 29 | 34 | public: | 30 | 35 | bool inspector_enabled = false; | 31 | 36 | bool deprecated_debug = false; | @@ -57,7 +62,7 @@ class DebugOptions { | 57 | 62 | } | 58 | 63 | }; | 59 | 64 |
| 60 | | -class EnvironmentOptions { | | 65 | +class EnvironmentOptions : public Options { | 61 | 66 | public: | 62 | 67 | std::shared_ptr<DebugOptions> debug_options { new DebugOptions() }; | 63 | 68 | bool abort_on_uncaught_exception = false; | @@ -91,17 +96,19 @@ class EnvironmentOptions { | 91 | 96 | std::vector<std::string> user_argv; | 92 | 97 |
| 93 | 98 | inline DebugOptions* get_debug_options(); | | 99 | + void CheckOptions(std::vector<std::string>* errors); | 94 | 100 | }; | 95 | 101 |
| 96 | | -class PerIsolateOptions { | | 102 | +class PerIsolateOptions : public Options { | 97 | 103 | public: | 98 | 104 | std::shared_ptr<EnvironmentOptions> per_env { new EnvironmentOptions() }; | 99 | 105 | bool track_heap_objects = false; | 100 | 106 |
| 101 | 107 | inline EnvironmentOptions* get_per_env_options(); | | 108 | + void CheckOptions(std::vector<std::string>* errors); | 102 | 109 | }; | 103 | 110 |
| 104 | | -class PerProcessOptions { | | 111 | +class PerProcessOptions : public Options { | 105 | 112 | public: | 106 | 113 | std::shared_ptr<PerIsolateOptions> per_isolate { new PerIsolateOptions() }; | 107 | 114 |
| @@ -139,13 +146,15 @@ class PerProcessOptions { | 139 | 146 | #endif | 140 | 147 |
| 141 | 148 | inline PerIsolateOptions* get_per_isolate_options(); | | 149 | + void CheckOptions(std::vector<std::string>* errors); | 142 | 150 | }; | 143 | 151 |
| 144 | 152 | // The actual options parser, as opposed to the structs containing them: | 145 | 153 |
| 146 | 154 | namespace options_parser { | 147 | 155 |
| 148 | | -HostPort SplitHostPort(const std::string& arg, std::string* error); | | 156 | +HostPort SplitHostPort(const std::string& arg, | | 157 | + std::vector<std::string>* errors); | 149 | 158 | void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args); | 150 | 159 |
| 151 | 160 | enum OptionEnvvarSettings { | @@ -255,7 +264,7 @@ class OptionsParser { | 255 | 264 | std::vector<std::string>* const v8_args, | 256 | 265 | Options* const options, | 257 | 266 | OptionEnvvarSettings required_env_settings, | 258 | | - std::string* const error); | | 267 | + std::vector<std::string>* const errors); | 259 | 268 |
| 260 | 269 | private: | 261 | 270 | // We support the wide variety of different option types by remembering | |
|