[php-src] master: ext/snmp: Fix the hostname and community argument numbers in errors

From: Date: Fri, 20 Nov 2026 08:29:46 +0000
Subject: [php-src] master: ext/snmp: Fix the hostname and community argument numbers in errors
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143959@lists.php.net to get a copy of this message
Author: lacatoire (lacatoire) Committer: David Carlier (devnexen) Date: 2026-11-20T08:17:46Z Commit: https://github.com/php/php-src/commit/25540f7e3455f0f0430ec184129d2a647d390460 Raw diff: https://github.com/php/php-src/commit/25540f7e3455f0f0430ec184129d2a647d390460.diff ext/snmp: Fix the hostname and community argument numbers in errors snmp_session_init() reported the hostname as argument 2 and the community as argument 3, which is only right for SNMP::__construct(). The procedural functions take the hostname first, so snmpget() and the other snmp*_get(), set and walk functions named the wrong parameter. Pass the position of the hostname instead. Close GH-25590 Changed paths: A ext/snmp/tests/snmp_session_init_argument_numbers.phpt M NEWS M ext/snmp/snmp.c Diff: diff --git a/NEWS b/NEWS index 844ee50419fd..cf0cc16f4a19 100644 --- a/NEWS +++ b/NEWS @@ -139,6 +139,8 @@ PHP NEWS - SNMP: . Fixed snmp3_set() citing the wrong argument number for an invalid $timeout or $retries. (lacatoire) + . Fixed the procedural snmp functions citing the wrong argument number for an + invalid $hostname or $community. (lacatoire) - SOAP: . Fixed bug GH-25267 (NULL dereference in to_xml_any() with integer key). diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index 0e9459185c45..da4d3a4734c7 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -829,7 +829,7 @@ static bool php_snmp_parse_oid( /* {{{ snmp_session_init allocates memory for session and session->peername, caller should free it manually using snmp_session_free() and efree() */ -static bool snmp_session_init(php_snmp_session **session_p, int version, zend_string *hostname, zend_string *community, zend_long timeout, zend_long retries, int timeout_argument_offset) +static bool snmp_session_init(php_snmp_session **session_p, int version, zend_string *hostname, zend_string *community, zend_long timeout, zend_long retries, int hostname_argument_offset, int timeout_argument_offset) { php_snmp_session *session; char *pptr, *host_ptr; @@ -844,22 +844,22 @@ static bool snmp_session_init(php_snmp_session **session_p, int version, zend_st ZEND_ASSERT(community != NULL); if (zend_str_has_nul_byte(hostname)) { - zend_argument_value_error(2, "must not contain any null bytes"); + zend_argument_value_error(hostname_argument_offset, "must not contain any null bytes"); return false; } if (ZSTR_LEN(hostname) >= MAX_NAME_LEN) { - zend_argument_value_error(2, "length must be lower than %d", MAX_NAME_LEN); + zend_argument_value_error(hostname_argument_offset, "length must be lower than %d", MAX_NAME_LEN); return false; } if (zend_str_has_nul_byte(community)) { - zend_argument_value_error(3, "must not contain any null bytes"); + zend_argument_value_error(hostname_argument_offset + 1, "must not contain any null bytes"); return false; } if (ZSTR_LEN(community) == 0) { - zend_argument_must_not_be_empty_error(3); + zend_argument_must_not_be_empty_error(hostname_argument_offset + 1); return false; } @@ -901,7 +901,7 @@ static bool snmp_session_init(php_snmp_session **session_p, int version, zend_st char *pport = pptr + 2; tmp_port = atoi(pport); if (tmp_port < 0 || tmp_port > USHRT_MAX) { - zend_argument_value_error(2, "remote port must be between 0 and %u", USHRT_MAX); + zend_argument_value_error(hostname_argument_offset, "remote port must be between 0 and %u", USHRT_MAX); return false; } remote_port = (unsigned short)tmp_port; @@ -916,7 +916,7 @@ static bool snmp_session_init(php_snmp_session **session_p, int version, zend_st char *pport = pptr + 1; tmp_port = atoi(pport); if (tmp_port < 0 || tmp_port > USHRT_MAX) { - zend_argument_value_error(2, "remote port must be between 0 and %u", USHRT_MAX); + zend_argument_value_error(hostname_argument_offset, "remote port must be between 0 and %u", USHRT_MAX); return false; } remote_port = (unsigned short)tmp_port; @@ -1371,7 +1371,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version) } if (session_less_mode) { - if (!snmp_session_init(&session, version, a1, a2, timeout, retries, timeout_argument_offset)) { + if (!snmp_session_init(&session, version, a1, a2, timeout, retries, 1, timeout_argument_offset)) { php_free_objid_query(&objid_query, oid_ht, value_ht, st); snmp_session_free(&session); RETURN_FALSE; @@ -1675,7 +1675,7 @@ PHP_METHOD(SNMP, __construct) snmp_session_free(&(snmp_object->session)); } - if (!snmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries, 4)) { + if (!snmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries, 2, 4)) { return; } snmp_object->max_oids = 0; diff --git a/ext/snmp/tests/snmp_session_init_argument_numbers.phpt b/ext/snmp/tests/snmp_session_init_argument_numbers.phpt new file mode 100644 index 000000000000..0b1e2cae11a2 --- /dev/null +++ b/ext/snmp/tests/snmp_session_init_argument_numbers.phpt @@ -0,0 +1,36 @@ +--TEST-- +Errors of the hostname and the community name the right argument +--EXTENSIONS-- +snmp +--FILE-- +<?php +$oid = '.1.3.6.1.2.1.1.5.0'; +$calls = [ + fn() => snmpget("a\0b", 'public', $oid), + fn() => snmpget(str_repeat('a', 300), 'public', $oid), + fn() => snmpget('127.0.0.1:99999', 'public', $oid), + fn() => snmpget('127.0.0.1', "a\0b", $oid), + fn() => snmpget('127.0.0.1', '', $oid), + fn() => snmp3_get("a\0b", 'user', 'noAuthNoPriv', '', '', '', '', $oid), + fn() => snmp3_get('127.0.0.1', '', 'noAuthNoPriv', '', '', '', '', $oid), + fn() => new SNMP(SNMP::VERSION_2C, "a\0b", 'public'), + fn() => new SNMP(SNMP::VERSION_2C, '127.0.0.1', ''), +]; +foreach ($calls as $call) { + try { + $call(); + } catch (ValueError $e) { + echo $e->getMessage(), "\n"; + } +} +?> +--EXPECT-- +snmpget(): Argument #1 ($hostname) must not contain any null bytes +snmpget(): Argument #1 ($hostname) length must be lower than 128 +snmpget(): Argument #1 ($hostname) remote port must be between 0 and 65535 +snmpget(): Argument #2 ($community) must not contain any null bytes +snmpget(): Argument #2 ($community) must not be empty +snmp3_get(): Argument #1 ($hostname) must not contain any null bytes +snmp3_get(): Argument #2 ($security_name) must not be empty +SNMP::__construct(): Argument #2 ($hostname) must not contain any null bytes +SNMP::__construct(): Argument #3 ($community) must not be empty

« previous php.cvs (#143959) next »