Skip to content

Commit 66e090a

Browse files
committed
Fix GH-17330: SNMP::setSecurity segfaults when object had been closed.
having new macro when the workflow needs to deal with an existing SNMP session.
1 parent 9999a5b commit 66e090a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

ext/snmp/snmp.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ typedef struct snmp_session php_snmp_session;
8787
} \
8888
}
8989

90+
#define PHP_SNMP_FETCH_OBJECT \
91+
snmp_object = Z_SNMP_P(object); \
92+
if (!snmp_object->session) { \
93+
zend_throw_error(NULL, "Invalid or uninitialized SNMP object"); \
94+
RETURN_THROWS(); \
95+
}
96+
9097
ZEND_DECLARE_MODULE_GLOBALS(snmp)
9198
static PHP_GINIT_FUNCTION(snmp);
9299

@@ -1612,6 +1619,8 @@ PHP_METHOD(SNMP, close)
16121619
php_snmp_object *snmp_object;
16131620
zval *object = ZEND_THIS;
16141621

1622+
// Possibly using fetching object to avoid closing pointlessly,
1623+
// albeit netsnmp_session_free already checks the session (master?) ?
16151624
snmp_object = Z_SNMP_P(object);
16161625

16171626
if (zend_parse_parameters_none() == FAILURE) {
@@ -1659,7 +1668,7 @@ PHP_METHOD(SNMP, setSecurity)
16591668
zval *object = ZEND_THIS;
16601669
zend_string *a1 = NULL, *a2 = NULL, *a3 = NULL, *a4 = NULL, *a5 = NULL, *a6 = NULL, *a7 = NULL;
16611670

1662-
snmp_object = Z_SNMP_P(object);
1671+
PHP_SNMP_FETCH_OBJECT
16631672

16641673
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|SSSSSS", &a1, &a2, &a3, &a4,&a5, &a6, &a7) == FAILURE) {
16651674
RETURN_THROWS();

ext/snmp/tests/gh17330.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
SNMP::setSecurity() segfault when the object had been closed.
3+
--EXTENSIONS--
4+
snmp
5+
--CREDITS--
6+
YuanchengJiang
7+
--FILE--
8+
<?php
9+
$session = new SNMP(SNMP::VERSION_2c, "localhost", 'timeout_community_432');
10+
$session->close();
11+
try {
12+
$session->setSecurity('authPriv', 'MD5', '', 'AES', '');
13+
} catch(Error $e) {
14+
echo $e->getMessage();
15+
}
16+
?>
17+
--EXPECT--
18+
Invalid or uninitialized SNMP object

0 commit comments

Comments
 (0)