checkpatch: Add warnings for using strn(cat|cpy)
PATCH 1/1 checkpatch: Add warnings for using strn(cat|cpy)
strn(cat|cpy) has a bad habit of not nul-terminating the destination, resulting in constructions like
strncpy(foo, bar, sizeof(foo) - 1); foo[sizeof(foo) - 1] = '\0';
However, it is very easy to forget about this behavior and accidentally leave a string unterminated. This has shown up in some recent coverity scans [1, 2] (including code recently touched by yours truly).
Fortunately, the guys at OpenBSD came up with strl(cat|cpy), which always nul-terminate strings. These functions are already in U-Boot, so we should encourage new code to use them instead of strn(cat|cpy).
[1] https://lists.denx.de/pipermail/u-boot/2021-March/442888.html
[2] https://lists.denx.de/pipermail/u-boot/2021-January/438073.html
---
scripts/checkpatch.pl | 6 ++++++ tools/patman/test_checkpatch.py | 14 +++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +6 -0
@@ -5892,6 +5892,12 @@ sub process { } } +# prefer strl(cpy|cat) over strn(cpy|cat) + if ($line =~ /\bstrn(cpy|cat)\s*\(/) { + WARN("STRL", + "strl$1 is preferred over strn$1 because it always produces a nul-terminated string\n" . $herecurr); + } + # prefer usleep_range over udelay if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { my $delay = $1;
tools/patman/test_checkpatch.py +13 -1
@@ -353,7 +353,7 @@ index 0000000..2234c87 Args: pm: PatchMaker object to use - msg" Expected message (e.g. 'LIVETREE') + msg: Expected message (e.g. 'LIVETREE') pmtype: Type of problem ('error', 'warning') """ result = pm.run_checkpatch()
@@ -439,6 +439,18 @@ index 0000000..2234c87 self.check_struct('per_device_auto', '_priv', 'DEVICE_PRIV_AUTO') self.check_struct('per_device_plat_auto', '_plat', 'DEVICE_PLAT_AUTO') + def check_strl(self, func): + """Check one of the checks for strn(cpy|cat)""" + pm = PatchMaker() + pm.add_line('common/main.c', "strn%s(foo, bar, sizeof(foo));" % func) + self.checkSingleMessage(pm, "STRL", + "strl%s is preferred over strn%s because it always produces a nul-terminated string\n" + % (func, func)) + + def testStrl(self): + """Check for uses of strn(cat|cpy)""" + self.check_strl("cat"); + self.check_strl("cpy"); if __name__ == "__main__": unittest.main()
Signature · 1 line
2.30.1
PATCH 1/1 checkpatch: Add warnings for using strn(cat|cpy)
Quoted 31 lines · Sean Anderson
strn(cat|cpy) has a bad habit of not nul-terminating the destination, resulting in constructions like
strncpy(foo, bar, sizeof(foo) - 1); foo[sizeof(foo) - 1] = '\0';However, it is very easy to forget about this behavior and accidentally leave a string unterminated. This has shown up in some recent coverity scans [1, 2] (including code recently touched by yours truly).
Fortunately, the guys at OpenBSD came up with strl(cat|cpy), which always nul-terminate strings. These functions are already in U-Boot, so we should encourage new code to use them instead of strn(cat|cpy).
[1] https://lists.denx.de/pipermail/u-boot/2021-March/442888.html
[2] https://lists.denx.de/pipermail/u-boot/2021-January/438073.htmlSigned-off-by Sean Anderson <[email protected]>---
scripts/checkpatch.pl | 6 ++++++ tools/patman/test_checkpatch.py | 14 +++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-)scripts/checkpatch.pl +0
-0@@ -5892,6 +5892,12 @@ sub process { } }
scripts/checkpatch.pl sub process { · line 5903
Quoted 10 lines
scripts/checkpatch.pl +6
-0+# prefer strl(cpy|cat) over strn(cpy|cat) + if ($line =~ /\bstrn(cpy|cat)\s*\(/) { + WARN("STRL", + "strl$1 is preferred over strn$1 because it always produces a nul-terminated string\n" . $herecurr); + } + # prefer usleep_range over udelay if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { my $delay = $1;
This needs to be in the u-boot specific checks section (starting at around line 2300) as I assume Linux won't really want this. Thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210310/dab661a9/attachment.sig>
PATCH 1/1 checkpatch: Add warnings for using strn(cat|cpy)
Quoted 33 lines · Tom Rini
Quoted 32 lines · Sean Anderson
strn(cat|cpy) has a bad habit of not nul-terminating the destination, resulting in constructions like
strncpy(foo, bar, sizeof(foo) - 1); foo[sizeof(foo) - 1] = '\0';However, it is very easy to forget about this behavior and accidentally leave a string unterminated. This has shown up in some recent coverity scans [1, 2] (including code recently touched by yours truly).
Fortunately, the guys at OpenBSD came up with strl(cat|cpy), which always nul-terminate strings. These functions are already in U-Boot, so we should encourage new code to use them instead of strn(cat|cpy).
[1] https://lists.denx.de/pipermail/u-boot/2021-March/442888.html
[2] https://lists.denx.de/pipermail/u-boot/2021-January/438073.htmlSigned-off-by Sean Anderson <[email protected]>---
scripts/checkpatch.pl | 6 ++++++ tools/patman/test_checkpatch.py | 14 +++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-)scripts/checkpatch.pl +0
-0@@ -5892,6 +5892,12 @@ sub process { } }
Quoted 13 lines
scripts/checkpatch.pl sub process { · line 5903
Quoted 9 lines
scripts/checkpatch.pl +6
-0+# prefer strl(cpy|cat) over strn(cpy|cat) + if ($line =~ /\bstrn(cpy|cat)\s*\(/) { + WARN("STRL", + "strl$1 is preferred over strn$1 because it always produces a nul-terminated string\n" . $herecurr); + } + # prefer usleep_range over udelay if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { my $delay = $1;This needs to be in the u-boot specific checks section (starting at around line 2300) as I assume Linux won't really want this. Thanks!
Ah, I didn't realize we had our own section.
Also, as it turns out the strlcat in U-Boot was just a stub, so I will add one in the next revision.
--Sean