-
-
Notifications
You must be signed in to change notification settings - Fork 10.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+40
−35
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -605,7 +605,7 @@ load_non_nullable_string(char *in, int has_null, const npy_static_string *defaul | |
const npy_packed_static_string *ps = (npy_packed_static_string *)in; | ||
int isnull = NpyString_load(allocator, ps, string_to_load); | ||
if (isnull == -1) { | ||
const char *msg = "Failed to load string for conversion to a non-nullable type"; | ||
const char msg[] = "Failed to load string for conversion to a non-nullable type"; | ||
if (has_gil) | ||
{ | ||
PyErr_SetString(PyExc_MemoryError, msg); | ||
|
@@ -617,7 +617,7 @@ load_non_nullable_string(char *in, int has_null, const npy_static_string *defaul | |
} | ||
else if (isnull) { | ||
if (has_null) { | ||
const char *msg = "Arrays with missing data cannot be converted to a non-nullable type"; | ||
const char msg[] = "Arrays with missing data cannot be converted to a non-nullable type"; | ||
if (has_gil) | ||
{ | ||
PyErr_SetString(PyExc_ValueError, msg); | ||
|
@@ -821,8 +821,8 @@ static PyType_Slot s2int_slots[] = { | |
|
||
static const char * | ||
make_s2type_name(NPY_TYPES typenum) { | ||
const char *prefix = "cast_StringDType_to_"; | ||
size_t plen = strlen(prefix); | ||
const char prefix[] = "cast_StringDType_to_"; | ||
size_t plen = sizeof(prefix)/sizeof(char) - 1; | ||
|
||
const char *type_name = typenum_to_cstr(typenum); | ||
size_t nlen = strlen(type_name); | ||
|
@@ -833,31 +833,36 @@ make_s2type_name(NPY_TYPES typenum) { | |
return NULL; | ||
} | ||
|
||
// memcpy instead of strcpy to avoid stringop-truncation warning, since | ||
// we are not including the trailing null character | ||
memcpy(buf, prefix, plen); | ||
strncat(buf, type_name, nlen); | ||
// memcpy instead of strcpy/strncat to avoid stringop-truncation warning, | ||
// since we are not including the trailing null character | ||
char *p = buf; | ||
memcpy(p, prefix, plen); | ||
p += plen; | ||
memcpy(p, type_name, nlen); | ||
return buf; | ||
} | ||
|
||
static const char * | ||
make_type2s_name(NPY_TYPES typenum) { | ||
const char *prefix = "cast_"; | ||
size_t plen = strlen(prefix); | ||
const char prefix[] = "cast_"; | ||
size_t plen = sizeof(prefix)/sizeof(char) - 1; | ||
|
||
const char *type_name = typenum_to_cstr(typenum); | ||
size_t nlen = strlen(type_name); | ||
|
||
const char *suffix = "_to_StringDType"; | ||
size_t slen = strlen(suffix); | ||
const char suffix[] = "_to_StringDType"; | ||
size_t slen = sizeof(prefix)/sizeof(char) - 1; | ||
|
||
char *buf = (char *)PyMem_RawCalloc(sizeof(char), plen + nlen + slen + 1); | ||
|
||
// memcpy instead of strcpy to avoid stringop-truncation warning, since | ||
// we are not including the trailing null character | ||
memcpy(buf, prefix, plen); | ||
strncat(buf, type_name, nlen); | ||
strncat(buf, suffix, slen); | ||
// memcpy instead of strcpy/strncat to avoid stringop-truncation warning, | ||
// since we are not including the trailing null character | ||
char *p = buf; | ||
memcpy(p, prefix, plen); | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. this too |
||
return buf; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?
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:
The previous code had already been using
memcpy()
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: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.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.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 👍