Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

number parsing is too lenient #469

Open
cebtenzzre opened this issue Aug 21, 2023 · 0 comments
Open

number parsing is too lenient #469

cebtenzzre opened this issue Aug 21, 2023 · 0 comments

Comments

@cebtenzzre
Copy link
Contributor

Since we do not use the 'pos' parameter of stoi/stol/stoll/stoul/stoull/stof/stod/stold, many parameters allow garbage after the number.

For example, I accidentally passed -t 24,3.9 (meaning to use llama.cpp's '-ts' option), and this was interpreted as simply '24' with no error. Also, all of the integer options accept floating-point arguments, but silently alter them, e.g. '1e5' and '1.5' are both interpreted as '1'.

A correct use of stoi to parse a single number looks more like this:

size_t pos;
auto i = std::stoi(s, &pos);
if (pos < s.size())
    throw std::invalid_argument(s);

Although it would be even better if we avoided unhandled exceptions for parsing errors, so we do not coredump in such cases. Printing an error message and then calling exit(1) would be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant