Skip to content

Commit 518afbf

Browse files
committed
check segment fault in log_error function
1 parent 5e50854 commit 518afbf

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

include/peacalm/luaw.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,13 +1937,28 @@ class luaw {
19371937
std::cerr << "Lua: " << s << std::endl;
19381938
}
19391939

1940-
void log_error_in_stack(int idx = -1) const {
1941-
std::cerr << "Lua: " << lua_tostring(L_, idx) << std::endl;
1940+
const char* get_error_info_in_stack(int idx = -1) const {
1941+
return lua_tostring(L_, idx);
19421942
}
19431943

1944-
void log_error_out(int idx = -1) {
1945-
log_error_in_stack(idx);
1946-
pop();
1944+
bool log_error_in_stack(int idx = -1) const {
1945+
const char* s = get_error_info_in_stack(idx);
1946+
if (s) {
1947+
std::cerr << "Lua: " << s << std::endl;
1948+
return true;
1949+
} else {
1950+
std::cerr << "No valid error info in stack" << std::endl;
1951+
return false;
1952+
}
1953+
}
1954+
1955+
// Output error info on top on stack and pop it if it is a valid string.
1956+
bool log_error_out() {
1957+
if (log_error_in_stack(-1)) {
1958+
pop();
1959+
return true;
1960+
}
1961+
return false;
19471962
}
19481963

19491964
void log_type_convert_error(int idx, const char* to) {

0 commit comments

Comments
 (0)