Skip to content

Commit ac9836f

Browse files
bugfix: double values larger than int32_t were incorrectly printed out as 64-bit integers.
1 parent 85fb9b8 commit ac9836f

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/ngx_http_lua_output.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ ngx_http_lua_get_num_len(lua_State *L, int idx)
3030
double num;
3131

3232
num = (double) lua_tonumber(L, idx);
33-
if (num == (double) (long) num) {
34-
return NGX_INT64_LEN;
33+
if (num == (double) (int32_t) num) {
34+
return NGX_INT32_LEN;
3535
}
3636

3737
return NGX_DOUBLE_LEN;
@@ -45,11 +45,16 @@ ngx_http_lua_write_num(lua_State *L, int idx, u_char *dst)
4545
int n;
4646

4747
num = (double) lua_tonumber(L, idx);
48-
if (num == (double) (long) num) {
49-
dst = ngx_snprintf(dst, NGX_INT64_LEN, "%l", (long) num);
48+
/*
49+
* luajit format number with only 14 significant digits.
50+
* To be consistent with lujit, don't use (double) (long) below
51+
* or integer greater than 99,999,999,999,999 will different from luajit.
52+
*/
53+
if (num == (double) (int32_t) num) {
54+
dst = ngx_snprintf(dst, NGX_INT64_LEN, "%D", (int32_t) num);
5055

5156
} else {
52-
/**
57+
/*
5358
* The maximum number of significant digits is 14 in lua.
5459
* Please refer to lj_strfmt.c for more details.
5560
*/

t/164-say.t

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,59 @@ GET /lua
5353
GET /lua
5454
--- response_body
5555
123 3.1415926
56+
57+
58+
59+
=== TEST 4: ngx.say min int32 -2147483648
60+
--- config
61+
location /lua {
62+
content_by_lua_block {
63+
ngx.say(-2147483648)
64+
}
65+
}
66+
--- request
67+
GET /lua
68+
--- response_body
69+
-2147483648
70+
71+
72+
73+
=== TEST 5: ngx.say big integer 2147483647
74+
--- config
75+
location /lua {
76+
content_by_lua_block {
77+
ngx.say(2147483647)
78+
}
79+
}
80+
--- request
81+
GET /lua
82+
--- response_body
83+
2147483647
84+
85+
86+
87+
=== TEST 6: ngx.say big integer -9223372036854775808
88+
--- config
89+
location /lua {
90+
content_by_lua_block {
91+
ngx.say(-9223372036854775808)
92+
}
93+
}
94+
--- request
95+
GET /lua
96+
--- response_body
97+
-9.2233720368548e+18
98+
99+
100+
101+
=== TEST 7: ngx.say big integer 18446744073709551615
102+
--- config
103+
location /lua {
104+
content_by_lua_block {
105+
ngx.say(18446744073709551615)
106+
}
107+
}
108+
--- request
109+
GET /lua
110+
--- response_body
111+
1.844674407371e+19

0 commit comments

Comments
 (0)