Skip to content

Commit bf34b20

Browse files
committed
PHPC-592: ADD_ASSOC_STRINGL() can only be used with string literals
The previous macro uses ZEND_STRL(), which calculates the string length via sizeof() and makes it suitable only for string literals. Since this code is utilized by php_phongo_bson_visit_int64(), we must rely on strlen() to compute the field name length.
1 parent e430814 commit bf34b20

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

phongo_compat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
int tmp_len; \
111111
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Integer overflow detected on your platform: %lld", value); \
112112
tmp_len = spprintf(&tmp, 0, "%lld", value); \
113-
ADD_ASSOC_STRINGL(zval, key, tmp, tmp_len); \
113+
ADD_ASSOC_STRING_EX(zval, key, strlen(key), tmp, tmp_len); \
114114
efree(tmp); \
115115
} else { \
116116
add_assoc_long(zval, key, value); \

tests/bson/bug0592.phpt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
--TEST--
2+
PHPC-592: Property name corrupted when unserializing 64-bit integer on 32-bit platform
3+
--SKIPIF--
4+
<?php if (4 !== PHP_INT_SIZE) { die('skip Only for 32-bit platform'); } ?>
5+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
6+
--INI--
7+
mongodb.debug=stderr
8+
--FILE--
9+
<?php
10+
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
$tests = [
14+
'{ "x": { "$numberLong": "-2147483648" }}',
15+
'{ "x": { "$numberLong": "2147483647" }}',
16+
'{ "x": { "$numberLong": "4294967294" }}',
17+
'{ "x": { "$numberLong": "4294967295" }}',
18+
'{ "x": { "$numberLong": "9223372036854775807" }}',
19+
'{ "longFieldName": { "$numberLong": "-2147483648" }}',
20+
'{ "longFieldName": { "$numberLong": "2147483647" }}',
21+
'{ "longFieldName": { "$numberLong": "4294967294" }}',
22+
'{ "longFieldName": { "$numberLong": "4294967295" }}',
23+
'{ "longFieldName": { "$numberLong": "9223372036854775807" }}',
24+
];
25+
26+
foreach ($tests as $json) {
27+
printf("Test %s\n", $json);
28+
var_dump(toPHP(fromJSON($json)));
29+
echo "\n";
30+
}
31+
32+
?>
33+
===DONE===
34+
<?php exit(0); ?>
35+
--EXPECTF--
36+
Test { "x": { "$numberLong": "-2147483648" }}
37+
object(stdClass)#%d (%d) {
38+
["x"]=>
39+
int(-2147483648)
40+
}
41+
42+
Test { "x": { "$numberLong": "2147483647" }}
43+
object(stdClass)#%d (%d) {
44+
["x"]=>
45+
int(2147483647)
46+
}
47+
48+
Test { "x": { "$numberLong": "4294967295" }}
49+
[%s] PHONGO-BSON: WARNING > ENTRY: Integer overflow detected on your platform: 4294967295
50+
object(stdClass)#%d (%d) {
51+
["x"]=>
52+
string(10) "4294967295"
53+
}
54+
55+
Test { "x": { "$numberLong": "9223372036854775807" }}
56+
[%s] PHONGO-BSON: WARNING > ENTRY: Integer overflow detected on your platform: 9223372036854775807
57+
object(stdClass)#%d (%d) {
58+
["x"]=>
59+
string(19) "9223372036854775807"
60+
}
61+
62+
Test { "longFieldName": { "$numberLong": "-2147483648" }}
63+
object(stdClass)#%d (%d) {
64+
["longFieldName"]=>
65+
int(-2147483648)
66+
}
67+
68+
Test { "longFieldName": { "$numberLong": "2147483647" }}
69+
object(stdClass)#%d (%d) {
70+
["longFieldName"]=>
71+
int(2147483647)
72+
}
73+
74+
Test { "longFieldName": { "$numberLong": "4294967294" }}
75+
object(stdClass)#%d (%d) {
76+
["longFieldName"]=>
77+
string(10) "4294967294"
78+
}
79+
80+
Test { "longFieldName": { "$numberLong": "4294967295" }}
81+
[%s] PHONGO-BSON: WARNING > ENTRY: Integer overflow detected on your platform: 4294967295
82+
object(stdClass)#%d (%d) {
83+
["longFieldName"]=>
84+
string(10) "4294967295"
85+
}
86+
87+
Test { "longFieldName": { "$numberLong": "9223372036854775807" }}
88+
[%s] PHONGO-BSON: WARNING > ENTRY: Integer overflow detected on your platform: 9223372036854775807
89+
object(stdClass)#%d (%d) {
90+
["longFieldName"]=>
91+
string(19) "9223372036854775807"
92+
}
93+
94+
===DONE===

0 commit comments

Comments
 (0)