From 1b9b3864b6625046510fd7d14e091da11fe7b919 Mon Sep 17 00:00:00 2001 From: ahgamut <41098605+ahgamut@users.noreply.github.com> Date: Sun, 4 Jul 2021 06:28:32 +0530 Subject: [PATCH] fix failure edge case and PATH_MAX --- libc/dns/getservbyname.c | 7 +++++-- libc/dns/getservbyport.c | 6 ++++-- libc/dns/servicestxt.c | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/libc/dns/getservbyname.c b/libc/dns/getservbyname.c index 39e6d0484..ce368fc7b 100644 --- a/libc/dns/getservbyname.c +++ b/libc/dns/getservbyname.c @@ -46,7 +46,11 @@ struct servent *getservbyname(const char *name, const char *proto) { } p = LookupServicesByName(name, &localproto); - if (p == -1) return NULL; + if (p == -1) { + // localproto got alloc'd during the lookup? + if (!proto && localproto != proto) free(localproto); + return NULL; + } ptr0->s_port = p; if (ptr0->s_name) free(ptr0->s_name); @@ -55,7 +59,6 @@ struct servent *getservbyname(const char *name, const char *proto) { if (ptr0->s_proto) free(ptr0->s_proto); ptr0->s_proto = strdup(localproto); - // localproto got alloc'd during the lookup if (!proto && localproto != proto) free(localproto); return ptr0; diff --git a/libc/dns/getservbyport.c b/libc/dns/getservbyport.c index d80a56418..b61f8d1bb 100644 --- a/libc/dns/getservbyport.c +++ b/libc/dns/getservbyport.c @@ -44,8 +44,11 @@ struct servent *getservbyport(int port, const char *proto) { ptr1 = &se1; } - if (LookupServicesByPort(port, &localproto, name, sizeof(name)) == -1) + if (LookupServicesByPort(port, &localproto, name, sizeof(name)) == -1) { + // localproto got alloc'd during the lookup? + if (!proto && localproto != proto) free(localproto); return NULL; + } ptr1->s_port = port; if (ptr1->s_name) free(ptr1->s_name); @@ -54,7 +57,6 @@ struct servent *getservbyport(int port, const char *proto) { if (ptr1->s_proto) free(ptr1->s_proto); ptr1->s_proto = strdup(localproto); - // localproto got alloc'd during the lookup if (!proto && localproto != proto) free(localproto); return ptr1; diff --git a/libc/dns/servicestxt.c b/libc/dns/servicestxt.c index 0d4e25f9e..539998546 100644 --- a/libc/dns/servicestxt.c +++ b/libc/dns/servicestxt.c @@ -75,7 +75,7 @@ int LookupServicesByPort(const int servport, char **servproto, char *buf, size_t bufsize) { FILE *f; char *line; - char pathbuf[512]; + char pathbuf[PATH_MAX]; const char *path; size_t linesize; int count, found; @@ -138,7 +138,7 @@ int LookupServicesByPort(const int servport, char **servproto, char *buf, int LookupServicesByName(const char *servname, char **servproto) { FILE *f; char *line; - char pathbuf[512]; + char pathbuf[PATH_MAX]; const char *path; size_t linesize; int count, found, result;