diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 52baa64c..9071f56d 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -243,12 +243,16 @@ append_key(struct bson *bs, int type, const char *key, size_t sz) { write_string(bs, key, sz); } +static inline int +is_32bit(int64_t v) { + return v >= INT32_MIN && v <= INT32_MAX; +} + static void append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) { if (lua_isinteger(L, -1)) { int64_t i = lua_tointeger(L, -1); - int si = i >> 31; - if (si == 0 || si == -1) { + if (is_32bit(i)) { append_key(bs, BSON_INT32, key, sz); write_int32(bs, i); } else {