libselinux: make use of strndup

Using strndup(3) instead of malloc(3) followed by strncpy(3) simplifies
the code and pleases GCC:

    In file included from /usr/include/string.h:535,
                     from context.c:2:
    In function ‘strncpy’,
        inlined from ‘context_new’ at context.c:74:3:
    /usr/include/x86_64-linux-gnu/bits/string_fortified.h:95:10: error: ‘__builtin_strncpy’ destination unchanged after copying no bytes [-Werror=stringop-truncation]
       95 |   return __builtin___strncpy_chk (__dest, __src, __len,
          |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       96 |                                   __glibc_objsize (__dest));
          |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Christian Göttsche 2022-11-09 20:56:38 +01:00 committed by James Carter
parent 7c0a84c8cc
commit 0cdaf73c08

View File

@ -68,11 +68,9 @@ context_t context_new(const char *str)
for (p = tok; *p; p++) { /* empty */
}
}
n->component[i] = (char *)malloc(p - tok + 1);
n->component[i] = strndup(tok, p - tok);
if (n->component[i] == 0)
goto err;
strncpy(n->component[i], tok, p - tok);
n->component[i][p - tok] = '\0';
tok = *p ? p + 1 : p;
}
return result;