@@ -829,10 +829,33 @@ static bool php_snmp_parse_oid(
829829}
830830/* }}} */
831831
832+ static bool php_snmp_parse_port (const char * port_string , unsigned short * port )
833+ {
834+ unsigned int value = 0 ;
835+
836+ if (* port_string == '\0' ) {
837+ return false;
838+ }
839+
840+ do {
841+ if (!ZEND_IS_DIGIT (* port_string )) {
842+ return false;
843+ }
844+
845+ value = value * 10 + (unsigned int ) (* port_string - '0' );
846+ if (value > USHRT_MAX ) {
847+ return false;
848+ }
849+ } while (* ++ port_string != '\0' );
850+
851+ * port = (unsigned short ) value ;
852+ return true;
853+ }
854+
832855/* {{{ netsnmp_session_init
833856 allocates memory for session and session->peername, caller should free it manually using netsnmp_session_free() and efree()
834857*/
835- static bool netsnmp_session_init (php_snmp_session * * session_p , int version , zend_string * hostname , zend_string * community , int timeout , int retries )
858+ static bool netsnmp_session_init (php_snmp_session * * session_p , int version , zend_string * hostname , zend_string * community , int timeout , int retries , uint32_t hostname_arg_num )
836859{
837860 php_snmp_session * session ;
838861 char * pptr , * host_ptr ;
@@ -841,7 +864,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
841864 struct sockaddr * * psal ;
842865 struct sockaddr * * res ;
843866 // TODO: Do not strip and re-add the port in peername?
844- unsigned remote_port = SNMP_PORT ;
867+ unsigned short remote_port = SNMP_PORT ;
845868
846869 * session_p = (php_snmp_session * )emalloc (sizeof (php_snmp_session ));
847870 session = * session_p ;
@@ -862,7 +885,10 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
862885 host_ptr ++ ;
863886 if ((pptr = strchr (host_ptr , ']' ))) {
864887 if (pptr [1 ] == ':' ) {
865- remote_port = atoi (pptr + 2 );
888+ if (!php_snmp_parse_port (pptr + 2 , & remote_port )) {
889+ zend_argument_value_error (hostname_arg_num , "remote port must be between 0 and %u" , (unsigned int ) USHRT_MAX );
890+ return false;
891+ }
866892 }
867893 * pptr = '\0' ;
868894 } else {
@@ -871,7 +897,10 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
871897 }
872898 } else { /* IPv4 address */
873899 if ((pptr = strchr (host_ptr , ':' ))) {
874- remote_port = atoi (pptr + 1 );
900+ if (!php_snmp_parse_port (pptr + 1 , & remote_port )) {
901+ zend_argument_value_error (hostname_arg_num , "remote port must be between 0 and %u" , (unsigned int ) USHRT_MAX );
902+ return false;
903+ }
875904 * pptr = '\0' ;
876905 }
877906 }
@@ -921,7 +950,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
921950 if (remote_port != SNMP_PORT ) {
922951 size_t peername_length = strlen (session -> peername );
923952 pptr = session -> peername + peername_length ;
924- snprintf (pptr , MAX_NAME_LEN - peername_length , ":%d" , remote_port );
953+ snprintf (pptr , MAX_NAME_LEN - peername_length , ":%u" , ( unsigned int ) remote_port );
925954 }
926955
927956 php_network_freeaddresses (psal );
@@ -1314,7 +1343,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
13141343 }
13151344
13161345 if (session_less_mode ) {
1317- if (!netsnmp_session_init (& session , version , a1 , a2 , timeout , retries )) {
1346+ if (!netsnmp_session_init (& session , version , a1 , a2 , timeout , retries , 1 )) {
13181347 php_free_objid_query (& objid_query , oid_ht , value_ht , st );
13191348 netsnmp_session_free (& session );
13201349 RETURN_FALSE ;
@@ -1618,7 +1647,7 @@ PHP_METHOD(SNMP, __construct)
16181647 netsnmp_session_free (& (snmp_object -> session ));
16191648 }
16201649
1621- if (!netsnmp_session_init (& (snmp_object -> session ), version , a1 , a2 , timeout , retries )) {
1650+ if (!netsnmp_session_init (& (snmp_object -> session ), version , a1 , a2 , timeout , retries , 2 )) {
16221651 return ;
16231652 }
16241653 snmp_object -> max_oids = 0 ;
0 commit comments