Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
Loading
from

Conversation

DimitriPapadopoulos
Copy link
Contributor

@DimitriPapadopoulos DimitriPapadopoulos commented May 16, 2025

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Contributor Author

@DimitriPapadopoulos DimitriPapadopoulos May 16, 2025

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 destination buf 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 by PyMem_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.

Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this too

@DimitriPapadopoulos DimitriPapadopoulos marked this pull request as ready for review May 16, 2025 12:52
@ngoldbaum
Copy link
Member

I wonder if we could get clang-tidy to lint stuff like this.

@DimitriPapadopoulos
Copy link
Contributor Author

I think Linux's checkpatch.pl could have caught some of these issues, I don't have experience with clang-tidy but wasn't able to find relevant checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.