Skip to content

Commit 95ebf84

Browse files
no1semanDifferentialOrange
authored andcommitted
Fix silently cast huge cdata numbers to null
1 parent cd0f6e4 commit 95ebf84

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

graphql/types.lua

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,7 @@ local function isLong(value)
286286
end
287287

288288
if type(value) == 'cdata' then
289-
if ffi.istype('int64_t', value) then
290-
return true
291-
elseif ffi.istype('uint64_t', value) then
292-
return value < 2^63
293-
end
289+
return ffi.istype('int64_t', value) or ffi.istype('uint64_t', value)
294290
end
295291

296292
return false
@@ -299,9 +295,7 @@ end
299295
local function coerceLong(value)
300296
if value ~= nil then
301297
value = tonumber64(value)
302-
if not isLong(value) then return end
303298
end
304-
305299
return value
306300
end
307301

test/integration/graphql_test.lua

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function g.test_variables()
9696
local function callback(_, args)
9797
local result = ''
9898
for _, tuple in ipairs(getmetatable(args).__index) do
99-
result = result .. tuple.value
99+
result = result .. tostring(tuple.value)
100100
end
101101
return result
102102
end
@@ -184,10 +184,10 @@ function g.test_variables()
184184
)
185185

186186
t.assert_error_msg_equals(
187-
'Could not coerce value "18446744073709551614" to type "Long"',
187+
'Could not coerce value "18446744073709551617" to type "Long"',
188188
function()
189189
check_request([[
190-
query { test(arg: "", arg4: 18446744073709551614) }
190+
query { test(arg: "", arg4: 18446744073709551617) }
191191
]], query_schema)
192192
end
193193
)
@@ -2202,3 +2202,37 @@ function g.test_non_finite_float()
22022202
query_schema, nil, nil, {variables = variables})
22032203
end
22042204
end
2205+
2206+
function g.test_huge_cdata_number()
2207+
local query = [[
2208+
query ($x: Long!) { test(arg: $x) }
2209+
]]
2210+
2211+
local function callback(_, args)
2212+
return args[1].value
2213+
end
2214+
2215+
local query_schema = {
2216+
['test'] = {
2217+
kind = types.long.nonNull,
2218+
arguments = {
2219+
arg = types.long.nonNull,
2220+
},
2221+
resolve = callback,
2222+
}
2223+
}
2224+
2225+
local test_values = {
2226+
{4503599627370495ULL, "{\"test\":4503599627370495}"},
2227+
{9223372036854775807ULL, "{\"test\":9223372036854775807}"},
2228+
{-1LL, "{\"test\":-1}"},
2229+
{-1ULL, "{\"test\":18446744073709551615}"}}
2230+
2231+
for _, v in ipairs(test_values) do
2232+
local variables = {x = v[1]}
2233+
local res = check_request(query, query_schema, nil, nil, {variables = variables})
2234+
t.assert_type(res, 'table')
2235+
t.assert_equals(res.test, v[1])
2236+
t.assert_equals(json.encode(res), v[2])
2237+
end
2238+
end

0 commit comments

Comments
 (0)