-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
MNT: constant string arrays instead of pointers in C #28985
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
base: main
Are you sure you want to change the base?
Conversation
3dda2c3
to
d171d09
Compare
d171d09
to
ad50492
Compare
Trying to avoid this compiler warning: ../numpy/_core/src/multiarray/stringdtype/casts.cpp:860:12: warning: 'char* strncat(char*, const char*, size_t)' specified bound 15 equals source length [-Wstringop-overflow=] 860 | strncat(buf, suffix, slen); | ~~~~~~~^~~~~~~~~~~~~~~~~~~
7f2226a
to
62ec719
Compare
62ec719
to
22132f1
Compare
char *p = buf; | ||
memcpy(p, prefix, plen); | ||
p += plen; | ||
memcpy(p, type_name, nlen); |
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.
why this change?
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.
See the comment in the relevant commit:
Trying to avoid this compiler warning:
../numpy/_core/src/multiarray/stringdtype/casts.cpp:860:12: warning: 'char* strncat(char*, const char*, size_t)' specified bound 15 equals source length [-Wstringop-overflow=]
860 | strncat(buf, suffix, slen);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
The previous code had already been using memcpy()
instead of strncpy()
to avoid a similar warning. The new code extends the logic to strncat()
. In any case, the code should be consistent, and use only one set of methods:
- Use
strcpy()
/strcat()
all along, relying on the trailing NUL to find the characters to copy. This would avoid compiler warnings. I know these functions feel less secure, but in this case they are not since we know for sure the destinationbuf
is large enough. - Use
strncpy()
/strncat()
all along, looking for a trailing NUL while copying, but omitting writing the trailing NUL in the destinationbuf
, since it is already initialised byPyMem_RawCalloc()
. This results in the above compiler warning, because the trailing NUL is omitted. - Use
memcpy()
all along, relying on the already calculated length of the strings. That should be the most efficient option and avoids compiler warnings.
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.
Sounds good 👍
p += plen; | ||
memcpy(p, type_name, nlen); | ||
p += nlen; | ||
memcpy(p, suffix, slen); |
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 too
I wonder if we could get clang-tidy to lint stuff like this. |
I think Linux's |
See for example: