diff options
author | Michael Brown <mcb30@ipxe.org> | 2014-05-18 21:05:39 +0100 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2014-05-18 21:09:49 +0100 |
commit | 2b4be69eee372e1010af2302e82c3d0cf1d875e9 (patch) | |
tree | 29a68f744d7df70437b8e3ede82e98c3d4c9de9e /src/core | |
parent | 524936895089772a3d1eac629b5f978fa076a161 (diff) | |
download | ipxe-2b4be69eee372e1010af2302e82c3d0cf1d875e9.tar.gz |
[libc] Prevent strndup() from reading beyond the end of the string
strndup() may be called on a string which is not NUL-terminated. Use
strnlen() instead of strlen() to ensure that we do not read beyond the
end of such a string.
Add self-tests for strndup(), including a test case with an
unterminated string.
Originally-fixed-by: Marin Hannache <git@mareo.fr>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/string.c | 4 |
1 files changed, 1 insertions, 3 deletions
diff --git a/src/core/string.c b/src/core/string.c index 190007a4..e53c283c 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -337,11 +337,9 @@ void * memchr(const void *s, int c, size_t n) char * strndup(const char *s, size_t n) { - size_t len = strlen(s); + size_t len = strnlen(s,n); char *new; - if (len>n) - len = n; new = malloc(len+1); if (new) { new[len] = '\0'; |