Skip to content

Commit

Permalink
Shell: Don't require ArgsParser values to be null-terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
alimpfard authored and awesomekling committed Mar 31, 2023
1 parent 1280d70 commit 1173adb
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions Userland/Shell/Builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,10 @@ ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)

Vector<StringView> descriptors;
Variant<Core::ArgsParser::Option, Core::ArgsParser::Arg, Empty> current;
DeprecatedString help_string_storage;
DeprecatedString long_name_storage;
DeprecatedString value_name_storage;
DeprecatedString name_storage;
DeprecatedString current_variable;
// if max > 1 or min < 1, or explicit `--list`.
bool treat_arg_as_list = false;
Expand Down Expand Up @@ -1521,6 +1525,7 @@ ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)
return true;
},
});

parser.add_option(Core::ArgsParser::Option {
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
.help_string = "Set the help string of the option or argument being defined",
Expand All @@ -1533,8 +1538,8 @@ ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)
return false;
},
[&](auto& option) {
VERIFY(value.length() == strlen(value.characters_without_null_termination()));
option.help_string = value.characters_without_null_termination();
help_string_storage = value;
option.help_string = help_string_storage.characters();
return true;
});
},
Expand All @@ -1554,8 +1559,9 @@ ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)
warnln("Repeated application of --long-name is not allowed, current option has long name set to \"{}\"", option->long_name);
return false;
}
VERIFY(value.length() == strlen(value.characters_without_null_termination()));
option->long_name = value.characters_without_null_termination();

long_name_storage = value;
option->long_name = long_name_storage.characters();
return true;
},
});
Expand Down Expand Up @@ -1603,8 +1609,8 @@ ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)
return false;
}

VERIFY(value.length() == strlen(value.characters_without_null_termination()));
option.value_name = value.characters_without_null_termination();
value_name_storage = value;
option.value_name = value_name_storage.characters();
return true;
},
[&](Core::ArgsParser::Arg& arg) {
Expand All @@ -1613,8 +1619,8 @@ ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)
return false;
}

VERIFY(value.length() == strlen(value.characters_without_null_termination()));
arg.name = value.characters_without_null_termination();
name_storage = value;
arg.name = name_storage.characters();
return true;
});
},
Expand Down

0 comments on commit 1173adb

Please sign in to comment.