diff options
Diffstat (limited to 'src/lib/libc/net/inet_net_ntop.c')
-rw-r--r-- | src/lib/libc/net/inet_net_ntop.c | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/src/lib/libc/net/inet_net_ntop.c b/src/lib/libc/net/inet_net_ntop.c index f5cb588d10..18eea6bb6d 100644 --- a/src/lib/libc/net/inet_net_ntop.c +++ b/src/lib/libc/net/inet_net_ntop.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: inet_net_ntop.c,v 1.2 2002/02/17 19:42:23 millert Exp $ */ | 1 | /* $OpenBSD: inet_net_ntop.c,v 1.3 2002/08/19 03:01:54 itojun Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 1996 by Internet Software Consortium. | 4 | * Copyright (c) 1996 by Internet Software Consortium. |
@@ -21,7 +21,7 @@ | |||
21 | #if 0 | 21 | #if 0 |
22 | static const char rcsid[] = "$From: inet_net_ntop.c,v 8.2 1996/08/08 06:54:44 vixie Exp $"; | 22 | static const char rcsid[] = "$From: inet_net_ntop.c,v 8.2 1996/08/08 06:54:44 vixie Exp $"; |
23 | #else | 23 | #else |
24 | static const char rcsid[] = "$OpenBSD: inet_net_ntop.c,v 1.2 2002/02/17 19:42:23 millert Exp $"; | 24 | static const char rcsid[] = "$OpenBSD: inet_net_ntop.c,v 1.3 2002/08/19 03:01:54 itojun Exp $"; |
25 | #endif | 25 | #endif |
26 | #endif | 26 | #endif |
27 | 27 | ||
@@ -88,13 +88,19 @@ inet_net_ntop_ipv4(src, bits, dst, size) | |||
88 | char *t; | 88 | char *t; |
89 | u_int m; | 89 | u_int m; |
90 | int b; | 90 | int b; |
91 | char *ep; | ||
92 | int advance; | ||
93 | |||
94 | ep = dst + size; | ||
95 | if (ep <= dst) | ||
96 | goto emsgsize; | ||
91 | 97 | ||
92 | if (bits < 0 || bits > 32) { | 98 | if (bits < 0 || bits > 32) { |
93 | errno = EINVAL; | 99 | errno = EINVAL; |
94 | return (NULL); | 100 | return (NULL); |
95 | } | 101 | } |
96 | if (bits == 0) { | 102 | if (bits == 0) { |
97 | if (size < sizeof "0") | 103 | if (ep - dst < sizeof "0") |
98 | goto emsgsize; | 104 | goto emsgsize; |
99 | *dst++ = '0'; | 105 | *dst++ = '0'; |
100 | *dst = '\0'; | 106 | *dst = '\0'; |
@@ -102,34 +108,43 @@ inet_net_ntop_ipv4(src, bits, dst, size) | |||
102 | 108 | ||
103 | /* Format whole octets. */ | 109 | /* Format whole octets. */ |
104 | for (b = bits / 8; b > 0; b--) { | 110 | for (b = bits / 8; b > 0; b--) { |
105 | if (size < sizeof "255.") | 111 | if (ep - dst < sizeof "255.") |
112 | goto emsgsize; | ||
113 | advance = snprintf(dst, ep - dst, "%u", *src++); | ||
114 | if (advance <= 0 || advance >= ep - dst) | ||
106 | goto emsgsize; | 115 | goto emsgsize; |
107 | t = dst; | 116 | dst += advance; |
108 | dst += sprintf(dst, "%u", *src++); | ||
109 | if (b > 1) { | 117 | if (b > 1) { |
118 | if (dst + 1 >= ep) | ||
119 | goto emsgsize; | ||
110 | *dst++ = '.'; | 120 | *dst++ = '.'; |
111 | *dst = '\0'; | 121 | *dst = '\0'; |
112 | } | 122 | } |
113 | size -= (size_t)(dst - t); | ||
114 | } | 123 | } |
115 | 124 | ||
116 | /* Format partial octet. */ | 125 | /* Format partial octet. */ |
117 | b = bits % 8; | 126 | b = bits % 8; |
118 | if (b > 0) { | 127 | if (b > 0) { |
119 | if (size < sizeof ".255") | 128 | if (ep - dst < sizeof ".255") |
120 | goto emsgsize; | 129 | goto emsgsize; |
121 | t = dst; | ||
122 | if (dst != odst) | 130 | if (dst != odst) |
131 | if (dst + 1 >= ep) | ||
132 | goto emsgsize; | ||
123 | *dst++ = '.'; | 133 | *dst++ = '.'; |
124 | m = ((1 << b) - 1) << (8 - b); | 134 | m = ((1 << b) - 1) << (8 - b); |
125 | dst += sprintf(dst, "%u", *src & m); | 135 | advance = snprintf(dst, ep - dst, "%u", *src & m); |
126 | size -= (size_t)(dst - t); | 136 | if (advance <= 0 || advance >= ep - dst) |
137 | goto emsgsize; | ||
138 | dst += advance; | ||
127 | } | 139 | } |
128 | 140 | ||
129 | /* Format CIDR /width. */ | 141 | /* Format CIDR /width. */ |
130 | if (size < sizeof "/32") | 142 | if (ep - dst < sizeof "/32") |
143 | goto emsgsize; | ||
144 | advance = snprintf(dst, ep - dst, "/%u", bits); | ||
145 | if (advance <= 0 || advance >= ep - dst) | ||
131 | goto emsgsize; | 146 | goto emsgsize; |
132 | dst += sprintf(dst, "/%u", bits); | 147 | dst += advance; |
133 | return (odst); | 148 | return (odst); |
134 | 149 | ||
135 | emsgsize: | 150 | emsgsize: |