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

    From John Paul Adrian Glaubitz@21:1/5 to All on Mon Dec 26 11:00:01 2022
    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 <[email protected]>
    ---
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeffrey Walton@21:1/5 to [email protected] on Mon Dec 26 14:30:01 2022
    Hi Adrian,

    On Mon, Dec 26, 2022 at 4:55 AM John Paul Adrian Glaubitz <[email protected]> wrote:

    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 <[email protected]>
    ---
    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

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John Paul Adrian Glaubitz@21:1/5 to Jeffrey Walton on Mon Dec 26 17:40:02 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)