-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add basic error checking to parsing nr from rw=randrw:<nr>, etc #1623
Conversation
Hi @vincentkfu , I was wondering if you might take a quick look at this and let me know what you think. I've been burnt by this before so I think moving away from atoi is good. I can change it to allow 0 even though it doesn't make sense if we are worried there could be clients intentionally passing 0. |
options.c
Outdated
@@ -597,7 +597,22 @@ static int str_rw_cb(void *data, const char *str) | |||
return 0; | |||
|
|||
if (td_random(td)) | |||
o->ddir_seq_nr = atoi(nr); | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Braces go on the same line as the if. You have an odd mix here.
options.c
Outdated
free(nr); | ||
return 1; | ||
} | ||
if ((val <= 0) || (val>UINT_MAX)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spacing, val > UINT_MAX
options.c
Outdated
return 1; | ||
} | ||
o->ddir_seq_nr = (unsigned int) val; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else and closing brace go on the same line.
Some style fixups needed, but I agree with moving away from atoi(). |
Previously this was parsed by just doing atoi(). This returns 0 or has undefined behavior in error cases. Silently getting a 0 for nr is not great. In fact, 0 (or less) should likely not be allowed for nr; while the code handles it, the effective result is that the randomness is gone - all I/O becomes sequential. It makes sense to prohibit 0 as an nr value in the random case. We leverage str_to_decimal to do our parsing instead of atoi. It isn't perfect, but it is a lot more resilient than atoi, and used in other similar places. We can then return an error when parsing fails, and also return an error when the parsed numeric value is outside of the ranges that can be stored in the unsigned int used for nr, along with when nr is 0. Fixes axboe#1622 Signed-off-by: Nick Neumann nick@pcpartpicker.com
21fdef3
to
2e0a525
Compare
Sorry about the brace/space issues. I think I have those corrected now. Thanks! I updated the PR. |
Previously this was parsed by just doing atoi(). This returns 0 or has undefined behavior in error cases.
Silently getting a 0 for nr is not great. In fact, 0 (or less) should likely not be allowed for nr; while the code handles it, the effective result is that the randomness is gone - all I/O becomes sequential. It makes sense to prohibit 0 as an nr value in the random case.
We leverage str_to_decimal to do our parsing instead of atoi. It isn't perfect, but it is a lot more resilient than atoi, and used in other similar places. We can then return an error when parsing fails, and also return an error when the parsed numeric value is outside of the ranges that can be stored in the unsigned int used for nr, along with when nr is 0.
Fixes #1622
Signed-off-by: Nick Neumann nick@pcpartpicker.com