Skip to content

Commit dc963f3

Browse files
committed
to exception
1 parent 29d56a5 commit dc963f3

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

ext/snmp/snmp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ static bool snmp_session_init(php_snmp_session **session_p, int version, zend_st
842842
struct sockaddr **res;
843843

844844
if (ZSTR_LEN(hostname) >= MAX_NAME_LEN) {
845-
php_error_docref(NULL, E_WARNING, "hostname length must be lower than %d", MAX_NAME_LEN);
845+
zend_value_error("hostname length must be lower than %d", MAX_NAME_LEN);
846846
return false;
847847
}
848848

@@ -872,7 +872,7 @@ static bool snmp_session_init(php_snmp_session **session_p, int version, zend_st
872872
char *pport = pptr + 2;
873873
tmp_port = atoi(pport);
874874
if (tmp_port < 0 || tmp_port > USHRT_MAX) {
875-
php_error_docref(NULL, E_WARNING, "Remote port must be between 0 and %u", USHRT_MAX);
875+
zend_value_error("remote port must be between 0 and %u", USHRT_MAX);
876876
return false;
877877
}
878878
remote_port = (unsigned short)tmp_port;
@@ -887,7 +887,7 @@ static bool snmp_session_init(php_snmp_session **session_p, int version, zend_st
887887
char *pport = pptr + 1;
888888
tmp_port = atoi(pport);
889889
if (tmp_port < 0 || tmp_port > USHRT_MAX) {
890-
php_error_docref(NULL, E_WARNING, "Remote port must be between 0 and %u", USHRT_MAX);
890+
zend_value_error("remote port must be between 0 and %u", USHRT_MAX);
891891
return false;
892892
}
893893
remote_port = (unsigned short)tmp_port;

ext/snmp/tests/snmp_session_error.phpt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@ require_once(__DIR__.'/skipif.inc');
1313
require_once(__DIR__.'/snmp_include.inc');
1414

1515
$longhostname = str_repeat($hostname4, 1_000_000);
16-
new SNMP(SNMP::VERSION_1, "$hostname4:-1", $community, $timeout, $retries);
17-
new SNMP(SNMP::VERSION_1, "$hostname4:65536", $community, $timeout, $retries);
18-
new SNMP(SNMP::VERSION_1, "$longhostname:$port", $community, $timeout, $retries);
16+
try {
17+
new SNMP(SNMP::VERSION_1, "$hostname4:-1", $community, $timeout, $retries);
18+
} catch (\ValueError $e) {
19+
echo $e->getMessage(), PHP_EOL;
20+
}
21+
try {
22+
new SNMP(SNMP::VERSION_1, "$hostname4:65536", $community, $timeout, $retries);
23+
} catch (\ValueError $e) {
24+
echo $e->getMessage(), PHP_EOL;
25+
}
26+
try {
27+
new SNMP(SNMP::VERSION_1, "$longhostname:$port", $community, $timeout, $retries);
28+
} catch (\ValueError $e) {
29+
echo $e->getMessage(), PHP_EOL;
30+
}
1931
echo "OK";
2032
?>
21-
--EXPECTF--
22-
23-
Warning: SNMP::__construct(): Remote port must be between 0 and 65535 in %s on line %d
24-
25-
Warning: SNMP::__construct(): Remote port must be between 0 and 65535 in %s on line %d
26-
27-
Warning: SNMP::__construct(): hostname length must be lower than 128 in %s on line %d
33+
--EXPECT--
34+
remote port must be between 0 and 65535
35+
remote port must be between 0 and 65535
36+
hostname length must be lower than 128
2837
OK

0 commit comments

Comments
 (0)