From ba6566ba78ec5d146deb99b3117f6e7332769e20 Mon Sep 17 00:00:00 2001 From: millert Date: Wed, 13 Apr 2022 16:20:11 +0000 Subject: [PATCH] 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@ --- lib/libc/net/inet_net_pton.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/libc/net/inet_net_pton.c b/lib/libc/net/inet_net_pton.c index aaffc1802a6..610036605dd 100644 --- a/lib/libc/net/inet_net_pton.c +++ b/lib/libc/net/inet_net_pton.c @@ -1,4 +1,4 @@ -/* $OpenBSD: inet_net_pton.c,v 1.11 2021/01/19 16:43:44 florian Exp $ */ +/* $OpenBSD: inet_net_pton.c,v 1.12 2022/04/13 16:20:11 millert Exp $ */ /* * Copyright (c) 2012 by Gilles Chehade @@ -208,6 +208,7 @@ inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) struct in6_addr in6; int ret; int bits; + size_t bytes char buf[INET6_ADDRSTRLEN + sizeof("/128")]; char *sep; const char *errstr; @@ -235,10 +236,11 @@ inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) } } - if ((bits + 7) / 8 > size) { + bytes = (bits + 7) / 8; + if (bytes > size) { errno = EMSGSIZE; return (-1); } - memcpy(dst, &in6.s6_addr, size); + memcpy(dst, &in6.s6_addr, bytes); return (bits); } -- 2.20.1