summaryrefslogtreecommitdiff
path: root/src/network/getservbyname_r.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2017-09-06 21:42:15 -0400
committerRich Felker <dalias@aerifal.cx>2017-09-06 21:42:15 -0400
commit565dbee24d4bf55728be1c274fca1e7f3196fd73 (patch)
tree157ebbac897879151a8c14780e682f58b241fb6d /src/network/getservbyname_r.c
parent9e01be6e49b9ae433072207f420ef33c8189eb78 (diff)
downloadmusl-565dbee24d4bf55728be1c274fca1e7f3196fd73.tar.gz
musl-565dbee24d4bf55728be1c274fca1e7f3196fd73.tar.bz2
musl-565dbee24d4bf55728be1c274fca1e7f3196fd73.tar.xz
musl-565dbee24d4bf55728be1c274fca1e7f3196fd73.zip
don't treat numeric port strings as servent records in getservby*()
some applications use getservbyport to find port numbers that are not assigned to a service; if getservbyport always succeeds with a numeric string as the result, they fail to find any available ports. POSIX doesn't seem to mandate the behavior one way or another. it specifies an abstract service database, which an implementation could define to include numeric port strings, but it makes more sense to align behavior with traditional implementations. based on patch by A. Wilcox. the original patch only changed getservbyport[_r]. to maintain a consistent view of the "service database", I have also modified getservbyname[_r] to exclude numeric port strings.
Diffstat (limited to 'src/network/getservbyname_r.c')
-rw-r--r--src/network/getservbyname_r.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/network/getservbyname_r.c b/src/network/getservbyname_r.c
index ad3d6164..cad6317a 100644
--- a/src/network/getservbyname_r.c
+++ b/src/network/getservbyname_r.c
@@ -5,6 +5,7 @@
#include <inttypes.h>
#include <errno.h>
#include <string.h>
+#include <stdlib.h>
#include "lookup.h"
#define ALIGN (sizeof(struct { char a; char *b; }) - sizeof(char *))
@@ -17,6 +18,11 @@ int getservbyname_r(const char *name, const char *prots,
*res = 0;
+ /* Don't treat numeric port number strings as service records. */
+ char *end = "";
+ strtoul(name, &end, 10);
+ if (!*end) return ENOENT;
+
/* Align buffer */
align = -(uintptr_t)buf & ALIGN-1;
if (buflen < 2*sizeof(char *)+align)