Skip to content

Commit b1090b4

Browse files
committed
feedback 2
1 parent 37dd21c commit b1090b4

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

ext/ftp/php_ftp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,18 @@ PHP_FUNCTION(ftp_connect)
158158
RETURN_THROWS();
159159
}
160160

161+
const zend_long timeoutmax = (zend_long)((double) PHP_TIMEOUT_ULL_MAX / 1000000.0);
162+
161163
if (timeout_sec <= 0) {
162164
zend_argument_value_error(3, "must be greater than 0");
163165
RETURN_THROWS();
164166
}
165167

168+
if (timeout_sec >= timeoutmax) {
169+
zend_argument_value_error(3, "must be less than " ZEND_LONG_FMT, timeoutmax);
170+
RETURN_THROWS();
171+
}
172+
166173
/* connect */
167174
if (!(ftp = ftp_open(host, (short)port, timeout_sec))) {
168175
RETURN_FALSE;

ext/ftp/tests/gh20601.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ try {
1515
echo $e->getMessage();
1616
}
1717
?>
18-
--EXPECT--
19-
timeout value overflow
18+
--EXPECTF--
19+
ftp_connect(): Argument #3 ($timeout) must be less than %d

main/network.c

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -315,24 +315,18 @@ static inline void sub_times(struct timeval a, struct timeval b, struct timeval
315315
}
316316
}
317317

318-
static inline zend_result php_network_set_limit_time(struct timeval *limit_time,
318+
static inline void php_network_set_limit_time(struct timeval *limit_time,
319319
struct timeval *timeout)
320320
{
321321
gettimeofday(limit_time, NULL);
322322
const double timeoutmax = (double) PHP_TIMEOUT_ULL_MAX / 1000000.0;
323-
324-
if (limit_time->tv_sec >= (timeoutmax - timeout->tv_sec)) {
325-
zend_value_error("timeout value overflow");
326-
return FAILURE;
327-
}
328-
323+
ZEND_ASSERT(limit_time->tv_sec < (timeoutmax - timeout->tv_sec));
329324
limit_time->tv_sec += timeout->tv_sec;
330325
limit_time->tv_usec += timeout->tv_usec;
331326
if (limit_time->tv_usec >= 1000000) {
332327
limit_time->tv_usec -= 1000000;
333328
limit_time->tv_sec++;
334329
}
335-
return SUCCESS;
336330
}
337331
#endif
338332

@@ -399,11 +393,7 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd,
399393
if (timeout) {
400394
memcpy(&working_timeout, timeout, sizeof(working_timeout));
401395
#if HAVE_GETTIMEOFDAY
402-
if (UNEXPECTED(php_network_set_limit_time(&limit_time, &working_timeout) == FAILURE)) {
403-
error = ERANGE;
404-
ret = -1;
405-
goto ok;
406-
}
396+
php_network_set_limit_time(&limit_time, &working_timeout);
407397
#endif
408398
}
409399

@@ -861,10 +851,7 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
861851
if (timeout) {
862852
memcpy(&working_timeout, timeout, sizeof(working_timeout));
863853
#if HAVE_GETTIMEOFDAY
864-
if (UNEXPECTED(php_network_set_limit_time(&limit_time, &working_timeout) == FAILURE)) {
865-
php_network_freeaddresses(psal);
866-
return -1;
867-
}
854+
php_network_set_limit_time(&limit_time, &working_timeout);
868855
#endif
869856
}
870857

0 commit comments

Comments
 (0)