Skip to content

Commit 107cca1

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.
1 parent 9063aa6 commit 107cca1

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

ext/sockets/sendrecvmsg.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,15 @@ 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 */
305+
size_t rem_size = ZEND_LONG_MAX - entry->size;
306+
size_t n_max = entry->var_el_size > 0 ? rem_size / entry->var_el_size : 0;
307+
size_t size = entry->size + n * entry->var_el_size;
308+
size_t total_size = CMSG_SPACE(size);
309+
310+
if (entry->var_el_size > 0
311+
&& (n > n_max /* zend_long overflow */
312+
|| total_size > ZEND_LONG_MAX
313+
|| total_size < size /* align overflow */)) {
308314
php_error_docref(NULL, E_WARNING, "The value for the "
309315
"third argument (" ZEND_LONG_FMT ") is too large", n);
310316
return;

0 commit comments

Comments
 (0)