summaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorEric Faurot <eric@cvs.openbsd.org>2020-12-21 09:40:36 +0000
committerEric Faurot <eric@cvs.openbsd.org>2020-12-21 09:40:36 +0000
commiteebcb898c1053515271c9a04dd9b175c5e47fec1 (patch)
treec6820c161b962ee91efb2d6782e25f4a9f9e57de /lib/libc
parentd243428c13aeccbc317ae1f46ab8dde6616be674 (diff)
Always call endservent_r() after getservbyport_r().
Fix a memory leak when no entry is matched. ok florian
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/asr/getnameinfo.c11
-rw-r--r--lib/libc/asr/getnameinfo_async.c19
2 files changed, 17 insertions, 13 deletions
diff --git a/lib/libc/asr/getnameinfo.c b/lib/libc/asr/getnameinfo.c
index 7bee468dbc0..f6d1228d319 100644
--- a/lib/libc/asr/getnameinfo.c
+++ b/lib/libc/asr/getnameinfo.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getnameinfo.c,v 1.9 2019/07/03 03:24:03 deraadt Exp $ */
+/* $OpenBSD: getnameinfo.c,v 1.10 2020/12/21 09:40:35 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -129,12 +129,13 @@ asr_print_port(const struct sockaddr *sa, const char *proto, char *buf, size_t b
if (proto) {
memset(&sd, 0, sizeof (sd));
saved_errno = errno;
- if (getservbyport_r(port, proto, &s, &sd) != -1) {
+ r = getservbyport_r(port, proto, &s, &sd);
+ if (r == 0)
n = strlcpy(buf, s.s_name, buflen);
- endservent_r(&sd);
- return (n);
- }
+ endservent_r(&sd);
errno = saved_errno;
+ if (r == 0)
+ return (n);
}
r = snprintf(buf, buflen, "%u", ntohs(port));
diff --git a/lib/libc/asr/getnameinfo_async.c b/lib/libc/asr/getnameinfo_async.c
index 13c00f1c7a1..6a77f8ddb7a 100644
--- a/lib/libc/asr/getnameinfo_async.c
+++ b/lib/libc/asr/getnameinfo_async.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getnameinfo_async.c,v 1.14 2019/07/03 03:24:03 deraadt Exp $ */
+/* $OpenBSD: getnameinfo_async.c,v 1.15 2020/12/21 09:40:35 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -213,7 +213,7 @@ _servname(struct asr_query *as)
struct servent_data sd;
int port, r;
char *buf = as->as.ni.servname;
- size_t buflen = as->as.ni.servnamelen;
+ size_t n, buflen = as->as.ni.servnamelen;
if (as->as.ni.servname == NULL || as->as.ni.servnamelen == 0)
return (0);
@@ -225,12 +225,15 @@ _servname(struct asr_query *as)
if (!(as->as.ni.flags & NI_NUMERICSERV)) {
memset(&sd, 0, sizeof (sd));
- if (getservbyport_r(port,
- (as->as.ni.flags & NI_DGRAM) ? "udp" : "tcp",
- &s, &sd) != -1) {
- r = strlcpy(buf, s.s_name, buflen) >= buflen;
- endservent_r(&sd);
- return (r ? -1 : 0);
+ r = getservbyport_r(port, (as->as.ni.flags & NI_DGRAM) ?
+ "udp" : "tcp", &s, &sd);
+ if (r == 0)
+ n = strlcpy(buf, s.s_name, buflen);
+ endservent_r(&sd);
+ if (r == 0) {
+ if (n >= buflen)
+ return (-1);
+ return (0);
}
}