don't treat numeric port strings as servent records in getservby*()
[musl] / src / network / getservbyport_r.c
index a0a7cae..b7f21c6 100644 (file)
@@ -5,6 +5,7 @@
 #include <inttypes.h>
 #include <errno.h>
 #include <string.h>
+#include <stdlib.h>
 
 int getservbyport_r(int port, const char *prots,
        struct servent *se, char *buf, size_t buflen, struct servent **res)
@@ -20,6 +21,7 @@ int getservbyport_r(int port, const char *prots,
                if (r) r = getservbyport_r(port, "udp", se, buf, buflen, res);
                return r;
        }
+       *res = 0;
 
        /* Align buffer */
        i = (uintptr_t)buf & sizeof(char *)-1;
@@ -50,6 +52,9 @@ int getservbyport_r(int port, const char *prots,
                break;
        }
 
+       /* A numeric port string is not a service record. */
+       if (strtol(buf, 0, 10)==ntohs(port)) return ENOENT;
+
        *res = se;
        return 0;
 }