diff options
author | dtucker <> | 2016-10-14 18:19:04 +0000 |
---|---|---|
committer | dtucker <> | 2016-10-14 18:19:04 +0000 |
commit | fd9bf17b83993290e35c8d524c712af4003fa6ba (patch) | |
tree | b6389db570c445961bdaf8c657dc9a09b80b453a /src/lib/libc/string/strnlen.c | |
parent | c870335aee6efe920863d15f06b388eaf6ad1f1a (diff) | |
download | openbsd-fd9bf17b83993290e35c8d524c712af4003fa6ba.tar.gz openbsd-fd9bf17b83993290e35c8d524c712af4003fa6ba.tar.bz2 openbsd-fd9bf17b83993290e35c8d524c712af4003fa6ba.zip |
Cast pointers to uintptr_t to avoid potential signedness errors.
Based on patch from yuanjie.huang at windriver.com via OpenSSH bz#2608,
with & ok millert, ok deraadt.
Diffstat (limited to 'src/lib/libc/string/strnlen.c')
-rw-r--r-- | src/lib/libc/string/strnlen.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/lib/libc/string/strnlen.c b/src/lib/libc/string/strnlen.c index 26e9743f18..33c3b6e2ca 100644 --- a/src/lib/libc/string/strnlen.c +++ b/src/lib/libc/string/strnlen.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: strnlen.c,v 1.6 2015/08/31 02:53:57 guenther Exp $ */ | 1 | /* $OpenBSD: strnlen.c,v 1.7 2016/10/14 18:19:04 dtucker Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com> | 4 | * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com> |
@@ -19,6 +19,7 @@ | |||
19 | #include <sys/types.h> | 19 | #include <sys/types.h> |
20 | 20 | ||
21 | #include <string.h> | 21 | #include <string.h> |
22 | #include <stdint.h> | ||
22 | 23 | ||
23 | size_t | 24 | size_t |
24 | strnlen(const char *str, size_t maxlen) | 25 | strnlen(const char *str, size_t maxlen) |
@@ -28,6 +29,10 @@ strnlen(const char *str, size_t maxlen) | |||
28 | for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) | 29 | for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) |
29 | ; | 30 | ; |
30 | 31 | ||
31 | return (size_t)(cp - str); | 32 | /* |
33 | * Cast pointers to unsigned type before calculation, to avoid signed | ||
34 | * overflow when the string ends where the MSB has changed. | ||
35 | */ | ||
36 | return (size_t)((uintptr_t)cp - (uintptr_t)str); | ||
32 | } | 37 | } |
33 | DEF_WEAK(strnlen); | 38 | DEF_WEAK(strnlen); |