[U-Boot] [PATCH v3 3/4] cmd: bcb: Use strcmp() instead of strncmp() for string literals

Tom Rini trini at konsulko.com
Wed Jul 24 20:08:29 UTC 2019


On Fri, Jul 19, 2019 at 11:26:13PM +0200, Eugeniu Rosca wrote:

> Quote from https://patchwork.ozlabs.org/patch/1104244/#2210814:
> 
>  ----------8<-----------
> strncmp() is chosen for the sake of paranoid/defensive programming.
> Indeed, strncmp() is not really needed when comparing a variable
> with a string literal. We expect strcmp() to behave safely even if the
> string variable is not NUL-terminated.
> 
> In the same scenario, Linux v5.2-rc7 uses both strcmp() and strncmp(),
> but the frequency of strcmp() is higher:
> 
> $ git --version
> git version 2.22.0
> $ (Linux 5.2-rc7) git grep -En 'strncmp\([^"]*"[[:alnum:]]+"' | wc -l
> 1066
> $ (Linux 5.2-rc7) git grep -En 'strcmp\([^"]*"[[:alnum:]]+"' | wc -l
> 1968
> 
> A quick "strcmp vs strncmp" object size test shows that strcmp()
> generates smaller memory footprint (gcc-8, x86_64):
> 
> $ (U-Boot) size cmd/bcb-strncmp.o cmd/bcb-strcmp.o
>    text	   data	    bss	    dec	    hex	filename
>    3373	    400	   2048	   5821	   16bd	cmd/bcb-strncmp.o
>    3314	    400	   2048	   5762	   1682	cmd/bcb-strcmp.o
> 
> So, overall, I agree to use strcmp() whenever variables are compared
> with string literals.
>  ----------8<-----------
> 
> Fixes: db7b7a05b267 ("cmd: Add 'bcb' command to read/modify/write BCB fields")
> Reported-by: Simon Glass <sjg at chromium.org>
> Signed-off-by: Eugeniu Rosca <erosca at de.adit-jv.com>
> Reviewed-by: Sam Protsenko <semen.protsenko at linaro.org>
> Reviewed-by: Igor Opaniuk <igor.opaniuk at gmail.com>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190724/5d90bea4/attachment.sig>


More information about the U-Boot mailing list