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

Add basic error checking to parsing nr from rw=randrw:<nr>, etc #1623

Merged
merged 1 commit into from
Sep 2, 2023

Conversation

aggieNick02
Copy link
Contributor

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

@aggieNick02
Copy link
Contributor Author

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);
{
Copy link
Owner

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))
Copy link
Owner

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;
}
Copy link
Owner

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.

@axboe
Copy link
Owner

axboe commented Sep 1, 2023

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
@aggieNick02
Copy link
Contributor Author

aggieNick02 commented Sep 1, 2023

Sorry about the brace/space issues. I think I have those corrected now. Thanks! I updated the PR.

@axboe axboe merged commit 904ee91 into axboe:master Sep 2, 2023
10 checks passed
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

Successfully merging this pull request may close these issues.

Basic error checking needed in parsing nr from rw=randrw:<nr>, etc
2 participants