Skip to content

Commit e7dcd6a

Browse files
committed
Fix (attempt) GH-20752: netsnmp_session_init() without system ipv6.
- opening up building peername to non ipv6 supported systems. - while at it, inet_ntop should not be called with a query size as large as MAX_NAME_LEN/MAX_OID_LEN but INET6_ADDRSTRLEN max. - checking in case of non ipv6 support (AF_INET6 can be still defined in this case).
1 parent d9cbc31 commit e7dcd6a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

ext/snmp/snmp.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,9 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
842842
struct sockaddr **res;
843843
// TODO: Do not strip and re-add the port in peername?
844844
unsigned remote_port = SNMP_PORT;
845+
#if defined(HAVE_GETADDRINFO)
846+
char name[INET6_ADDRSTRLEN];
847+
#endif
845848

846849
*session_p = (php_snmp_session *)emalloc(sizeof(php_snmp_session));
847850
session = *session_p;
@@ -888,18 +891,17 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
888891
res = psal;
889892
while (n-- > 0) {
890893
pptr = session->peername;
891-
#if defined(HAVE_GETADDRINFO) && defined(HAVE_IPV6)
894+
#if defined(HAVE_GETADDRINFO)
892895
if (force_ipv6 && (*res)->sa_family != AF_INET6) {
893896
res++;
894897
continue;
895898
}
896899
if ((*res)->sa_family == AF_INET6) {
897-
strcpy(session->peername, "udp6:[");
898-
pptr = session->peername + strlen(session->peername);
899-
inet_ntop((*res)->sa_family, &(((struct sockaddr_in6*)(*res))->sin6_addr), pptr, MAX_NAME_LEN);
900-
strcat(pptr, "]");
900+
if (inet_ntop((*res)->sa_family, &(((struct sockaddr_in6*)(*res))->sin6_addr), name, sizeof(name))) {
901+
snprintf(pptr, MAX_NAME_LEN, "udp6:[%s]", name);
902+
}
901903
} else if ((*res)->sa_family == AF_INET) {
902-
inet_ntop((*res)->sa_family, &(((struct sockaddr_in*)(*res))->sin_addr), pptr, MAX_NAME_LEN);
904+
inet_ntop((*res)->sa_family, &(((struct sockaddr_in*)(*res))->sin_addr), pptr, INET_ADDRSTRLEN);
903905
} else {
904906
res++;
905907
continue;

0 commit comments

Comments
 (0)