src: allows escaping NODE_OPTIONS with backslashes · nodejs/node@3ef1512 (original) (raw)

`@@ -671,11 +671,49 @@ int InitializeNodeWithArgs(std::vectorstd::string* argv,

`

671

671

``

672

672

`#if !defined(NODE_WITHOUT_NODE_OPTIONS)

`

673

673

` std::string node_options;

`

``

674

+

674

675

`if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) {

`

675

``

`-

// [0] is expected to be the program name, fill it in from the real argv

`

676

``

`-

// and use 'x' as a placeholder while parsing.

`

677

``

`-

std::vectorstd::string env_argv = SplitString("x " + node_options, ' ');

`

678

``

`-

env_argv[0] = argv->at(0);

`

``

676

`+

std::vectorstd::string env_argv;

`

``

677

`+

// [0] is expected to be the program name, fill it in from the real argv.

`

``

678

`+

env_argv.push_back(argv->at(0));

`

``

679

+

``

680

`+

bool is_in_string = false;

`

``

681

`+

bool will_start_new_arg = true;

`

``

682

`+

for (std:🧵:size_type index = 0;

`

``

683

`+

index < node_options.size();

`

``

684

`+

++index) {

`

``

685

`+

char c = node_options.at(index);

`

``

686

+

``

687

`+

// Backslashes escape the following character

`

``

688

`+

if (c == '\' && is_in_string) {

`

``

689

`+

if (index + 1 == node_options.size()) {

`

``

690

`+

errors->push_back("invalid value for NODE_OPTIONS "

`

``

691

`+

"(invalid escape)\n");

`

``

692

`+

return 9;

`

``

693

`+

} else {

`

``

694

`+

c = node_options.at(++index);

`

``

695

`+

}

`

``

696

`+

} else if (c == ' ' && !is_in_string) {

`

``

697

`+

will_start_new_arg = true;

`

``

698

`+

continue;

`

``

699

`+

} else if (c == '"') {

`

``

700

`+

is_in_string = !is_in_string;

`

``

701

`+

continue;

`

``

702

`+

}

`

``

703

+

``

704

`+

if (will_start_new_arg) {

`

``

705

`+

env_argv.push_back(std::string(1, c));

`

``

706

`+

will_start_new_arg = false;

`

``

707

`+

} else {

`

``

708

`+

env_argv.back() += c;

`

``

709

`+

}

`

``

710

`+

}

`

``

711

+

``

712

`+

if (is_in_string) {

`

``

713

`+

errors->push_back("invalid value for NODE_OPTIONS "

`

``

714

`+

"(unterminated string)\n");

`

``

715

`+

return 9;

`

``

716

`+

}

`

679

717

``

680

718

`const int exit_code = ProcessGlobalArgs(&env_argv, nullptr, errors, true);

`

681

719

`if (exit_code != 0) return exit_code;

`