diff mbox series

[1/3] libselinux: make use of strndup

Message ID 20221109195640.60484-1-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit 0cdaf73c0824
Headers show
Series [1/3] libselinux: make use of strndup | expand

Commit Message

Christian Göttsche Nov. 9, 2022, 7:56 p.m. UTC
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>
---
 libselinux/src/context.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libselinux/src/context.c b/libselinux/src/context.c
index 9dddbc5a..a6779a03 100644
--- a/libselinux/src/context.c
+++ b/libselinux/src/context.c
@@ -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;