Skip to content

Commit adea221

Browse files
booti386Girgias
authored andcommitted
Improve socket cmsg space handling.
This should also fix the null pointer arithmetic warning on MacOS as we don't depend on whack code written by Apple. Closes GH-5387
1 parent 4a935bc commit adea221

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

ext/sockets/sendrecvmsg.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,18 @@ PHP_FUNCTION(socket_cmsg_space)
302302
return;
303303
}
304304

305-
if (entry->var_el_size > 0 && n > (zend_long)((ZEND_LONG_MAX - entry->size -
306-
CMSG_SPACE(0) - 15L) / entry->var_el_size)) {
307-
/* the -15 is to account for any padding CMSG_SPACE may add after the data */
308-
php_error_docref(NULL, E_WARNING, "The value for the "
305+
if (entry->var_el_size > 0) {
306+
size_t rem_size = ZEND_LONG_MAX - entry->size;
307+
size_t n_max = rem_size / entry->var_el_size;
308+
size_t size = entry->size + n * entry->var_el_size;
309+
size_t total_size = CMSG_SPACE(size);
310+
if (n > n_max /* zend_long overflow */
311+
|| total_size > ZEND_LONG_MAX
312+
|| total_size < size /* align overflow */) {
313+
php_error_docref(NULL, E_WARNING, "The value for the "
309314
"third argument (" ZEND_LONG_FMT ") is too large", n);
310-
return;
315+
return;
316+
}
311317
}
312318

313319
RETURN_LONG((zend_long)CMSG_SPACE(entry->size + n * entry->var_el_size));

0 commit comments

Comments
 (0)