Skip to contentSkip to conversation

cmd: env: fix unreachable statements

PATCH New AKASHI Takahiro 3 participants 5 messages May 8, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd: env: fix unreachable statements

C's switch statement takes an integer value for switching. As efi_status_t is defined as unsigned long and each error code has the top bit set, all "cases" cannot be reachable.

Reported-by Coverity (CID 300335)
---
 cmd/nvedit_efi.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
cmd/nvedit_efi.c +6 -14
index 837e39e02179..84cba0c7324b 100644
@@ -597,26 +597,18 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	} else {
 		const char *msg;
 
-		switch (ret) {
-		case EFI_NOT_FOUND:
+		if (ret == EFI_NOT_FOUND)
 			msg = " (not found)";
-			break;
-		case EFI_WRITE_PROTECTED:
+		else if (ret == EFI_WRITE_PROTECTED)
 			msg = " (read only)";
-			break;
-		case EFI_INVALID_PARAMETER:
+		else if (ret == EFI_INVALID_PARAMETER)
 			msg = " (invalid parameter)";
-			break;
-		case EFI_SECURITY_VIOLATION:
+		else if (ret == EFI_SECURITY_VIOLATION)
 			msg = " (validation failed)";
-			break;
-		case EFI_OUT_OF_RESOURCES:
+		else if (ret == EFI_OUT_OF_RESOURCES)
 			msg = " (out of memory)";
-			break;
-		default:
+		else
 			msg = "";
-			break;
-		}
 		printf("## Failed to set EFI variable%s\n", msg);
 		ret = CMD_RET_FAILURE;
 	}
Signature · 1 line
2.25.2
#1 lore raw

PATCH 1/1 cmd: env: fix unreachable statements

Quoted 6 lines · AKASHI Takahiro

C's switch statement takes an integer value for switching. As efi_status_t is defined as unsigned long and each error code has the top bit set, all "cases" cannot be reachable.

Reported-by Coverity (CID 300335)

The requirement of C 1999 specification is:
"The controlling expression of a switch statement shall have integer type." The requirement is not that the controlling expression should be int.

GCC works fine with uint64_t as argument of a switch statement.

To me this is a false positive of Coverity.

Best regards

Heinrich

Quoted 42 lines
---
 cmd/nvedit_efi.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
cmd/nvedit_efi.c +6 -14
index 837e39e02179..84cba0c7324b 100644
@@ -597,26 +597,18 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	} else {
 		const char *msg;
 
-		switch (ret) {
-		case EFI_NOT_FOUND:
+		if (ret == EFI_NOT_FOUND)
 			msg = " (not found)";
-			break;
-		case EFI_WRITE_PROTECTED:
+		else if (ret == EFI_WRITE_PROTECTED)
 			msg = " (read only)";
-			break;
-		case EFI_INVALID_PARAMETER:
+		else if (ret == EFI_INVALID_PARAMETER)
 			msg = " (invalid parameter)";
-			break;
-		case EFI_SECURITY_VIOLATION:
+		else if (ret == EFI_SECURITY_VIOLATION)
 			msg = " (validation failed)";
-			break;
-		case EFI_OUT_OF_RESOURCES:
+		else if (ret == EFI_OUT_OF_RESOURCES)
 			msg = " (out of memory)";
-			break;
-		default:
+		else
 			msg = "";
-			break;
-		}
 		printf("## Failed to set EFI variable%s\n", msg);
 		ret = CMD_RET_FAILURE;
 	}
#2 lore raw

PATCH 1/1 cmd: env: fix unreachable statements

Quoted 5 lines · Heinrich Schuchardt
Quoted 4 lines · AKASHI Takahiro

C's switch statement takes an integer value for switching. As efi_status_t is defined as unsigned long and each error code has the top bit set, all "cases" cannot be reachable.

Quoted 8 lines
Quoted 2 lines
Reported-by Coverity (CID 300335)

The requirement of C 1999 specification is:
"The controlling expression of a switch statement shall have integer type." The requirement is not that the controlling expression should be int.

GCC works fine with uint64_t as argument of a switch statement.

OK, but for the record what about C11 with GNU extensions?

Quoted 1 line

To me this is a false positive of Coverity.

I can go mark it as such after the above is answered, 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/20200508/f9aed0a8/attachment.sig>
#3 lore raw

PATCH 1/1 cmd: env: fix unreachable statements

Quoted 10 lines · Tom Rini
Quoted 8 lines · Heinrich Schuchardt
Quoted 6 lines · AKASHI Takahiro

C's switch statement takes an integer value for switching. As efi_status_t is defined as unsigned long and each error code has the top bit set, all "cases" cannot be reachable.

Reported-by Coverity (CID 300335)
Quoted 8 lines
Quoted 6 lines

The requirement of C 1999 specification is: "The controlling expression of a switch statement shall have integer type." The requirement is not that the controlling expression should be int.

GCC works fine with uint64_t as argument of a switch statement.

OK, but for the record what about C11 with GNU extensions?

C11: "The controlling expression of a switchstatement shall have integer type." (https://web.cs.dal.ca/~vlado/pl/C_Standard_2011-n1570.pdf)

Why did you expect anything that is incompatible to C99?

Best regards

Heinrich

Quoted 5 lines
Quoted 2 lines

To me this is a false positive of Coverity.

I can go mark it as such after the above is answered, thanks!

#4 lore raw

PATCH 1/1 cmd: env: fix unreachable statements

Quoted 16 lines · Heinrich Schuchardt
Quoted 15 lines · Tom Rini
Quoted 13 lines · Heinrich Schuchardt
Quoted 6 lines · AKASHI Takahiro

C's switch statement takes an integer value for switching. As efi_status_t is defined as unsigned long and each error code has the top bit set, all "cases" cannot be reachable.

Reported-by Coverity (CID 300335)

The requirement of C 1999 specification is: "The controlling expression of a switch statement shall have integer type." The requirement is not that the controlling expression should be int.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

GCC works fine with uint64_t as argument of a switch statement.

OK, but for the record what about C11 with GNU extensions?

C11: "The controlling expression of a switchstatement shall have integer type." (https://web.cs.dal.ca/~vlado/pl/C_Standard_2011-n1570.pdf)

Why did you expect anything that is incompatible to C99?

I didn't really, I just wanted a reference to C11. It's odd I think Coverity hit this as a false positive, but it's just a tool. 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/20200508/1e12f3c2/attachment.sig>