Skip to content

Commit 90aac52

Browse files
committed
Fix GH-16592 msg_send() crashes when the type does not serialize as expected.
It is assumed that the serialization always had initialised its buffer zend_string, but in the case of a type not serialising, it is null. close GH-16599
1 parent e643129 commit 90aac52

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ PHP NEWS
129129
. Fixed bug GH-16293 (Failed assertion when throwing in assert() callback with
130130
bail enabled). (ilutov)
131131

132+
- SysVMsg:
133+
. Fixed bug GH-16592 (msg_send() crashes when a type does not properly
134+
serialized). (David Carlier / cmb)
135+
132136
- SysVShm:
133137
. Fixed bug GH-16591 (Assertion error in shm_put_var). (nielsdos, cmb)
134138

ext/sysvmsg/sysvmsg.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,19 @@ PHP_FUNCTION(msg_send)
371371
php_var_serialize(&msg_var, message, &var_hash);
372372
PHP_VAR_SERIALIZE_DESTROY(var_hash);
373373

374+
if (UNEXPECTED(EG(exception))) {
375+
smart_str_free(&msg_var);
376+
RETURN_THROWS();
377+
}
378+
379+
380+
zend_string *str = smart_str_extract(&msg_var);
381+
message_len = ZSTR_LEN(str);
374382
/* NB: php_msgbuf is 1 char bigger than a long, so there is no need to
375383
* allocate the extra byte. */
376-
messagebuffer = safe_emalloc(ZSTR_LEN(msg_var.s), 1, sizeof(struct php_msgbuf));
377-
memcpy(messagebuffer->mtext, ZSTR_VAL(msg_var.s), ZSTR_LEN(msg_var.s) + 1);
378-
message_len = ZSTR_LEN(msg_var.s);
384+
messagebuffer = safe_emalloc(message_len, 1, sizeof(struct php_msgbuf));
385+
memcpy(messagebuffer->mtext, ZSTR_VAL(str), message_len + 1);
386+
zend_string_release_ex(str, false);
379387
smart_str_free(&msg_var);
380388
} else {
381389
char *p;

ext/sysvmsg/tests/gh16592.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
msg_send() segfault when the type does not serialize as expected
3+
--EXTENSIONS--
4+
sysvmsg
5+
--FILE--
6+
<?php
7+
class Test {
8+
function __serialize() {}
9+
}
10+
11+
$q = msg_get_queue(1);
12+
try {
13+
msg_send($q, 1, new Test, true);
14+
} catch (\TypeError $e) {
15+
echo $e->getMessage();
16+
}
17+
?>
18+
--EXPECT--
19+
Test::__serialize() must return an array

0 commit comments

Comments
 (0)