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

Message ID 20190719212614.25527-4-erosca@de.adit-jv.com
State Accepted
Delegated to: Tom Rini
Headers show
Series
  • Fixes and improvements in BCB and Android docs [ver #3]
Related show

Commit Message

Eugeniu Rosca July 19, 2019, 9:26 p.m. UTC
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@chromium.org>
Signed-off-by: Eugeniu Rosca <erosca@de.adit-jv.com>
Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org>
---
v3:
 - No changes
 - Added 'Reviewed-by: Sam Protsenko' from v2

v2:
 - Fixed accidental rename of field/size variables in bcb_field_get()
 - https://patchwork.ozlabs.org/patch/1131358/

v1:
 - https://patchwork.ozlabs.org/patch/1131306/
---
 cmd/bcb.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

Comments

Igor Opaniuk July 22, 2019, 11:58 a.m. UTC | #1
On Sat, Jul 20, 2019 at 12:28 AM Eugeniu Rosca <roscaeugeniu@gmail.com> 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@chromium.org>
> Signed-off-by: Eugeniu Rosca <erosca@de.adit-jv.com>
> Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org>
> ---
> v3:
>  - No changes
>  - Added 'Reviewed-by: Sam Protsenko' from v2
>
> v2:
>  - Fixed accidental rename of field/size variables in bcb_field_get()
>  - https://patchwork.ozlabs.org/patch/1131358/
>
> v1:
>  - https://patchwork.ozlabs.org/patch/1131306/
> ---
>  cmd/bcb.c | 22 +++++++++++-----------
>  1 file changed, 11 insertions(+), 11 deletions(-)
>
> diff --git a/cmd/bcb.c b/cmd/bcb.c
> index 3b1c7434e287..fa9fdeeb0dfc 100644
> --- a/cmd/bcb.c
> +++ b/cmd/bcb.c
> @@ -24,17 +24,17 @@ static struct bootloader_message bcb = { { 0 } };
>
>  static int bcb_cmd_get(char *cmd)
>  {
> -       if (!strncmp(cmd, "load", sizeof("load")))
> +       if (!strcmp(cmd, "load"))
>                 return BCB_CMD_LOAD;
> -       if (!strncmp(cmd, "set", sizeof("set")))
> +       if (!strcmp(cmd, "set"))
>                 return BCB_CMD_FIELD_SET;
> -       if (!strncmp(cmd, "clear", sizeof("clear")))
> +       if (!strcmp(cmd, "clear"))
>                 return BCB_CMD_FIELD_CLEAR;
> -       if (!strncmp(cmd, "test", sizeof("test")))
> +       if (!strcmp(cmd, "test"))
>                 return BCB_CMD_FIELD_TEST;
> -       if (!strncmp(cmd, "store", sizeof("store")))
> +       if (!strcmp(cmd, "store"))
>                 return BCB_CMD_STORE;
> -       if (!strncmp(cmd, "dump", sizeof("dump")))
> +       if (!strcmp(cmd, "dump"))
>                 return BCB_CMD_FIELD_DUMP;
>         else
>                 return -1;
> @@ -85,19 +85,19 @@ err:
>
>  static int bcb_field_get(char *name, char **field, int *size)
>  {
> -       if (!strncmp(name, "command", sizeof("command"))) {
> +       if (!strcmp(name, "command")) {
>                 *field = bcb.command;
>                 *size = sizeof(bcb.command);
> -       } else if (!strncmp(name, "status", sizeof("status"))) {
> +       } else if (!strcmp(name, "status")) {
>                 *field = bcb.status;
>                 *size = sizeof(bcb.status);
> -       } else if (!strncmp(name, "recovery", sizeof("recovery"))) {
> +       } else if (!strcmp(name, "recovery")) {
>                 *field = bcb.recovery;
>                 *size = sizeof(bcb.recovery);
> -       } else if (!strncmp(name, "stage", sizeof("stage"))) {
> +       } else if (!strcmp(name, "stage")) {
>                 *field = bcb.stage;
>                 *size = sizeof(bcb.stage);
> -       } else if (!strncmp(name, "reserved", sizeof("reserved"))) {
> +       } else if (!strcmp(name, "reserved")) {
>                 *field = bcb.reserved;
>                 *size = sizeof(bcb.reserved);
>         } else {
> --
> 2.22.0
>

Reviewed-by: Igor Opaniuk <igor.opaniuk@gmail.com>
Tom Rini July 24, 2019, 8:08 p.m. UTC | #2
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@chromium.org>
> Signed-off-by: Eugeniu Rosca <erosca@de.adit-jv.com>
> Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org>
> Reviewed-by: Igor Opaniuk <igor.opaniuk@gmail.com>

Applied to u-boot/master, thanks!

Patch
diff mbox series

diff --git a/cmd/bcb.c b/cmd/bcb.c
index 3b1c7434e287..fa9fdeeb0dfc 100644
--- a/cmd/bcb.c
+++ b/cmd/bcb.c
@@ -24,17 +24,17 @@  static struct bootloader_message bcb = { { 0 } };
 
 static int bcb_cmd_get(char *cmd)
 {
-	if (!strncmp(cmd, "load", sizeof("load")))
+	if (!strcmp(cmd, "load"))
 		return BCB_CMD_LOAD;
-	if (!strncmp(cmd, "set", sizeof("set")))
+	if (!strcmp(cmd, "set"))
 		return BCB_CMD_FIELD_SET;
-	if (!strncmp(cmd, "clear", sizeof("clear")))
+	if (!strcmp(cmd, "clear"))
 		return BCB_CMD_FIELD_CLEAR;
-	if (!strncmp(cmd, "test", sizeof("test")))
+	if (!strcmp(cmd, "test"))
 		return BCB_CMD_FIELD_TEST;
-	if (!strncmp(cmd, "store", sizeof("store")))
+	if (!strcmp(cmd, "store"))
 		return BCB_CMD_STORE;
-	if (!strncmp(cmd, "dump", sizeof("dump")))
+	if (!strcmp(cmd, "dump"))
 		return BCB_CMD_FIELD_DUMP;
 	else
 		return -1;
@@ -85,19 +85,19 @@  err:
 
 static int bcb_field_get(char *name, char **field, int *size)
 {
-	if (!strncmp(name, "command", sizeof("command"))) {
+	if (!strcmp(name, "command")) {
 		*field = bcb.command;
 		*size = sizeof(bcb.command);
-	} else if (!strncmp(name, "status", sizeof("status"))) {
+	} else if (!strcmp(name, "status")) {
 		*field = bcb.status;
 		*size = sizeof(bcb.status);
-	} else if (!strncmp(name, "recovery", sizeof("recovery"))) {
+	} else if (!strcmp(name, "recovery")) {
 		*field = bcb.recovery;
 		*size = sizeof(bcb.recovery);
-	} else if (!strncmp(name, "stage", sizeof("stage"))) {
+	} else if (!strcmp(name, "stage")) {
 		*field = bcb.stage;
 		*size = sizeof(bcb.stage);
-	} else if (!strncmp(name, "reserved", sizeof("reserved"))) {
+	} else if (!strcmp(name, "reserved")) {
 		*field = bcb.reserved;
 		*size = sizeof(bcb.reserved);
 	} else {