Skip to content

Commit b633150

Browse files
committed
Convert int64 that overflow 32bit long to strings
This fixes PHPC-197 along with running the `serverStatus` command
1 parent a222e39 commit b633150

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

php_compat_53.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,29 @@
4646
#else
4747
# define ARG_UNUSED
4848
#endif
49+
50+
#if SIZEOF_LONG == 4
51+
# define ADD_INDEX_INT64(zval, index, value) \
52+
if (value > LONG_MAX || value < LONG_MIN) { \
53+
char *tmp; \
54+
int tmp_len; \
55+
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Integer overflow detected on your platform: %lld", value); \
56+
tmp_len = spprintf(&tmp, 0, "%lld", value); \
57+
add_index_stringl(zval, index, tmp, tmp_len, 0); \
58+
} else { \
59+
add_index_long(zval, index, val); \
60+
}
61+
# define ADD_ASSOC_INT64(zval, key, value) \
62+
if (value > LONG_MAX || value < LONG_MIN) { \
63+
char *tmp; \
64+
int tmp_len; \
65+
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Integer overflow detected on your platform: %lld", value); \
66+
tmp_len = spprintf(&tmp, 0, "%lld", value); \
67+
add_assoc_stringl(zval, key, tmp, tmp_len, 0); \
68+
} else { \
69+
add_assoc_long(zval, key, value); \
70+
}
71+
#else
72+
# define ADD_INDEX_INT64(zval, index, value) add_index_long(zval, index, value)
73+
# define ADD_ASSOC_INT64(zval, key, value) add_assoc_long(zval, key, value);
74+
#endif

src/MongoDB/WriteResult.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,7 @@ PHP_METHOD(WriteResult, getUpsertedIds)
226226
} else if (BSON_ITER_HOLDS_INT64(&outer)) {
227227
int64_t val = bson_iter_int64(&outer);
228228

229-
#if SIZEOF_LONG == 4
230-
if (val > INT_MAX) {
231-
add_index_long(return_value, index, (double)val);
232-
} else
233-
#endif
234-
{
235-
add_index_long(return_value, index, val);
236-
}
229+
ADD_INDEX_INT64(return_value, index, val);
237230
}
238231
}
239232
}

src/bson.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,19 +381,8 @@ bool php_phongo_bson_visit_int64(const bson_iter_t *iter ARG_UNUSED, const char
381381
{
382382
zval *retval = ((php_phongo_bson_state *)data)->zchild;
383383

384-
#if SIZEOF_LONG == 4
385-
if (v_int64 > LONG_MAX || v_int64 < LONG_MIN) {
386-
char *tmp;
387-
int tmp_len;
388-
389-
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Integer overflow detected on your platform: %lld", v_int64);
390-
tmp_len = spprintf(&tmp, 0, "%lld", v_int64);
391-
add_assoc_stringl(retval, key, tmp, tmp_len, 0);
392-
return false;
393-
}
394-
#endif
395384

396-
add_assoc_long(retval, key, v_int64);
385+
ADD_ASSOC_INT64(retval, key, v_int64);
397386

398387
return false;
399388
}

0 commit comments

Comments
 (0)