Skip to content

MAINT: f2py: replace deprecated sprintf with snprintf#30998

Merged
mattip merged 3 commits into
numpy:mainfrom
lucascolley:patch-2
Mar 19, 2026
Merged

MAINT: f2py: replace deprecated sprintf with snprintf#30998
mattip merged 3 commits into
numpy:mainfrom
lucascolley:patch-2

Conversation

@lucascolley

Copy link
Copy Markdown
Contributor

PR summary

See gh-30997

AI Disclosure

I wasn't sure what the size should be for the mess + strlen(mess) case so ChatGPT suggested that patch.

cc @mattip

Comment thread numpy/f2py/cfuncs.py
Comment thread numpy/f2py/cfuncs.py Outdated
Co-authored-by: Lucas Colley <lucas.colley8@gmail.com>
Comment thread numpy/f2py/cfuncs.py
@lucascolley lucascolley requested a review from mattip March 13, 2026 12:16

@HaoZeke HaoZeke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks @lucascolley

Comment thread numpy/f2py/cfuncs.py
Comment on lines +855 to 857
size_t len = strlen(mess);
snprintf(mess + len, F2PY_MESSAGE_BUFFER_SIZE - len,
" -- expected str|bytes|sequence-of-str-or-bytes, got ");

@DWesl DWesl Mar 13, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For this particular case, would strcat/strncat work? I suspect strcat will have the same problems as sprintf, but strncat should work.

Suggested change
size_t len = strlen(mess);
snprintf(mess + len, F2PY_MESSAGE_BUFFER_SIZE - len,
" -- expected str|bytes|sequence-of-str-or-bytes, got ");
size_t len = strlen(mess);
strncat(mess, " -- expected str|bytes|sequence-of-str-or-bytes, got ",
F2PY_MESSAGE_BUFFER_SIZE - len);

(This will need #include <string.h> somewhere in the resulting file)

@lucascolley

Copy link
Copy Markdown
Contributor Author

can we merge it? Happy to apply the suggestion from @DWesl if others agree

@mattip mattip merged commit 7a58071 into numpy:main Mar 19, 2026
79 checks passed
@mattip

mattip commented Mar 19, 2026

Copy link
Copy Markdown
Member

Thanks @lucascolley

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.

5 participants