From 9e277c6c500e3e93cd5e3b091132b5f2f069df2d Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 29 Mar 2020 16:27:56 +0800 Subject: [PATCH] libc/netdb: Return null alias for getservbyport_r/getservbyname_r Signed-off-by: Xiang Xiao Change-Id: I82728a8ea9a5136e0154cd67ec8de70a4e0ac6ed --- libs/libc/netdb/lib_getservbyname.c | 3 +-- libs/libc/netdb/lib_getservbynamer.c | 26 ++++---------------------- libs/libc/netdb/lib_getservbyport.c | 3 +-- libs/libc/netdb/lib_getservbyportr.c | 16 +++------------- 4 files changed, 9 insertions(+), 39 deletions(-) diff --git a/libs/libc/netdb/lib_getservbyname.c b/libs/libc/netdb/lib_getservbyname.c index 1bcf78b1a33..697920ddb92 100644 --- a/libs/libc/netdb/lib_getservbyname.c +++ b/libs/libc/netdb/lib_getservbyname.c @@ -54,11 +54,10 @@ FAR struct servent *getservbyname(FAR const char *name, FAR const char *proto) { static struct servent ent; - static char *buf[2]; struct servent *res; int ret; - ret = getservbyname_r(name, proto, &ent, (void *)buf, sizeof buf, &res); + ret = getservbyname_r(name, proto, &ent, NULL, 0, &res); return (ret != OK) ? NULL : res; } diff --git a/libs/libc/netdb/lib_getservbynamer.c b/libs/libc/netdb/lib_getservbynamer.c index 56540cd219f..e97f2786de6 100644 --- a/libs/libc/netdb/lib_getservbynamer.c +++ b/libs/libc/netdb/lib_getservbynamer.c @@ -78,32 +78,16 @@ int getservbyname_r(FAR const char *name, FAR const char *proto, FAR struct servent *result_buf, FAR char *buf, size_t buflen, FAR struct servent **result) { - char *end = ""; int protocol; int i; - DEBUGASSERT(name != NULL && buf != NULL); + DEBUGASSERT(name != NULL); DEBUGASSERT(result_buf != NULL && result != NULL); /* Linux man page says result must be NULL in case of failure. */ *result = NULL; - /* We need space for two pointers for hostalias strings. */ - - if (buflen < 2 * sizeof(char *)) - { - return ERANGE; - } - - /* Numeric port number strings are not service records. */ - - strtoul(name, &end, 10); - if (*end == '\0') - { - return ENOENT; - } - if (proto == NULL) { protocol = 0; @@ -126,11 +110,9 @@ int getservbyname_r(FAR const char *name, FAR const char *proto, if (strcmp(name, g_services_db[i].s_name) == 0 && (protocol == 0 || protocol == g_services_db[i].s_protocol)) { - result_buf->s_name = (char *)name; - result_buf->s_aliases = (void *)buf; - result_buf->s_aliases[0] = (char *)name; - result_buf->s_aliases[1] = NULL; - result_buf->s_port = HTONS(g_services_db[i].s_port); + result_buf->s_name = (FAR char *)name; + result_buf->s_aliases = NULL; + result_buf->s_port = htons(g_services_db[i].s_port); if (g_services_db[i].s_protocol == IPPROTO_TCP) { diff --git a/libs/libc/netdb/lib_getservbyport.c b/libs/libc/netdb/lib_getservbyport.c index 59c4e4a96e3..b0ecf5ae7d5 100644 --- a/libs/libc/netdb/lib_getservbyport.c +++ b/libs/libc/netdb/lib_getservbyport.c @@ -58,11 +58,10 @@ FAR struct servent *getservbyport(int port, FAR const char *proto) { static struct servent ent; - static FAR char *buf[2]; struct servent *res; int ret; - ret = getservbyport_r(port, proto, &ent, (FAR void *)buf, sizeof buf, &res); + ret = getservbyport_r(port, proto, &ent, NULL, 0, &res); return (ret != OK) ? NULL : res; } diff --git a/libs/libc/netdb/lib_getservbyportr.c b/libs/libc/netdb/lib_getservbyportr.c index 233254c9f34..af506ce40e2 100644 --- a/libs/libc/netdb/lib_getservbyportr.c +++ b/libs/libc/netdb/lib_getservbyportr.c @@ -70,20 +70,12 @@ int getservbyport_r(int port, FAR const char *proto, int protocol; int i; - DEBUGASSERT(buf != NULL); DEBUGASSERT(result_buf != NULL && result != NULL); /* Linux man page says result must be NULL in case of failure. */ *result = NULL; - /* We need space for two pointers for hostalias strings. */ - - if (buflen < 2 * sizeof(char *)) - { - return ERANGE; - } - if (proto == NULL) { protocol = 0; @@ -106,11 +98,9 @@ int getservbyport_r(int port, FAR const char *proto, if (port == g_services_db[i].s_port && (protocol == 0 || protocol == g_services_db[i].s_protocol)) { - result_buf->s_name = (char *)g_services_db[i].s_name; - result_buf->s_aliases = (void *)buf; - result_buf->s_aliases[0] = (char *)g_services_db[i].s_name; - result_buf->s_aliases[1] = NULL; - result_buf->s_port = HTONS(g_services_db[i].s_port); + result_buf->s_name = (FAR char *)g_services_db[i].s_name; + result_buf->s_aliases = NULL; + result_buf->s_port = htons(port); if (g_services_db[i].s_protocol == IPPROTO_TCP) {