Browse code

core: zero-ending ipv4 string in ip_addr2sbufz() like done for ipv6

Daniel-Constantin Mierla authored on 06/04/2022 18:36:26
Showing 1 changed files
... ...
@@ -237,7 +237,7 @@ int ip_addr2sbuf(struct ip_addr* ip, char* buff, int len)
237 237
 }
238 238
 
239 239
 
240
-/* same as ip_addr2sbuf, but with [  ] around IPv6 addresses */
240
+/* same as ip_addr2sbuf, but with [  ] around IPv6 addresses and ending \0 */
241 241
 int ip_addr2sbufz(struct ip_addr* ip, char* buff, int len)
242 242
 {
243 243
 	char *p;
... ...
@@ -247,15 +247,15 @@ int ip_addr2sbufz(struct ip_addr* ip, char* buff, int len)
247 247
 	switch(ip->af){
248 248
 		case AF_INET6:
249 249
 			*p++ = '[';
250
-			sz = ip6tosbuf(ip->u.addr, p, len-2);
250
+			sz = ip6tosbuf(ip->u.addr, p, len-3);
251 251
 			p += sz;
252 252
 			*p++ = ']';
253
-			*p=0;
253
+			*p = '\0';
254 254
 			return sz + 2;
255
-			break;
256 255
 		case AF_INET:
257
-			return ip4tosbuf(ip->u.addr, buff, len);
258
-			break;
256
+			sz = ip4tosbuf(ip->u.addr, buff, len-1);
257
+			buff[sz] = '\0';
258
+			return sz;
259 259
 		default:
260 260
 			LM_CRIT("unknown address family %d\n", ip->af);
261 261
 			return 0;