This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Use strnlen instead of strlen when the size i known.
Type: Stage: patch review
Components: C API Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Niclas Larsson, eric.smith, larry, python-dev, vstinner
Priority: normal Keywords: patch

Created on 2020-06-30 14:44 by Niclas Larsson, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 21236 closed python-dev, 2020-06-30 14:50
Messages (4)
msg372727 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-07-01 01:08
Is strnlen() supported by all of the compilers we care about?
msg372752 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2020-07-01 12:33
strnlen() isn't standard C, but an exciting new function strnlen_s() is, as of C11.

https://en.cppreference.com/w/c/string/byte/strlen

(At this rate, we should be able to code CPython using that standard in about 2030.)

But!  I found a 2005 thread on /. talking about strnlen in MSVC.  So maybe it's there.  Though Microsoft has this funny habit of putting an underscore in front of C library functions that aren't standard, so maybe it's _strnlen().
msg377632 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-09-28 22:06
PR 21236 was closed by his author, can this issue be closed?
msg377633 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-09-28 22:08
"Use strnlen instead of strlen when the size i known" rationale was "This PR changes strlen to strnlen so no buffer overruns are made when there's no null terminator", but strlen() was not called whne the string was not null terminated.

Serhiy wrote that strnlen() can be faster and that can be slower... If the goal is an optimization, a benchmark should prove it (on at least one platform).
https://github.com/python/cpython/pull/21236#issuecomment-699609949
History
Date User Action Args
2022-04-11 14:59:33adminsetgithub: 85342
2020-09-28 22:08:46vstinnersetmessages: + msg377633
2020-09-28 22:06:33vstinnersetnosy: + vstinner
messages: + msg377632
2020-07-01 12:33:44larrysetnosy: + larry
messages: + msg372752
2020-07-01 01:08:17eric.smithsetnosy: + eric.smith
messages: + msg372727
2020-06-30 14:50:20python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request20388
stage: patch review
2020-06-30 14:44:52Niclas Larssoncreate