Skip to content

Commit 4635cf8

Browse files
committed
PHP-1195: Error out on 32bit platforms if we cannot represent the int
1 parent fa414d1 commit 4635cf8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

php_phongo.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,13 @@ void phongo_writeresult_init(zval *return_value, const bson_t *bson, int server_
349349
php_phongo_objectid_new_from_oid(zid, bson_iter_oid(&outer) TSRMLS_CC);
350350
add_index_zval(writeresult->upsertedIds, index, zid);
351351
} else if (BSON_ITER_HOLDS_INT64(&outer)) {
352-
int val = bson_iter_int64(&outer);
352+
int64_t val = bson_iter_int64(&outer);
353353

354+
#if SIZEOF_LONG == 4
355+
if (val > INT_MAX) {
356+
add_index_long(writeresult->upsertedIds, index, (double)val);
357+
} else
358+
#endif
354359
add_index_long(writeresult->upsertedIds, index, val);
355360
}
356361
}

src/bson.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040

4141
#if SIZEOF_LONG == 8
4242
# define BSON_APPEND_INT(b, key, keylen, val) \
43-
bson_append_int64(b, key, keylen, val);
43+
if (val > INT_MAX || val < INT_MIN) { \
44+
bson_append_int64(b, key, keylen, val); \
45+
} else { \
46+
bson_append_int32(b, key, keylen, val); \
47+
}
4448
#elif SIZEOF_LONG == 4
4549
# define BSON_APPEND_INT(b, key, keylen, val) \
4650
bson_append_int32(b, key, keylen, val);
@@ -425,6 +429,13 @@ bool php_phongo_bson_visit_int64(const bson_iter_t *iter __attribute__((unused))
425429
zval *retval = *(zval **)data;
426430
TSRMLS_FETCH();
427431

432+
#if SIZEOF_LONG == 4
433+
if (v_int64 > INT_MAX) {
434+
mongoc_log(MONGOC_LOG_LEVEL_ERROR, MONGOC_LOG_DOMAIN, "Integer overflow detected on your platform: %lld", v_int64);
435+
return false
436+
}
437+
#endif
438+
428439
if (Z_TYPE_P(retval) == IS_ARRAY) {
429440
add_assoc_long(retval, key, v_int64);
430441
} else if (Z_TYPE_P(retval) == IS_OBJECT) {

0 commit comments

Comments
 (0)