Skip to content

Commit fb746e0

Browse files
committed
1 parent 9263e4c commit fb746e0

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

msgpack_unpack.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,37 @@ int msgpack_unserialize_uint16(msgpack_unserialize_data *unpack, uint16_t data,
395395
int msgpack_unserialize_uint32(msgpack_unserialize_data *unpack, uint32_t data, zval **obj) /* {{{ */ {
396396
MSGPACK_UNSERIALIZE_ALLOC_STACK(unpack);
397397

398-
ZVAL_LONG(*obj, data);
398+
if (data <= (uint32_t) ZEND_LONG_MAX) {
399+
ZVAL_LONG(*obj, data);
400+
} else {
401+
ZVAL_DOUBLE(*obj, data);
402+
}
399403

400404
return 0;
401405
}
402406
/* }}} */
403407

408+
/* see zend_print_ulong_to_buf() in Zend/zend_operators.h */
409+
static inline char *print_u64_to_buf(char *buf, uint64_t num) {
410+
*buf = '\0';
411+
do {
412+
*--buf = (char) (num % 10) + '0';
413+
num /= 10;
414+
} while (num > 0);
415+
return buf;
416+
}
417+
404418
int msgpack_unserialize_uint64(msgpack_unserialize_data *unpack, uint64_t data, zval **obj) /* {{{ */ {
405419
MSGPACK_UNSERIALIZE_ALLOC_STACK(unpack);
406420

407-
/* FIXME >= PHP_INT_MAX */
408-
ZVAL_LONG(*obj, data);
421+
if (data <= (uint64_t) ZEND_LONG_MAX) {
422+
ZVAL_LONG(*obj, data);
423+
} else if (data <= 1L<<52) {
424+
ZVAL_DOUBLE(*obj, data);
425+
} else {
426+
char buf[0x20] = {0}, *ptr = print_u64_to_buf(&buf[sizeof(buf)-1], data);
427+
ZVAL_STRING(*obj, ptr);
428+
}
409429

410430
return 0;
411431
}

tests/issue067.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Issue #67 (uint64_t)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("msgpack")) {
6+
die("skip");
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
echo msgpack_unpack("\xcf"."\xff\xff\xff\xff"."\xff\xff\xff\xff"), "\n";
12+
?>
13+
OK
14+
--EXPECT--
15+
18446744073709551615
16+
OK

0 commit comments

Comments
 (0)