Skip to content

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
merged 3 commits into from
May 23, 2025

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.

@ngoldbaum ngoldbaum added the 09 - Backport-Candidate PRs tagged should be backported label May 22, 2025
@charris charris added this to the 2.3.0 release milestone May 22, 2025
@charris charris merged commit d04c239 into numpy:main May 23, 2025
74 checks passed
@charris
Copy link
Member

charris commented May 23, 2025

Thanks @DimitriPapadopoulos .

@DimitriPapadopoulos DimitriPapadopoulos deleted the const_char branch May 23, 2025 15:52
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label May 23, 2025
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.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy