Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check range of integral when converting from number #91

Open
Flamefire opened this issue Aug 1, 2019 · 0 comments
Open

Check range of integral when converting from number #91

Flamefire opened this issue Aug 1, 2019 · 0 comments

Comments

@Flamefire
Copy link
Contributor

In Lua numbers are essentially doubles. So the following is valid in lua: foo(-1)

If this is bound to a C++ function void foo(unsigned i) it silently does the wrong thing.

This is caused by the unchecked cast at:

return static_cast<get_type>(lua_type_traits<lua_Number>::get(l, index));

Similar problem for C++ function void bar(int8_t i) called with foo(1000)

Proposed fix:


lua_Number num = lua_type_traits<lua_Number>::get(l, index);
if(num < std::numeric_limits<get_type>::min() || num > std::numeric_limits<get_type>::max())
  throw LuaTypeMismatch();
return static_cast<get_type>(num);

Probably extract the check to a standalone function as it is also required at other places (e.g. opt and for Lua 5.3)

Not sure if this breaks the contract of strictCheckType as it may be possible, that a value passes the check (just from checking types) but fails to be gotten as it is outside the range.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant