From 2a9ce651b1c5e84f70570a283e2a5ccd1e14eb25 Mon Sep 17 00:00:00 2001 From: jca <> Date: Mon, 8 Dec 2025 13:30:08 +0000 Subject: Do not crash when calling freeaddrinfo(NULL) Supported by Linux since at least 1997, and FreeBSD since 2017. While not defined, there's no real good reason to crash when we could just cope. Indeed, software out there relies on the Linux behavior. Point out in the manpage that the behavior of getaddrinfo(NULL) isn't defined and thus isn't portable. Edge case spotted recently by kn@ in OpenVPN. ok millert@ djm@ deraadt@ kn@ --- src/lib/libc/net/freeaddrinfo.c | 10 +++++++--- src/lib/libc/net/getaddrinfo.3 | 8 ++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'src/lib/libc') diff --git a/src/lib/libc/net/freeaddrinfo.c b/src/lib/libc/net/freeaddrinfo.c index 154f70cd75..c06318fb75 100644 --- a/src/lib/libc/net/freeaddrinfo.c +++ b/src/lib/libc/net/freeaddrinfo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: freeaddrinfo.c,v 1.9 2016/09/21 04:38:56 guenther Exp $ */ +/* $OpenBSD: freeaddrinfo.c,v 1.10 2025/12/08 13:30:08 jca Exp $ */ /* * Copyright (c) 1996, 1997, 1998, 1999, Craig Metz, All rights reserved. @@ -40,11 +40,15 @@ freeaddrinfo(struct addrinfo *ai) { struct addrinfo *p; - do { + /* + * Calling freeaddrinfo() with a NULL pointer is unspecified, + * but try to cope with it anyway for compatibility. + */ + while (ai != NULL) { p = ai; ai = ai->ai_next; free(p->ai_canonname); free(p); - } while (ai); + } } DEF_WEAK(freeaddrinfo); diff --git a/src/lib/libc/net/getaddrinfo.3 b/src/lib/libc/net/getaddrinfo.3 index 780c7a409f..2df5fbe896 100644 --- a/src/lib/libc/net/getaddrinfo.3 +++ b/src/lib/libc/net/getaddrinfo.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: getaddrinfo.3,v 1.61 2022/09/11 06:38:10 jmc Exp $ +.\" $OpenBSD: getaddrinfo.3,v 1.62 2025/12/08 13:30:08 jca Exp $ .\" $KAME: getaddrinfo.3,v 1.36 2005/01/05 03:23:05 itojun Exp $ .\" .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") @@ -16,7 +16,7 @@ .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR .\" PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: September 11 2022 $ +.Dd $Mdocdate: December 8 2025 $ .Dt GETADDRINFO 3 .Os .Sh NAME @@ -475,3 +475,7 @@ flag bit first appeared in Windows 7. .%R RFC 4007 .%T IPv6 Scoped Address Architecture .Re +.Sh CAVEATS +The behavior of +.Fn freeaddrinfo "NULL" +is not specified and therefore not portable. -- cgit v1.2.3-55-g6feb