Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork10.9k
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 ourterms of service andprivacy 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
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); | ~~~~~~~^~~~~~~~~~~~~~~~~~~
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?
DimitriPapadopoulosMay 16, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 usingmemcpy()
instead ofstrncpy()
to avoid a similar warning. The new code extends the logic tostrncat()
. 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 |
Uh oh!
There was an error while loading.Please reload this page.
See for example: