]> git.saurik.com Git - apple/libc.git/blobdiff - secure/strncpy_chk.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / secure / strncpy_chk.c
index df4420d1e85eafeab5e0896beecd1659e9e3c03f..e8f70053dc233f820315e2f1d95d7f75b4986633 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007 Apple Inc. All rights reserved.
+ * Copyright (c) 2007-2013 Apple Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  *
 
 #include <stdlib.h>
 #include <string.h>
+#include "secure.h"
 
-extern void __chk_fail (void) __attribute__((__noreturn__));
-
-void *
+char *
 __strncpy_chk (char *restrict dest, char *restrict src,
               size_t len, size_t dstlen)
 {
+  size_t n;
+
   if (__builtin_expect (dstlen < len, 0))
-    __chk_fail ();
+    __chk_fail_overflow ();
+
+  // stpncpy normally returns a pointer to the \0
+  n = stpncpy (dest, src, len) - dest + 1;
+
+  // Check if it's pointing to the location after the buffer after not writing \0
+  if (n == len + 1)
+    n--;
+
+  if (__builtin_expect (__chk_assert_no_overlap != 0, 1))
+    __chk_overlap(dest, n, src, n);
 
-  return strncpy (dest, src, len);
+  return dest;
 }