strndup_chk関数の修正とユニットテスト追加
[YACASL2.git] / src / cmem.c
index 4e24586..17535d8 100644 (file)
@@ -31,3 +31,18 @@ char *strdup_chk(const char *s, char *tag)
     strcpy(t, s);
     return t;
 }
+
+char *strndup_chk(const char *s, size_t len, char *tag)
+{
+    assert(s != NULL);
+    char *t;
+
+    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;
+}