Skip to content

Commit 42f7a28

Browse files
committed
Fix 64 bit integer processing on 32 bit platforms
This is the solution to #4 but rewritten to use compile-time pointer size determination.
1 parent 587922d commit 42f7a28

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lua_cmsgpack.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,19 @@
3030
#define IS_INT64_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int64_t)
3131
#define IS_INT_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int)
3232

33+
/* If size of pointer is equal to a 4 byte integer, we're on 32 bits. */
34+
#if UINTPTR_MAX == UINT_MAX
35+
#define BITS_32 1
36+
#else
37+
#define BITS_32 0
38+
#endif
39+
3340
#if LUA_VERSION_NUM < 503
34-
#define lua_pushunsigned(L, n) lua_pushinteger(L, n)
41+
#if BITS_32
42+
#define lua_pushunsigned(L, n) lua_pushnumber(L, n)
43+
#else
44+
#define lua_pushunsigned(L, n) lua_pushinteger(L, n)
45+
#endif
3546
#endif
3647

3748
/* =============================================================================
@@ -348,7 +359,11 @@ static void mp_encode_lua_bool(lua_State *L, mp_buf *buf) {
348359

349360
/* Lua 5.3 has a built in 64-bit integer type */
350361
static void mp_encode_lua_integer(lua_State *L, mp_buf *buf) {
362+
#if (LUA_VERSION_NUM < 503) && BITS_32
363+
lua_Number i = lua_tonumber(L,-1);
364+
#else
351365
lua_Integer i = lua_tointeger(L,-1);
366+
#endif
352367
mp_encode_int(buf, (int64_t)i);
353368
}
354369

@@ -431,10 +446,11 @@ static int table_is_an_array(lua_State *L) {
431446
/* The <= 0 check is valid here because we're comparing indexes. */
432447
#if LUA_VERSION_NUM < 503
433448
if ((LUA_TNUMBER != lua_type(L,-1)) || (n = lua_tonumber(L, -1)) <= 0 ||
434-
!IS_INT_EQUIVALENT(n)) {
449+
!IS_INT_EQUIVALENT(n))
435450
#else
436-
if (!lua_isinteger(L,-1) || (n = lua_tointeger(L, -1)) <= 0) {
451+
if (!lua_isinteger(L,-1) || (n = lua_tointeger(L, -1)) <= 0)
437452
#endif
453+
{
438454
lua_settop(L, stacktop);
439455
return 0;
440456
}
@@ -628,7 +644,11 @@ void mp_decode_to_lua_type(lua_State *L, mp_cur *c) {
628644
break;
629645
case 0xd3: /* int 64 */
630646
mp_cur_need(c,9);
647+
#if LUA_VERSION_NUM < 503
648+
lua_pushnumber(L,
649+
#else
631650
lua_pushinteger(L,
651+
#endif
632652
((int64_t)c->p[1] << 56) |
633653
((int64_t)c->p[2] << 48) |
634654
((int64_t)c->p[3] << 40) |

0 commit comments

Comments
 (0)