Skip to content

Commit

Permalink
+ Fix lua API in 5.1 or upper
Browse files Browse the repository at this point in the history
+ Fix UB when pass a negative number into `isprint()` when using UTF-8 charators.(Which will crash with MSVC)
+ Fix `cannot convert ‘bool’ to ‘const char*’ in return` or something like this with new GCC and clang.

Signed-off-by: owent <[email protected]>
  • Loading branch information
owent committed Jun 12, 2023
1 parent 56a7708 commit 21c81ca
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lua/def.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,11 @@ const upb_FileDef* lupb_FileDef_check(lua_State* L, int narg) {

static int lupb_FileDef_Dependency(lua_State* L) {
const upb_FileDef* f = lupb_FileDef_check(L, 1);
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
int index = luaL_checkinteger(L, 2);
#else
int index = luaL_checkint(L, 2);
#endif
const upb_FileDef* dep = upb_FileDef_Dependency(f, index);
lupb_wrapper_pushwrapper(L, 1, dep, LUPB_FILEDEF);
return 1;
Expand All @@ -631,7 +635,11 @@ static int lupb_FileDef_DependencyCount(lua_State* L) {

static int lupb_FileDef_enum(lua_State* L) {
const upb_FileDef* f = lupb_FileDef_check(L, 1);
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
int index = luaL_checkinteger(L, 2);
#else
int index = luaL_checkint(L, 2);
#endif
const upb_EnumDef* e = upb_FileDef_TopLevelEnum(f, index);
lupb_wrapper_pushwrapper(L, 1, e, LUPB_ENUMDEF);
return 1;
Expand All @@ -645,7 +653,11 @@ static int lupb_FileDef_enumcount(lua_State* L) {

static int lupb_FileDef_msg(lua_State* L) {
const upb_FileDef* f = lupb_FileDef_check(L, 1);
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
int index = luaL_checkinteger(L, 2);
#else
int index = luaL_checkint(L, 2);
#endif
const upb_MessageDef* m = upb_FileDef_TopLevelMessage(f, index);
lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
return 1;
Expand Down
2 changes: 2 additions & 0 deletions lua/upb.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@
/* Lua compatibility code *****************************************************/

/* Shims for upcoming Lua 5.3 functionality. */
#if LUA_VERSION_NUM < 503
static bool lua_isinteger(lua_State* L, int argn) {
LUPB_UNUSED(L);
LUPB_UNUSED(argn);
return false;
}
#endif

/* Utility functions **********************************************************/

Expand Down
2 changes: 1 addition & 1 deletion lua/upbc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void PrintString(int max_cols, absl::string_view* str,
} else if (ch == '\'') {
printer->PrintRaw("\\'");
max_cols--;
} else if (isprint(ch)) {
} else if (isprint(static_cast<int>(static_cast<unsigned char>(ch)))) {
printer->WriteRaw(&ch, 1);
max_cols--;
} else {
Expand Down
2 changes: 1 addition & 1 deletion upb/text/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ static void txtenc_map(txtenc* e, const upb_Map* map, const upb_FieldDef* f) {
#define CHK(x) \
do { \
if (!(x)) { \
return false; \
return NULL; \
} \
} while (0)

Expand Down

0 comments on commit 21c81ca

Please sign in to comment.