[PATCH 2/3] drmgr/common_pci: Prefer strlen() to check for valid string length

1 view
Skip to first unread message

John Paul Adrian Glaubitz

<glaubitz@physik.fu-berlin.de>
unread,
Dec 26, 2022, 4:54:43 AM12/26/22
to powerpc-utils-devel@googlegroups.com, debian-powerpc@lists.debian.org, tyreld@linux.ibm.com, John Paul Adrian Glaubitz
This fixes the following warning when building with gcc-12 that is
the result of ofdt_path being a fixed-sized array which means that
(char *)ofdt_path never be NULL:

src/drmgr/common_pci.c: In function 'devspec_check_node':
src/drmgr/common_pci.c:465:29: error: the comparison will always evaluate as 'false' for the address of 'ofdt_path' will never be NULL [-Werror=address]
465 | if (node->ofdt_path == NULL)
| ^~
In file included from src/drmgr/drpci.h:25,
from src/drmgr/rtas_calls.h:25,
from src/drmgr/dr.h:30,
from src/drmgr/common_pci.c:31:
src/drmgr/ofdt.h:78:25: note: 'ofdt_path' declared here
78 | char ofdt_path[DR_PATH_MAX];
|

Signed-off-by: John Paul Adrian Glaubitz <glau...@physik.fu-berlin.de>
---
src/drmgr/common_pci.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/drmgr/common_pci.c b/src/drmgr/common_pci.c
index cbd48c9..c6dcfdf 100644
--- a/src/drmgr/common_pci.c
+++ b/src/drmgr/common_pci.c
@@ -462,7 +462,7 @@ devspec_check_node(struct dr_node *node, char *sysfs_path,

*found = 0;

- if (node->ofdt_path == NULL)
+ if (!strlen(node->ofdt_path))
return 0;

if (! strcmp(full_of_path, node->ofdt_path)) {
--
2.30.2

Jeffrey Walton

<noloader@gmail.com>
unread,
Dec 26, 2022, 8:28:06 AM12/26/22
to John Paul Adrian Glaubitz, powerpc-utils-devel@googlegroups.com, debian-powerpc@lists.debian.org, tyreld@linux.ibm.com
Hi Adrian,
Do you need strlen? It looks like you only care that the path is
non-empty. You may be able to save on the call to strlen, and instead,
check the first char of the string. So maybe something like:

> - if (node->ofdt_path == NULL)
> + if (! node->ofdt_path[0])
> return 0;

My apologies if I am mis-parsing things.

Jeff

John Paul Adrian Glaubitz

<glaubitz@physik.fu-berlin.de>
unread,
Dec 26, 2022, 11:38:16 AM12/26/22
to noloader@gmail.com, powerpc-utils-devel@googlegroups.com, debian-powerpc@lists.debian.org, tyreld@linux.ibm.com
Hello Jeffrey!

On 12/26/22 14:27, Jeffrey Walton wrote:
> Do you need strlen? It looks like you only care that the path is
> non-empty. You may be able to save on the call to strlen, and instead,
> check the first char of the string. So maybe something like:
>
>> - if (node->ofdt_path == NULL)
>> + if (! node->ofdt_path[0])
>> return 0;

Using strlen creates a more readable and safer code. And since the string-optimization
routines of the compiler are extremely good these days [1], there is no need trying to
beat the compiler with manual optimizations.

Adrian

> [1] https://www.youtube.com/watch?v=f_tLQl0wLUM

--
.''`. John Paul Adrian Glaubitz
: :' : Debian Developer
`. `' Physicist
`- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913

Reply all
Reply to author
Forward
0 new messages