Skip to content

Commit ee0f213

Browse files
author
millert
committed
inet_net_pton_ipv6: avoid signed vs unsigned comparison
Use a temporary variable to store the number of bytes to be copied (size_t) and also use it as the memcpy(3) length. Previously we copied "size" bytes instead of just the necessary number. OK claudio@ tb@
1 parent cbe7bb1 commit ee0f213

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/lib/libc/net/inet_net_pton.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: inet_net_pton.c,v 1.11 2021/01/19 16:43:44 florian Exp $ */
1+
/* $OpenBSD: inet_net_pton.c,v 1.12 2022/04/13 16:20:11 millert Exp $ */
22

33
/*
44
* Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org>
@@ -208,6 +208,7 @@ inet_net_pton_ipv6(const char *src, u_char *dst, size_t size)
208208
struct in6_addr in6;
209209
int ret;
210210
int bits;
211+
size_t bytes
211212
char buf[INET6_ADDRSTRLEN + sizeof("/128")];
212213
char *sep;
213214
const char *errstr;
@@ -235,10 +236,11 @@ inet_net_pton_ipv6(const char *src, u_char *dst, size_t size)
235236
}
236237
}
237238

238-
if ((bits + 7) / 8 > size) {
239+
bytes = (bits + 7) / 8;
240+
if (bytes > size) {
239241
errno = EMSGSIZE;
240242
return (-1);
241243
}
242-
memcpy(dst, &in6.s6_addr, size);
244+
memcpy(dst, &in6.s6_addr, bytes);
243245
return (bits);
244246
}

0 commit comments

Comments
 (0)