summaryrefslogtreecommitdiffstats
path: root/XUtils.c
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2021-01-05 14:44:09 +0100
committerBenBE <BenBE@geshi.org>2021-01-11 20:12:34 +0100
commita118928dee0874eef98be81531a98577c1ae3f7d (patch)
treeb37d56ceded1d8ccf7551ae148f5594129851299 /XUtils.c
parent3715301fe3d49a0255788d1157a4c8e12ee97711 (diff)
XUtils: add safe strncpy implementation
The standard strncpy fails to null-terminate the destination in case the source is longer than the passed size.
Diffstat (limited to 'XUtils.c')
-rw-r--r--XUtils.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/XUtils.c b/XUtils.c
index 01f33424..fe2d8b9a 100644
--- a/XUtils.c
+++ b/XUtils.c
@@ -193,6 +193,18 @@ char* String_readLine(FILE* fd) {
}
}
+size_t String_safeStrncpy(char *restrict dest, const char *restrict src, size_t size) {
+ assert(size > 0);
+
+ size_t i = 0;
+ for (; i < size - 1 && src[i]; i++)
+ dest[i] = src[i];
+
+ dest[i] = '\0';
+
+ return i;
+}
+
int xAsprintf(char** strp, const char* fmt, ...) {
va_list vl;
va_start(vl, fmt);

© 2014-2024 Faster IT GmbH | imprint | privacy policy