Skip to content

Commit 4be6ec8

Browse files
authored
Merge pull request #2043 from joto/fix-double-to-uint
Fix architecture-dependent double to integer conversion
2 parents 337505d + 016c1a8 commit 4be6ec8

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

src/flex-lua-expire-output.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,19 @@ create_expire_output(lua_State *lua_state, std::string const &default_schema,
4646
}
4747

4848
// required "maxzoom" field
49-
auto value = luaX_get_table_optional_uint32(
50-
lua_state, "maxzoom", -1, "The 'maxzoom' field in a expire output");
51-
if (value >= 1 && value <= 20) {
52-
new_expire_output.set_minzoom(value);
53-
new_expire_output.set_maxzoom(value);
54-
} else {
55-
throw std::runtime_error{
56-
"Value of 'maxzoom' field must be between 1 and 20."};
57-
}
49+
auto const maxzoom = luaX_get_table_optional_uint32(
50+
lua_state, "maxzoom", -1, "The 'maxzoom' field in a expire output", 1,
51+
20, "1 and 20");
52+
new_expire_output.set_minzoom(maxzoom);
53+
new_expire_output.set_maxzoom(maxzoom);
5854
lua_pop(lua_state, 1); // "maxzoom"
5955

6056
// optional "minzoom" field
61-
value = luaX_get_table_optional_uint32(
62-
lua_state, "minzoom", -1, "The 'minzoom' field in a expire output");
63-
if (value >= 1 && value <= new_expire_output.maxzoom()) {
64-
new_expire_output.set_minzoom(value);
65-
} else if (value != 0) {
66-
throw std::runtime_error{
67-
"Value of 'minzoom' field must be between 1 and 'maxzoom'."};
57+
auto const minzoom = luaX_get_table_optional_uint32(
58+
lua_state, "minzoom", -1, "The 'minzoom' field in a expire output", 1,
59+
maxzoom, "1 and 'maxzoom'");
60+
if (minzoom > 0) {
61+
new_expire_output.set_minzoom(minzoom);
6862
}
6963
lua_pop(lua_state, 1); // "minzoom"
7064

src/lua-utils.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ bool luaX_get_table_bool(lua_State *lua_state, char const *key, int table_index,
160160
}
161161

162162
uint32_t luaX_get_table_optional_uint32(lua_State *lua_state, char const *key,
163-
int table_index, char const *error_msg)
163+
int table_index, char const *error_msg,
164+
uint32_t min, uint32_t max,
165+
char const *range)
164166
{
165167
assert(lua_state);
166168
assert(key);
@@ -172,7 +174,13 @@ uint32_t luaX_get_table_optional_uint32(lua_State *lua_state, char const *key,
172174
if (!lua_isnumber(lua_state, -1)) {
173175
throw fmt_error("{} must contain an integer.", error_msg);
174176
}
175-
return lua_tonumber(lua_state, -1);
177+
178+
auto const num = lua_tonumber(lua_state, -1);
179+
if (num < static_cast<double>(min) || num > static_cast<double>(max)) {
180+
throw fmt_error("{} must be between {}.", error_msg, range);
181+
}
182+
183+
return static_cast<uint32_t>(num);
176184
}
177185

178186
// Lua 5.1 doesn't support luaL_traceback, unless LuaJIT is used

src/lua-utils.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ char const *luaX_get_table_string(lua_State *lua_state, char const *key,
5858
char const *default_value);
5959

6060
uint32_t luaX_get_table_optional_uint32(lua_State *lua_state, char const *key,
61-
int table_index, char const *error_msg);
61+
int table_index, char const *error_msg,
62+
uint32_t min, uint32_t max,
63+
char const *range);
6264

6365
bool luaX_get_table_bool(lua_State *lua_state, char const *key, int table_index,
6466
char const *error_msg, bool default_value);

tests/bdd/flex/lua-expire-output-definitions.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Feature: Expire output definitions in Lua file
104104
Then running osm2pgsql flex fails
105105
And the error output contains
106106
"""
107-
Value of 'maxzoom' field must be between 1 and 20.
107+
The 'maxzoom' field in a expire output must be between 1 and 20.
108108
"""
109109

110110
Scenario: Minzoom value in expire output definition has to be in range
@@ -121,7 +121,7 @@ Feature: Expire output definitions in Lua file
121121
Then running osm2pgsql flex fails
122122
And the error output contains
123123
"""
124-
Value of 'minzoom' field must be between 1 and 'maxzoom'.
124+
The 'minzoom' field in a expire output must be between 1 and 'maxzoom'.
125125
"""
126126

127127
Scenario: Minzoom value in expire output definition has to be smaller than maxzoom
@@ -138,6 +138,6 @@ Feature: Expire output definitions in Lua file
138138
Then running osm2pgsql flex fails
139139
And the error output contains
140140
"""
141-
Value of 'minzoom' field must be between 1 and 'maxzoom'.
141+
The 'minzoom' field in a expire output must be between 1 and 'maxzoom'.
142142
"""
143143

0 commit comments

Comments
 (0)