79 lines
2.5 KiB
Diff
79 lines
2.5 KiB
Diff
|
From 7d5a89314be88b3085cfdf6083837045a5e44aa9 Mon Sep 17 00:00:00 2001
|
||
|
From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= <cgzones@googlemail.com>
|
||
|
Date: Tue, 10 May 2022 20:20:38 +0200
|
||
|
Subject: [PATCH] libselinux: introduce strlcpy
|
||
|
MIME-Version: 1.0
|
||
|
Content-Type: text/plain; charset=UTF-8
|
||
|
Content-Transfer-Encoding: 8bit
|
||
|
Content-type: text/plain
|
||
|
|
||
|
To copy string safely, by always NULL-terminating them, and provide an
|
||
|
easy way to check for truncation introduce the nonstandard function
|
||
|
strlcpy(3). Use the system implementation if available.
|
||
|
|
||
|
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
|
||
|
---
|
||
|
libselinux/src/Makefile | 6 ++++++
|
||
|
libselinux/src/selinux_internal.c | 18 ++++++++++++++++++
|
||
|
libselinux/src/selinux_internal.h | 4 ++++
|
||
|
3 files changed, 28 insertions(+)
|
||
|
create mode 100644 libselinux/src/selinux_internal.c
|
||
|
|
||
|
diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile
|
||
|
index 04bf4f240168..88aa32f80944 100644
|
||
|
--- a/libselinux/src/Makefile
|
||
|
+++ b/libselinux/src/Makefile
|
||
|
@@ -103,6 +103,12 @@ FTS_LDLIBS ?=
|
||
|
|
||
|
override CFLAGS += -I../include -D_GNU_SOURCE $(DISABLE_FLAGS) $(PCRE_CFLAGS)
|
||
|
|
||
|
+# check for strlcpy(3) availability
|
||
|
+H := \#
|
||
|
+ifeq (yes,$(shell printf '${H}include <string.h>\nint main(void){char*d,*s;strlcpy(d, s, 0);return 0;}' | $(CC) -x c -o /dev/null - >/dev/null 2>&1 && echo yes))
|
||
|
+override CFLAGS += -DHAVE_STRLCPY
|
||
|
+endif
|
||
|
+
|
||
|
SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
|
||
|
-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations \
|
||
|
-Wno-deprecated-declarations
|
||
|
diff --git a/libselinux/src/selinux_internal.c b/libselinux/src/selinux_internal.c
|
||
|
new file mode 100644
|
||
|
index 000000000000..c2be7c0a9128
|
||
|
--- /dev/null
|
||
|
+++ b/libselinux/src/selinux_internal.c
|
||
|
@@ -0,0 +1,18 @@
|
||
|
+#include "selinux_internal.h"
|
||
|
+
|
||
|
+#include <string.h>
|
||
|
+
|
||
|
+
|
||
|
+#ifndef HAVE_STRLCPY
|
||
|
+size_t strlcpy(char *dest, const char *src, size_t size)
|
||
|
+{
|
||
|
+ size_t ret = strlen(src);
|
||
|
+
|
||
|
+ if (size) {
|
||
|
+ size_t len = (ret >= size) ? size - 1 : ret;
|
||
|
+ memcpy(dest, src, len);
|
||
|
+ dest[len] = '\0';
|
||
|
+ }
|
||
|
+ return ret;
|
||
|
+}
|
||
|
+#endif /* HAVE_STRLCPY */
|
||
|
diff --git a/libselinux/src/selinux_internal.h b/libselinux/src/selinux_internal.h
|
||
|
index 9f4c90739171..06f2c0383bc2 100644
|
||
|
--- a/libselinux/src/selinux_internal.h
|
||
|
+++ b/libselinux/src/selinux_internal.h
|
||
|
@@ -94,4 +94,8 @@ extern int selinux_page_size ;
|
||
|
|
||
|
extern int has_selinux_config ;
|
||
|
|
||
|
+#ifndef HAVE_STRLCPY
|
||
|
+size_t strlcpy(char *dest, const char *src, size_t size);
|
||
|
+#endif
|
||
|
+
|
||
|
#endif /* SELINUX_INTERNAL_H_ */
|
||
|
--
|
||
|
2.38.1
|
||
|
|