Fix off by one error in runtime check for array length

This commit is contained in:
coffeepots 2018-05-01 20:59:10 +01:00 committed by zah
parent f4598f563e
commit 562eb71ee5

View File

@ -159,13 +159,17 @@ macro processFields(jsonIdent, fieldName, fieldType: typed): untyped =
else: else:
# array # array
let let
expectedParams = typeInfo[1][1][2] startLen = typeInfo[1][1][1]
expectedParamsStr = expectedParams.toStrLit endLen = typeInfo[1][1][2]
expectedLenStr = "Expected parameter `" & fieldName.repr & "` to have a length of " & $expectedParamsStr & " but got " expectedLen = genSym(nskConst)
expectedParams = quote do:
const `expectedLen` = `endLen` - `startLen` + 1
expectedLenStr = "Expected parameter `" & fieldName.repr & "` to have a length of "
# TODO: Note, currently only raising if greater than value, not different size # TODO: Note, currently only raising if greater than value, not different size
result.add(quote do: result.add(quote do:
if `jsonIdent`.len > `expectedParams`: `expectedParams`
raise newException(ValueError, `expectedLenStr` & $`jsonIdent`.len) if `jsonIdent`.len > `expectedLen`:
raise newException(ValueError, `expectedLenStr` & $`expectedLen` & " but got " & $`jsonIdent`.len)
) )
result.add(quote do: result.add(quote do:
@ -257,7 +261,7 @@ when isMainModule:
res.add %int(item) res.add %int(item)
result = res result = res
type Test = object type Test = object
a: int #array[0..10, int] a: seq[int] #array[0..10, int]
type MyObject* = object type MyObject* = object
a: int a: int
@ -282,7 +286,7 @@ when isMainModule:
check r2 == ckR2 check r2 == ckR2
test "Object parameters": test "Object parameters":
let let
obj = %*{"a": %1, "b": %*{"a": %5}, "c": %1.23} obj = %*{"a": %1, "b": %*{"a": %[5]}, "c": %1.23}
r = waitfor rpcObjParam(%[%"abc", obj]) r = waitfor rpcObjParam(%[%"abc", obj])
check r == obj check r == obj
test "Runtime errors": test "Runtime errors":