assert(s != NULL);
char *t;
- t = malloc_chk(len + 1, tag);
- strncpy(t, s, len);
- t[len] = '\0';
+ if(len < strlen(s)) {
+ t = malloc_chk(len + 1, tag);
+ strncpy(t, s, len);
+ t[len] = '\0';
+ } else {
+ t = strdup_chk(s, tag);
+ }
return t;
}
--- /dev/null
+abcdefghijklmnopqrstuvwxyz
--- /dev/null
+include ../Define.mk
+include ../Test.mk
+
+CC := gcc
+CFLAGS := -Wall
+
+.INTERMEDIATE: $(CMD_FILE)
+
+CMDSRC_FILE := cmd.c
+TARGETDIR := ../../../src
+INCLUDEDIR := ../../../include
+TESTTARGET_FILES := $(TARGETDIR)/cmem.c # Set test target files
+
+$(CMD_FILE): $(CMDSRC_FILE) $(TESTTARGET_FILES)
+ $(CC) $(CFLAGS) -I $(INCLUDEDIR) -o $@ $^
+
+clean_cmd:
+ @rm -rf cmd.dSYM cmd
+
+clean: clean_cmd
--- /dev/null
+#include <stdio.h>
+#include "cmem.h"
+
+int main(){
+ char *dst, *src = "abcdefghijklmnopqrstuvwxyz";
+
+ dst = strdup_chk(src, "strdup_chk unit test");
+ printf("%s\n", dst);
+ FREE(dst);
+ return 0;
+}