Skip to content

Commit 982b362

Browse files
committed
Promote warnings to Error in SNMP extension
1 parent 67d21bf commit 982b362

10 files changed

+384
-276
lines changed

ext/snmp/snmp.c

+35-38
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,6 @@ static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
659659
*
660660
* OID parser (and type, value for SNMP_SET command)
661661
*/
662-
663662
static int php_snmp_parse_oid(
664663
zval *object, int st, struct objid_query *objid_query, zend_string *oid_str, HashTable *oid_ht,
665664
zend_string *type_str, HashTable *type_ht, zend_string *value_str, HashTable *value_ht
@@ -676,23 +675,23 @@ static int php_snmp_parse_oid(
676675
if (st & SNMP_CMD_SET) {
677676
if (type_str && value_str) {
678677
if (ZSTR_LEN(type_str) != 1) {
679-
php_error_docref(NULL, E_WARNING, "Bogus type '%s', should be single char, got %zu", ZSTR_VAL(type_str), ZSTR_LEN(type_str));
678+
zend_value_error("Type must be a character");
680679
efree(objid_query->vars);
681680
return FALSE;
682681
}
683682
pptr = ZSTR_VAL(type_str);
684683
objid_query->vars[objid_query->count].type = *pptr;
685684
objid_query->vars[objid_query->count].value = ZSTR_VAL(value_str);
686685
} else {
687-
php_error_docref(NULL, E_WARNING, "Single objid and multiple type or values are not supported");
686+
zend_value_error("Type must be a character when Object ID is a string");
688687
efree(objid_query->vars);
689688
return FALSE;
690689
}
691690
}
692691
objid_query->count++;
693692
} else if (oid_ht) { /* we got objid array */
694693
if (zend_hash_num_elements(oid_ht) == 0) {
695-
php_error_docref(NULL, E_WARNING, "Got empty OID array");
694+
zend_value_error("Object ID array cannot be empty");
696695
return FALSE;
697696
}
698697
objid_query->vars = (snmpobjarg *)safe_emalloc(sizeof(snmpobjarg), zend_hash_num_elements(oid_ht), 0);
@@ -715,7 +714,7 @@ static int php_snmp_parse_oid(
715714
if (idx_type < type_ht->nNumUsed) {
716715
convert_to_string_ex(tmp_type);
717716
if (Z_STRLEN_P(tmp_type) != 1) {
718-
php_error_docref(NULL, E_WARNING, "'%s': bogus type '%s', should be single char, got %zu", Z_STRVAL_P(tmp_oid), Z_STRVAL_P(tmp_type), Z_STRLEN_P(tmp_type));
717+
zend_value_error("Type must be a character");
719718
efree(objid_query->vars);
720719
return FALSE;
721720
}
@@ -916,6 +915,7 @@ static int netsnmp_session_set_sec_level(struct snmp_session *s, char *level)
916915
} else if (!strcasecmp(level, "authPriv") || !strcasecmp(level, "ap")) {
917916
s->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
918917
} else {
918+
zend_value_error("Security level must be \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"");
919919
return (-1);
920920
}
921921
return (0);
@@ -933,7 +933,7 @@ static int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot)
933933
s->securityAuthProto = usmHMACSHA1AuthProtocol;
934934
s->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
935935
} else {
936-
php_error_docref(NULL, E_WARNING, "Unknown authentication protocol '%s'", prot);
936+
zend_value_error("Authentication protocol mus be either MD5 or SHA");
937937
return (-1);
938938
}
939939
return (0);
@@ -953,7 +953,11 @@ static int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot)
953953
s->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
954954
#endif
955955
} else {
956-
php_error_docref(NULL, E_WARNING, "Unknown security protocol '%s'", prot);
956+
zend_value_error("Security protocol must be DES"
957+
#ifdef HAVE_AES
958+
", AES128, or AES"
959+
#endif
960+
);
957961
return (-1);
958962
}
959963
return (0);
@@ -987,7 +991,7 @@ static int netsnmp_session_gen_sec_key(struct snmp_session *s, char *pass)
987991
(u_char *)pass, strlen(pass),
988992
s->securityPrivKey, &(s->securityPrivKeyLen)))) {
989993
php_error_docref(NULL, E_WARNING, "Error generating a key for privacy pass phrase '%s': %s", pass, snmp_api_errstring(snmp_errno));
990-
return (-2);
994+
return (-1);
991995
}
992996
return (0);
993997
}
@@ -1001,6 +1005,7 @@ static int netsnmp_session_set_contextEngineID(struct snmp_session *s, char * co
10011005
u_char *ebuf = (u_char *) emalloc(ebuf_len);
10021006

10031007
if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1, contextEngineID)) {
1008+
// TODO Promote to Error?
10041009
php_error_docref(NULL, E_WARNING, "Bad engine ID value '%s'", contextEngineID);
10051010
efree(ebuf);
10061011
return (-1);
@@ -1023,15 +1028,15 @@ static int netsnmp_session_set_security(struct snmp_session *session, char *sec_
10231028

10241029
/* Setting the security level. */
10251030
if (netsnmp_session_set_sec_level(session, sec_level)) {
1026-
php_error_docref(NULL, E_WARNING, "Invalid security level '%s'", sec_level);
1031+
/* ValueError already generated, just bail out */
10271032
return (-1);
10281033
}
10291034

10301035
if (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
10311036

10321037
/* Setting the authentication protocol. */
10331038
if (netsnmp_session_set_auth_protocol(session, auth_protocol)) {
1034-
/* Warning message sent already, just bail out */
1039+
/* ValueError already generated, just bail out */
10351040
return (-1);
10361041
}
10371042

@@ -1044,7 +1049,7 @@ static int netsnmp_session_set_security(struct snmp_session *session, char *sec_
10441049
if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
10451050
/* Setting the security protocol. */
10461051
if (netsnmp_session_set_sec_protocol(session, priv_protocol)) {
1047-
/* Warning message sent already, just bail out */
1052+
/* ValueError already generated, just bail out */
10481053
return (-1);
10491054
}
10501055

@@ -1220,9 +1225,9 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12201225
snmp_object = Z_SNMP_P(object);
12211226
session = snmp_object->session;
12221227
if (!session) {
1223-
php_error_docref(NULL, E_WARNING, "Invalid or uninitialized SNMP object");
1228+
zend_throw_error(NULL, "Invalid or uninitialized SNMP object");
12241229
efree(objid_query.vars);
1225-
RETURN_FALSE;
1230+
RETURN_THROWS();
12261231
}
12271232

12281233
if (snmp_object->max_oids > 0) {
@@ -1351,11 +1356,9 @@ PHP_FUNCTION(snmp_set_oid_output_format)
13511356
case NETSNMP_OID_OUTPUT_NONE:
13521357
netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, a1);
13531358
RETURN_TRUE;
1354-
break;
13551359
default:
1356-
php_error_docref(NULL, E_WARNING, "Unknown SNMP output print format '%d'", (int) a1);
1357-
RETURN_FALSE;
1358-
break;
1360+
zend_argument_value_error(1, "must be one of SNMP_OID_OUTPUT_*");
1361+
RETURN_THROWS();
13591362
}
13601363
}
13611364
/* }}} */
@@ -1443,8 +1446,8 @@ PHP_FUNCTION(snmp_set_valueretrieval)
14431446
SNMP_G(valueretrieval) = method;
14441447
RETURN_TRUE;
14451448
} else {
1446-
php_error_docref(NULL, E_WARNING, "Unknown SNMP value retrieval method '" ZEND_LONG_FMT "'", method);
1447-
RETURN_FALSE;
1449+
zend_argument_value_error(1, "must be SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, or SNMP_VALUE_OBJECT");
1450+
RETURN_THROWS();
14481451
}
14491452
}
14501453
/* }}} */
@@ -1827,48 +1830,46 @@ PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)
18271830
/* {{{ */
18281831
static int php_snmp_write_info(php_snmp_object *snmp_object, zval *newval)
18291832
{
1830-
php_error_docref(NULL, E_WARNING, "info property is read-only");
1833+
zend_throw_error(NULL, "info property is read-only");
18311834
return FAILURE;
18321835
}
18331836
/* }}} */
18341837

18351838
/* {{{ */
18361839
static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval)
18371840
{
1838-
int ret = SUCCESS;
18391841
zend_long lval;
18401842

18411843
if (Z_TYPE_P(newval) == IS_NULL) {
18421844
snmp_object->max_oids = 0;
1843-
return ret;
1845+
return SUCCESS;
18441846
}
18451847

18461848
lval = zval_get_long(newval);
18471849

1848-
if (lval > 0) {
1849-
snmp_object->max_oids = lval;
1850-
} else {
1851-
php_error_docref(NULL, E_WARNING, "max_oids should be positive integer or NULL, got " ZEND_LONG_FMT, lval);
1850+
if (lval <= 0) {
1851+
zend_value_error("max_oids must be greater than 0 or NULL");
1852+
return FAILURE;
18521853
}
1854+
snmp_object->max_oids = lval;
18531855

1854-
return ret;
1856+
return SUCCESS;
18551857
}
18561858
/* }}} */
18571859

18581860
/* {{{ */
18591861
static int php_snmp_write_valueretrieval(php_snmp_object *snmp_object, zval *newval)
18601862
{
1861-
int ret = SUCCESS;
18621863
zend_long lval = zval_get_long(newval);
18631864

18641865
if (lval >= 0 && lval <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
18651866
snmp_object->valueretrieval = lval;
18661867
} else {
1867-
php_error_docref(NULL, E_WARNING, "Unknown SNMP value retrieval method '" ZEND_LONG_FMT "'", lval);
1868-
ret = FAILURE;
1868+
zend_value_error("SNMP retrieval method must be SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, or SNMP_VALUE_OBJECT");
1869+
return FAILURE;
18691870
}
18701871

1871-
return ret;
1872+
return SUCCESS;
18721873
}
18731874
/* }}} */
18741875

@@ -1892,7 +1893,6 @@ PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(oid_increasing_check)
18921893
/* {{{ */
18931894
static int php_snmp_write_oid_output_format(php_snmp_object *snmp_object, zval *newval)
18941895
{
1895-
int ret = SUCCESS;
18961896
zend_long lval = zval_get_long(newval);
18971897

18981898
switch(lval) {
@@ -1903,14 +1903,11 @@ static int php_snmp_write_oid_output_format(php_snmp_object *snmp_object, zval *
19031903
case NETSNMP_OID_OUTPUT_UCD:
19041904
case NETSNMP_OID_OUTPUT_NONE:
19051905
snmp_object->oid_output_format = lval;
1906-
break;
1906+
return SUCCESS;
19071907
default:
1908-
php_error_docref(NULL, E_WARNING, "Unknown SNMP output print format '" ZEND_LONG_FMT "'", lval);
1909-
ret = FAILURE;
1910-
break;
1908+
zend_value_error("SNMP output print format must be one of SNMP_OID_OUTPUT_*");
1909+
return FAILURE;
19111910
}
1912-
1913-
return ret;
19141911
}
19151912
/* }}} */
19161913

ext/snmp/tests/snmp-object-error.phpt

+27-17
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,35 @@ var_dump($session->close());
5454

5555
echo "Open normal session\n";
5656
$session = new SNMP(SNMP::VERSION_3, $hostname, $user_noauth, $timeout, $retries);
57-
$session->valueretrieval = 67;
58-
var_dump($session->valueretrieval);
57+
try {
58+
$session->valueretrieval = 67;
59+
var_dump($session->valueretrieval);
60+
} catch (\ValueError $e) {
61+
echo $e->getMessage() . \PHP_EOL;
62+
}
5963
echo "Closing session\n";
6064
var_dump($session->close());
61-
var_dump($session->get('.1.3.6.1.2.1.1.1.0'));
62-
var_dump($session->close());
65+
66+
try {
67+
var_dump($session->get('.1.3.6.1.2.1.1.1.0'));
68+
var_dump($session->close());
69+
} catch (\Error $e) {
70+
echo $e->getMessage() . \PHP_EOL;
71+
}
6372

6473
$session = new SNMP(SNMP::VERSION_2c, $hostname, $community, $timeout, $retries);
6574

6675
var_dump($session->max_oids);
67-
$session->max_oids = "ttt";
68-
$session->max_oids = 0;
76+
try {
77+
$session->max_oids = "ttt";
78+
} catch (\ValueError $e) {
79+
echo $e->getMessage() . \PHP_EOL;
80+
}
81+
try {
82+
$session->max_oids = 0;
83+
} catch (\ValueError $e) {
84+
echo $e->getMessage() . \PHP_EOL;
85+
}
6986
var_dump($session->max_oids);
7087
?>
7188
--EXPECTF--
@@ -81,18 +98,11 @@ int(32)
8198
string(46) "Invalid object identifier: .1.3.6.1.2.1.1.1..0"
8299
bool(true)
83100
Open normal session
84-
85-
Warning: main(): Unknown SNMP value retrieval method '67' in %s on line %d
86-
int(%d)
101+
SNMP retrieval method must be SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, or SNMP_VALUE_OBJECT
87102
Closing session
88103
bool(true)
89-
90-
Warning: SNMP::get(): Invalid or uninitialized SNMP object in %s on line %d
91-
bool(false)
92-
bool(true)
104+
Invalid or uninitialized SNMP object
93105
NULL
94-
95-
Warning: main(): max_oids should be positive integer or NULL, got 0 in %s on line %d
96-
97-
Warning: main(): max_oids should be positive integer or NULL, got 0 in %s on line %d
106+
max_oids must be greater than 0 or NULL
107+
max_oids must be greater than 0 or NULL
98108
NULL

ext/snmp/tests/snmp-object-properties.phpt

+21-23
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,24 @@ $param = 'there is no such parameter';
5454
var_dump($session->$param);
5555
var_dump(property_exists($session, $param));
5656

57-
$session->valueretrieval = 67;
58-
var_dump($session->valueretrieval);
59-
$session->oid_output_format = 78;
60-
var_dump($session->oid_output_format);
61-
62-
$session->info = array("blah" => 2);
63-
var_dump($session->info);
57+
try {
58+
$session->valueretrieval = 67;
59+
var_dump($session->valueretrieval);
60+
} catch (\ValueError $e) {
61+
echo $e->getMessage() . \PHP_EOL;
62+
}
63+
try {
64+
$session->oid_output_format = 78;
65+
var_dump($session->oid_output_format);
66+
} catch (\ValueError $e) {
67+
echo $e->getMessage() . \PHP_EOL;
68+
}
69+
try {
70+
$session->info = array("blah" => 2);
71+
var_dump($session->info);
72+
} catch (\Error $e) {
73+
echo $e->getMessage() . \PHP_EOL;
74+
}
6475

6576
$session->max_oids = NULL;
6677
var_dump($session->max_oids);
@@ -179,20 +190,7 @@ Error handling
179190
Warning: Undefined property: SNMP::$there is no such parameter in %s on line %d
180191
NULL
181192
bool(false)
182-
183-
Warning: main(): Unknown SNMP value retrieval method '67' in %s on line %d
184-
int(1)
185-
186-
Warning: main(): Unknown SNMP output print format '78' in %s on line %d
187-
int(3)
188-
189-
Warning: main(): info property is read-only in %s on line %d
190-
array(3) {
191-
["hostname"]=>
192-
string(%d) "%s"
193-
["timeout"]=>
194-
int(%i)
195-
["retries"]=>
196-
int(%d)
197-
}
193+
SNMP retrieval method must be SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, or SNMP_VALUE_OBJECT
194+
SNMP output print format must be one of SNMP_OID_OUTPUT_*
195+
info property is read-only
198196
NULL

0 commit comments

Comments
 (0)