-
Notifications
You must be signed in to change notification settings - Fork 0
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
Do not use atoi #27
Do not use atoi #27
Conversation
src/chericat.c
Outdated
set_print_level(atoi(optarg)); | ||
{ | ||
char *pEnd; | ||
long int debug_level = strtol(optarg, &pEnd, 10); | ||
if (*pEnd != '\0' || debug_level < 0 || debug_level > 3) { | ||
errx(1, "Debug level can only be 0, 1, 2 or 3, with 0 being debug off and 3 being the most verbose"); | ||
} | ||
set_print_level(debug_level); | ||
} |
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.
I'd be tempted to change the syntax to have -d increment the print level and not take an argument. Then if you want level 3 you just do -ddd and you don't need to worry about overflow because it's impossible to specify enough command line arguments for that to be a program.
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.
I am using the getopt API to parse the options and it doesn't support multiple letters, to support what you suggested I will need to implement new logic to do the parsing, unless you know there is a simpler way?
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.
Normally for something like this you use a counter and have the getopt case bump the counter, e.g.:
static int verbose = 0;
....
while ((ch = getopt(argc, argv, "v")) != -1)
switch (ch) {
....
case 'v':
verbose++;
break;
}
Then later you might have:
if (verbose > 0)
printf("A verbose message\n");
if (verbose > 1)
printf("A more verbose message\n");
If you invoked this program with '-vv' it gets treated as '-v -v' by getopt(3) and verbose
ends up being 2.
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.
Thank you @bsdjhb! The trick works. I have now committed the changes, and updated the usage to reflect the new way to indicate debugging level.
src/mem_scan.c
Outdated
@@ -67,7 +67,12 @@ | |||
*/ | |||
void scan_mem(sqlite3 *db, char* arg_pid) | |||
{ | |||
int pid = atoi(arg_pid); | |||
char *pEnd; | |||
long int pid = strtol(arg_pid, &pEnd, 10); |
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.
I think this string paring should be done in scan_mem's caller (main). That would let you avoid the printf churn.
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.
Addressed.
src/db_process.c
Outdated
char *prot_pEnd; | ||
long int prot = strtol(argv[3], &prot_pEnd, 10); | ||
if (*prot_pEnd != '\0') { | ||
errx(1, "Invalid kve_protection, it should be an integer: %s", argv[3]); | ||
} |
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.
I'd be tempted to roll this into a utility function to avoid this single use variables.
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.
Addressed
src/ptrace_utils.c
Outdated
int pid = atoi(arg_pid); | ||
|
||
char *pEnd; | ||
long int pid = strtol(arg_pid, &pEnd, 10); |
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.
The same comment as scan_mem except that apparently nothing calls this?
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.
It was implemented when I started looking at kernel threads and hence trying to see what ptrace could give us. I haven't worked further on this yet, will extend this piece of code when I get back to it.
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.
Addressed and move the error handling to the caller (when it's implemented).
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.
This patch would benefit form rebasing and squashing.
src/chericat.c
Outdated
" to be printed. If omitted, the default is NO_PRINT level, meaning no\n" | ||
" debugging output. If one 'd' (i.e. -d) is provided, the level is set\n" | ||
" to INFO. If two 'd' (i.e. -dd) is provided, the level is set to VERBOSE.\n" | ||
" The most verbose level is three 'd' (i.e. -ddd), which is the\n" | ||
" TROUBLESHOOTING level. Any more 'd' after three will be ignored.\n" | ||
" -f Provide the database name to capture the data collected by chericat.\n" |
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.
Arguably too much detail for usage. Naming the levels isn't particularly informative.
src/chericat.c
Outdated
fprintf(stderr, "Usage: chericat [-d <debug level>] [-f <database name>] [-p <pid>] [-v]\n\t[-c <binary name>]\n" | ||
" debug level - 0 = No output; 1 = INFO; 2 = VERBOSE; 3 = TROUBLESHOOT\n" | ||
" pid - pid of the target process for a snapshot of caps info\n" | ||
fprintf(stderr, "Usage: chericat [-d*] [-f <database name>] [-p <pid>] [-v]\n\t[-c <binary name>]\n" |
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.
Not user what *
is supposed to mean. You usually just specify that -d can be passed and say that additional times increases debugging.
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.
Fixed - removed the * (it was supposed to represent zero or more) and reduced the details on the usage message. Also squashed the previous 5 commits.
Thanks!
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.
Thanks for the suggestion, committed.
Do not use atoi Tidied up commented out code in db_process Fixed typo in error message Refactored the call to strtol to make the code tidier Removed optarg required for debug_level, instead use multiples of d to indicate level. Updated usage message too.
d0f238c
to
7d75bfa
Compare
Co-authored-by: Brooks Davis <[email protected]>
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.
I'd squash it all when merging and make sure the commit title says what the commit does not that it's squashed.
atoi() doesn't provide a convenient way to check for error conditions/invalid values, hence converted the calls to use strtol() instead so that we can return early when invalid values are detected.
This is to fix issue #22