[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH 1/3] lib/nolibc/string: fix underflow in strnlen



The memchr return NULL if it did not find '\0' character. The strnlen
did not take this into account and always did subtraction to count the
string length.

Return the provided length limit if the line ending is not found
within this limit.

Signed-off-by: Yuri Volchkov <yuri.volchkov@xxxxxxxxx>
---
 lib/nolibc/string.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/nolibc/string.c b/lib/nolibc/string.c
index 6f853ba..fee2d10 100644
--- a/lib/nolibc/string.c
+++ b/lib/nolibc/string.c
@@ -132,7 +132,8 @@ size_t strlen(const char *str)
 
 size_t strnlen(const char *str, size_t len)
 {
-       return (size_t)((uintptr_t)memchr(str, '\0', len) - (uintptr_t)str);
+       const char *p = memchr(str, 0, len);
+       return p ? (size_t) (p - str) : len;
 }
 
 char *strncpy(char *dst, const char *src, size_t len)
-- 
2.19.2


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.