summaryrefslogtreecommitdiff
path: root/libtac/lib/xalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'libtac/lib/xalloc.c')
-rw-r--r--libtac/lib/xalloc.c25
1 files changed, 24 insertions, 1 deletions
diff --git a/libtac/lib/xalloc.c b/libtac/lib/xalloc.c
index d749b52..8fcce26 100644
--- a/libtac/lib/xalloc.c
+++ b/libtac/lib/xalloc.c
@@ -41,7 +41,7 @@ void *xrealloc(void *ptr, size_t size) {
return val;
}
-char *xstrdup(char *s) {
+char *xstrdup(const char *s) {
char *p;
if (s == NULL) return NULL;
@@ -51,3 +51,26 @@ char *xstrdup(char *s) {
}
return p;
}
+
+
+/*
+ safe string copy that aborts when destination buffer is too small
+*/
+char *xstrcpy(char *dst, const char *src, size_t dst_size) {
+ if (dst == NULL) {
+ TACSYSLOG((LOG_ERR, "xstrcpy(): dst == NULL"));
+ }
+ if (src == NULL) {
+ TACSYSLOG((LOG_ERR, "xstrcpy(): src == NULL"));
+ }
+ if (!dst_size)
+ return NULL;
+
+ if (strlen(src) >= dst_size) {
+ TACSYSLOG((LOG_ERR, "xstrcpy(): argument too long, aborting"));
+ abort();
+ }
+
+ return strcpy(dst, src);
+}
+