Documentation/checkpatch: Prefer str_has_prefix over strncmp
Subsystems (3)
PATCH 1/1 Documentation/checkpatch: Prefer str_has_prefix over strncmp
Add strncmp() to Documentation/process/deprecated.rst since using strncmp() to check whether a string starts with a prefix is error-prone.
The safe replacement is str_has_prefix().
Also add check to the newly introduced deprecated_string_apis in checkpatch.pl.
This patch depends on patch:
"Documentation/checkpatch: Prefer stracpy/strscpy over strcpy/strlcpy/strncpy."
--- Documentation/process/deprecated.rst | 8 ++++++++ scripts/checkpatch.pl | 1 + 2 files changed, 9 insertions(+)
Documentation/process/deprecated.rst +8 -0
@@ -109,6 +109,14 @@ the given limit of bytes to copy. This is inefficient and can lead to linear read overflows if a source string is not NUL-terminated. The safe replacement is stracpy() or strscpy(). +strncmp() +--------- +:c:func:`strncmp` is often used to test if a string starts with a prefix +by strncmp(str, prefix, length of prefix). This is error-prone because +length of prefix can have counting error if using a constant length, or use +sizeof(prefix) without - 1. Also, if the prefix is a pointer, sizeof(prefix) +leads to a wrong size. The safe replacement is str_has_prefix(). + Variable Length Arrays (VLAs) ----------------------------- Using stack VLAs produces much worse machine code than statically
scripts/checkpatch.pl +1 -0
@@ -609,6 +609,7 @@ our %deprecated_string_apis = ( "strcpy" => "stracpy or strscpy", "strlcpy" => "stracpy or strscpy", "strncpy" => "stracpy or strscpy - for non-NUL-terminated uses, strncpy dest should be __nonstring", + "strncmp" => "str_has_prefix", ); #Create a search pattern for all these strings apis to speed up a loop below
Signature · 1 line
2.20.1
Jonathan Corbet
maintainerQuoted 4 lines · On Fri, 2 Aug 2019 14:25:37 +0800 Chuhong Yuan <[email protected]> wrote:
Add
strncmp()toDocumentation/process/deprecated.rstsince usingstrncmp()to check whether a string starts with a prefix is error-prone.
The safe replacement isstr_has_prefix().
Is that the *only* use of strncmp()?
Quoted 18 lines
Also add check to the newly introduced
deprecated_string_apisin checkpatch.pl.This patch depends on patch:
"Documentation/checkpatch: Prefer stracpy/strscpy over strcpy/strlcpy/strncpy."Signed-off-by Chuhong Yuan <[email protected]>--- Documentation/process/deprecated.rst | 8 ++++++++ scripts/checkpatch.pl | 1 + 2 files changed, 9 insertions(+)Documentation/process/deprecated.rst +0
-0
Documentation/process/deprecated.rst the given limit of bytes to copy. This is inefficient and can lead to · line 114
Quoted 6 lines
Documentation/process/deprecated.rst +3
-0@@ -109,6 +109,14 @@ the given limit of bytes to copy. This is inefficient and can lead to linear read overflows if a source string is not NUL-terminated. The safe replacement is stracpy() or strscpy(). +strncmp() +--------- +:c:func:`strncmp` is often used to test if a string starts with a prefix
Please don't use :c:func: anymore; just say strncmp() and the right things will happen.
Thanks,
jon
Quoted 10 lines · Jonathan Corbet <[email protected]> 于2019年8月2日周五 下午9:38写道:
Quoted 4 lines · On Fri, 2 Aug 2019 14:25:37 +0800 Chuhong Yuan <[email protected]> wrote:
Add
strncmp()toDocumentation/process/deprecated.rstsince usingstrncmp()to check whether a string starts with a prefix is error-prone.
The safe replacement isstr_has_prefix().Is that the *only* use of
strncmp()?
This is not the only use of strncmp().
Maybe add a case description like strncpy() is more precise? For example, "strncmp() on string prefix".
Quoted 22 lines
Quoted 22 lines
Also add check to the newly introduced
deprecated_string_apisin checkpatch.pl.This patch depends on patch:
"Documentation/checkpatch: Prefer stracpy/strscpy over strcpy/strlcpy/strncpy."Signed-off-by Chuhong Yuan <[email protected]>--- Documentation/process/deprecated.rst | 8 ++++++++ scripts/checkpatch.pl | 1 + 2 files changed, 9 insertions(+)Documentation/process/deprecated.rst +0
-0@@ -109,6 +109,14 @@ the given limit of bytes to copy. This is inefficient and can lead to linear read overflows if a source string is not NUL-terminated. The safe replacement is stracpy() or strscpy().
Quoted 7 lines
Documentation/process/deprecated.rst the given limit of bytes to copy. This is inefficient and can lead to · line 114
Quoted 3 lines
Documentation/process/deprecated.rst +3
-0+strncmp() +--------- +:c:func:`strncmp` is often used to test if a string starts with a prefixPlease don't use :c:func: anymore; just say
strncmp()and the right things will happen.
I will revise this in the next version.
Regards,
Chuhong
Quoted 3 lines
Thanks,
jon