All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] libselinux: make use of strndup
@ 2022-11-09 19:56 Christian Göttsche
  2022-11-09 19:56 ` [PATCH 2/3] libselinux: bail out on path truncations Christian Göttsche
  2022-11-09 19:56 ` [PATCH 3/3] libselinux: filter arguments with path separators Christian Göttsche
  0 siblings, 2 replies; 9+ messages in thread
From: Christian Göttsche @ 2022-11-09 19:56 UTC (permalink / raw)
  To: selinux

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 --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;
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-11-23 15:04 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-09 19:56 [PATCH 1/3] libselinux: make use of strndup Christian Göttsche
2022-11-09 19:56 ` [PATCH 2/3] libselinux: bail out on path truncations Christian Göttsche
2022-11-09 21:38   ` James Carter
2022-11-10 18:23   ` [PATCH v2 " Christian Göttsche
2022-11-09 19:56 ` [PATCH 3/3] libselinux: filter arguments with path separators Christian Göttsche
2022-11-10 21:28   ` James Carter
2022-11-14 19:32   ` [PATCH v3 " Christian Göttsche
2022-11-21 21:39     ` James Carter
2022-11-23 15:03       ` James Carter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.