Skip to content

Commit 729e45b

Browse files
ext/snmp: add test for GH-21341 suffix-as-keys snprintf loop
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
1 parent 9b32d54 commit 729e45b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

ext/snmp/tests/gh21341.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
GH-21341: suffix-as-keys walk uses offset-tracked snprintf (no strcat overflow)
3+
--EXTENSIONS--
4+
snmp
5+
--SKIPIF--
6+
<?php
7+
require_once(__DIR__.'/skipif.inc');
8+
if (getenv('SKIP_ASAN')) die('skip Timeouts under ASAN');
9+
?>
10+
--FILE--
11+
<?php
12+
require_once(__DIR__.'/snmp_include.inc');
13+
14+
/* Exercise the suffix-as-keys code path (SNMP_USE_SUFFIX_AS_KEYS |
15+
* SNMP_CMD_WALK). The walk OID is chosen so the returned subidentifiers
16+
* form a multi-component suffix, driving multiple iterations of the
17+
* snprintf loop that GH-21341 replaced the strcat loop with. */
18+
$session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries);
19+
$result = $session->walk('.1.3.6.1.2.1.1', /* suffix_as_keys */ true);
20+
21+
var_dump(is_array($result));
22+
var_dump(count($result) > 0);
23+
24+
/* Every key must be a bare dotted-numeric suffix, not a full OID.
25+
* Full OIDs start with '.'; suffixes do not. */
26+
$bad = array_filter(array_keys($result), static function (string $k): bool {
27+
return $k[0] === '.' || !preg_match('/^\d[\d.]*$/', $k);
28+
});
29+
var_dump(count($bad) === 0);
30+
31+
var_dump($session->close());
32+
?>
33+
--EXPECT--
34+
bool(true)
35+
bool(true)
36+
bool(true)
37+
bool(true)

0 commit comments

Comments
 (0)