diff options
author | Christopher Ferris <cferris@google.com> | 2014-04-04 14:38:18 -0700 |
---|---|---|
committer | Christopher Ferris <cferris@google.com> | 2014-04-07 16:41:53 -0700 |
commit | 950a58e24d1019eb9d814dbb16f111a6b61e3f23 (patch) | |
tree | 5a91099e0ce1b9d552e7210fb914ba8d4d5ddb10 /libc/include/string.h | |
parent | 5f149f1bdef31d198c0e84fd8c904204d263e825 (diff) | |
download | bionic-950a58e24d1019eb9d814dbb16f111a6b61e3f23.zip bionic-950a58e24d1019eb9d814dbb16f111a6b61e3f23.tar.gz bionic-950a58e24d1019eb9d814dbb16f111a6b61e3f23.tar.bz2 |
Add stpcpy/stpncpy.
Add tests for the above.
Add the fortify implementations of __stpcpy_chk and __stpncpy_chk.
Modify the strncpy test to cover more cases and use this template for
stpncpy.
Add all of the fortify test cases.
Bug: 13746695
Change-Id: I8c0f0d4991a878b8e8734fff12c8b73b07fdd344
Diffstat (limited to 'libc/include/string.h')
-rw-r--r-- | libc/include/string.h | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/libc/include/string.h b/libc/include/string.h index ec998ed..c9ae03b 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -53,6 +53,7 @@ extern char* __strrchr_chk(const char *, int, size_t); extern size_t strlen(const char *) __purefunc; extern size_t __strlen_chk(const char *, size_t); extern int strcmp(const char *, const char *) __purefunc; +extern char* stpcpy(char* __restrict, const char* __restrict); extern char* strcpy(char* __restrict, const char* __restrict); extern char* strcat(char* __restrict, const char* __restrict); @@ -72,6 +73,7 @@ extern size_t strnlen(const char *, size_t) __purefunc; extern char* strncat(char* __restrict, const char* __restrict, size_t); extern char* strndup(const char *, size_t); extern int strncmp(const char *, const char *, size_t) __purefunc; +extern char* stpncpy(char* __restrict, const char* __restrict, size_t); extern char* strncpy(char* __restrict, const char* __restrict, size_t); extern size_t strlcat(char* __restrict, const char* __restrict, size_t); @@ -89,8 +91,8 @@ extern size_t strxfrm(char* __restrict, const char* __restrict, size_t); #if defined(__BIONIC_FORTIFY) -__errordecl(__memcpy_dest_size_error, "memcpy called with size bigger than destination"); -__errordecl(__memcpy_src_size_error, "memcpy called with size bigger than source"); +__errordecl(__memcpy_dest_size_error, "memcpy: prevented write past end of buffer"); +__errordecl(__memcpy_src_size_error, "memcpy: prevented read past end of buffer"); __BIONIC_FORTIFY_INLINE void* memcpy(void* __restrict dest, const void* __restrict src, size_t copy_amount) { @@ -116,11 +118,43 @@ void* memmove(void *dest, const void *src, size_t len) { } __BIONIC_FORTIFY_INLINE +char* stpcpy(char* __restrict dest, const char* __restrict src) { + return __builtin___stpcpy_chk(dest, src, __bos(dest)); +} + +__BIONIC_FORTIFY_INLINE char* strcpy(char* __restrict dest, const char* __restrict src) { return __builtin___strcpy_chk(dest, src, __bos(dest)); } -__errordecl(__strncpy_error, "strncpy called with size bigger than buffer"); +__errordecl(__stpncpy_error, "stpncpy: prevented write past end of buffer"); +extern char* __stpncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t); + +__BIONIC_FORTIFY_INLINE +char* stpncpy(char* __restrict dest, const char* __restrict src, size_t n) { + size_t bos_dest = __bos(dest); + size_t bos_src = __bos(src); + if (__builtin_constant_p(n) && (n > bos_dest)) { + __stpncpy_error(); + } + + if (bos_src == __BIONIC_FORTIFY_UNKNOWN_SIZE) { + return __builtin___stpncpy_chk(dest, src, n, bos_dest); + } + + if (__builtin_constant_p(n) && (n <= bos_src)) { + return __builtin___stpncpy_chk(dest, src, n, bos_dest); + } + + size_t slen = __builtin_strlen(src); + if (__builtin_constant_p(slen)) { + return __builtin___stpncpy_chk(dest, src, n, bos_dest); + } + + return __stpncpy_chk2(dest, src, n, bos_dest, bos_src); +} + +__errordecl(__strncpy_error, "strncpy: prevented write past end of buffer"); extern char* __strncpy_chk2(char* __restrict, const char* __restrict, size_t, size_t, size_t); __BIONIC_FORTIFY_INLINE @@ -164,7 +198,7 @@ void* memset(void *s, int c, size_t n) { extern size_t __strlcpy_real(char* __restrict, const char* __restrict, size_t) __asm__(__USER_LABEL_PREFIX__ "strlcpy"); -__errordecl(__strlcpy_error, "strlcpy called with size bigger than buffer"); +__errordecl(__strlcpy_error, "strlcpy: prevented write past end of buffer"); extern size_t __strlcpy_chk(char *, const char *, size_t, size_t); __BIONIC_FORTIFY_INLINE @@ -195,7 +229,7 @@ size_t strlcpy(char* __restrict dest, const char* __restrict src, size_t size) { extern size_t __strlcat_real(char* __restrict, const char* __restrict, size_t) __asm__(__USER_LABEL_PREFIX__ "strlcat"); -__errordecl(__strlcat_error, "strlcat called with size bigger than buffer"); +__errordecl(__strlcat_error, "strlcat: prevented write past end of buffer"); extern size_t __strlcat_chk(char* __restrict, const char* __restrict, size_t, size_t); |