Skip to content

bpo-41170: Use strnlen instead of strlen when the size i known. #21236

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

Closed
wants to merge 1 commit into from
Closed

bpo-41170: Use strnlen instead of strlen when the size i known. #21236

wants to merge 1 commit into from

Conversation

edge90
Copy link

@edge90 edge90 commented Jun 30, 2020

Use strnlen instead of strlen when the size i known:

Functions as unicode_decode_locale and _PyUnicodeWriter_WriteASCIIString takes a char* and a size, yet strlen is used.
This PR changes strlen to strnlen so no buffer overruns are made when there's no null terminator.

https://bugs.python.org/issue41170

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@edge90

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@csabella
Copy link
Contributor

@edge90, please sign the CLA. Thank you!

@edge90
Copy link
Author

edge90 commented Sep 21, 2020

@csabella I have already done that once, can try again tomorrow as the system haven't picked it up.

Edit: Added my github name to bugs.python.org (should pickup my CLA now).

@serhiy-storchaka
Copy link
Member

Pros:

  • In theory strnlen() may be slightly faster than strlen(). But it is insignificant in comparison with memory management and using locale specific functions.
  • The new code is slightly clearer. Perhaps.

Cons:

  • strnlen() can also be slightly slower than strlen().
  • strnlen() is in the POSIX standard, but not in the C standard, also it is implemented also on Windows. But it can potentially cause issues with porting Python to exotic platforms.

The current code was added by @vstinner 10 years ago and was not changed much since then. I do not think that its clearness (which is subjective) was a problem. I am inclined to close this issue.

@vstinner
Copy link
Member

This PR changes strlen to strnlen so no buffer overruns are made when there's no null terminator.

strlen() is only called if str[len] is '\0' (null terminator).

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.

7 participants