aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2026-06-02 12:42:36 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2026-06-02 12:42:36 -0400
commit48b2b3f42dab500b6406a65a8cef50f1ede250f5 (patch)
treebed667e9e6f48498d27a20c2a68528c848a03e28 /src/lib
parentd8ee8c27444a56bb98dd8bd67a019a1e9efbcc10 (diff)
downloadstasis-48b2b3f42dab500b6406a65a8cef50f1ede250f5.tar.gz
Add string copy and catonate replacements
* safe_strncpy * safe_strncat
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/core/include/str.h3
-rw-r--r--src/lib/core/str.c19
2 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/core/include/str.h b/src/lib/core/include/str.h
index 3e7c3a4..2f86242 100644
--- a/src/lib/core/include/str.h
+++ b/src/lib/core/include/str.h
@@ -324,4 +324,7 @@ char *to_short_version(const char *s);
void unindent(char *s);
+int safe_strncpy(char *dst, const char *src, size_t dsize);
+int safe_strncat(char *dst, const char *src, size_t dsize);
+
#endif //STASIS_STR_H
diff --git a/src/lib/core/str.c b/src/lib/core/str.c
index c31ce5e..02c6722 100644
--- a/src/lib/core/str.c
+++ b/src/lib/core/str.c
@@ -750,3 +750,22 @@ char *remove_extras(char *s) {
return s;
}
+int safe_strncpy(char *dst, const char *src, const size_t dsize) {
+ return snprintf(dst, dsize, "%s", src);
+}
+
+int safe_strncat(char *dst, const char *src, const size_t dsize) {
+ const size_t used = strnlen(dst, dsize);
+ if (used == dsize) {
+ SYSERROR("destination is not NUL terminated: %p", dst);
+ return -1;
+ }
+ const size_t maxlen = dsize - used;
+ const int len = snprintf(dst + strlen(dst), maxlen, "%s", src);
+ if (len < 0) {
+ SYSERROR("encoding error");
+ } else if ((size_t) len >= maxlen) {
+ SYSWARN("destination truncated: %p", dst);
+ }
+ return len;
+} \ No newline at end of file