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
Use strchr instead of strstr #2962
base: master
Are you sure you want to change the base?
Conversation
src/build.c
Outdated
@@ -1057,7 +1057,7 @@ gboolean build_parse_make_dir(const gchar *string, gchar **prefix) | |||
if (string == NULL) | |||
return FALSE; | |||
|
|||
if ((pos = strstr(string, "Entering directory")) != NULL) | |||
if (strstr(string, "Entering directory") != NULL) |
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.
What about this, to avoid rescanning the beginning of the string:
if ((pos = strstr(string, "Entering directory")) != NULL)
{
// ...
pos = strstr(pos, "/"); // instead of ... pos = strstr(string, "/");
// ...
}
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.
And is there any reason not to use strchr instead of strstr (when searching for '/')?
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.
@andy5995 Are there any tangible benefits besides maybe saving a few processor cycles or avoiding some nested function call?
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.
pos = strstr(pos, "/"); // instead of ... pos = strstr(string, "/");
Sounds good! I'll update the PR. Thanks!
Are there any tangible benefits besides maybe saving a few processor cycles or avoiding some nested function call?
Why to not save maybe a few processor cycles or not avoid some nested function call? :)
Upon further reflection... the change I suggested rescans the "Entering directory" portion of the string... Should probably be (but haven't tested): pos = strchr(pos + 18, '/'); From search, the string looks like:
|
I guess we can use hard-coded values ( |
What do you think about Edit - OR simply |
Well... it's inside an
I just tested the change with |
1e6d602
to
2e9617a
Compare
The problem with You're also using the wrong quote char ( |
Fixed now. Thanks!
I thought it could document the code well instead of using hard-coded number. But maybe I should add a comment instead and use the hard-coded number? |
According to internet, clang and gcc can optimize |
Yeah, but depends on what -O settings distros use. Either way it is scanning 18 chars in a loop thats reading data via a pipe from another process, which involves system calls and possibly process switches ie the difference is immaterial. I would just let it rescan for simpler code. |
@rootkea Can you copy/paste the error/warning message from |
It looks like @elextr nixed the idea of moving the pointer ahead for this case, but for future reference, I think you could use |
But now, we are passing the earlier assigned value of |
@andy5995 that would still have the string constant in two places, if it really was worth doing do it like: static char ed[] = "Entering Directory";
.
pos = strstr(string, ed);
.
strchr(pos + sizeof ed - 1, '/') Note the -1 to account for the end NUL. But as I said I don't think its worth doing. |
@andy5995 Thanks for the pointers! I'll definitely take a look. :) |
@elextr , fwiw, I agree. ;) |
Found by: scan-build