diff options
author | Marvin Borner | 2021-04-03 18:32:07 +0200 |
---|---|---|
committer | Marvin Borner | 2021-04-03 18:32:07 +0200 |
commit | bd1ad65704d44e3e06a587a890e8087bfd9abe3b (patch) | |
tree | 1c78794e0d1ca9f6e37119ef917b50de9f903446 /libs/libc/str.c | |
parent | 4f5062cea9dc5448c1d8fc5ee78dcf211635ea97 (diff) |
Added strnlen for buffer overflow prevention
Diffstat (limited to 'libs/libc/str.c')
-rw-r--r-- | libs/libc/str.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/libs/libc/str.c b/libs/libc/str.c index 52af5c4..9a30e92 100644 --- a/libs/libc/str.c +++ b/libs/libc/str.c @@ -13,6 +13,16 @@ u32 strlen(const char *str) return s - str; } +u32 strnlen(const char *str, u32 max) +{ + const char *s = str; + while (max && *s) { + s++; + max--; + } + return s - str; +} + u32 strlcpy(char *dst, const char *src, u32 size) { const char *orig = src; @@ -231,6 +241,14 @@ u32 strlen_user(const char *str) return ret; } +u32 strnlen_user(const char *str, u32 max) +{ + stac(); + u32 ret = strnlen(str, max); + clac(); + return ret; +} + u32 strlcpy_user(char *dst, const char *src, u32 size) { stac(); |