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

Error handling #13

Merged
merged 6 commits into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion oocairo.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ from_lua_rectangle (lua_State *L, cairo_rectangle_int_t *rect, int pos) {
#define DO(t) \
lua_pushstring(L, #t); \
lua_gettable(L, pos); \
rect->t = luaL_checkinteger(L, -1); \
if (!lua_isnumber(L, -1)) { \
luaL_argerror(L, pos, "invalid rectangle"); \
} \
rect->t = lua_tointeger(L, -1); \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... With Lua 5.3, luaL_checkinteger does more than lua_isnumber, because 5.5 is a number, but not an integer. But lua_isinteger only exists in Lua 5.3 and not in older Lua versions.

How about something like this:

#if LUA_VERSION_NUM >= 503
#define oocairo_lua_isinteger(L, idx) lua_isinteger(L, idx)
#else
#define oocairo_lua_isinteger(L, idx) lua_isnumber(L, idx)
#endif

One could also check if the number is an integer in older Lua versions instead of silently truncating, but I am not sure if that is worth it...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Lua 5.1 documentation for luaL_checkinteger differs from the Lua 5.3 documentation:

Lua 5.1

luaL_checkinteger
Checks whether the function argument narg is a number and returns this number cast to a lua_Integer.

Lua 5.3

luaL_checkinteger
Checks whether the function argument arg is an integer (or can be converted to an integer) and returns this integer cast to a lua_Integer.

So if the documentation is true there was already a silent truncation of numbers to integers before my changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I incorporated oocairo_lua_isinteger as you suggested, see my latest commit.

lua_pop(L, 1)
DO(x);
DO(y);
Expand Down
5 changes: 5 additions & 0 deletions test/region.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ if Cairo.check_version(1, 10, 0) then
assert_equal(reg:contains_rectangle(rect), "overlap-in")
rect.width = 8
assert_equal(reg:contains_rectangle(rect), "overlap-part")
local ok, errmsg = pcall(function()
reg:contains_rectangle({ x = 0, y = 0 })
end)
assert_false(ok)
assert_match("bad argument %#1 to %'contains_rectangle%' %(invalid rectangle%)", errmsg)
end

function module.test_copy ()
Expand Down