Skip to content

Commit a9e4f09

Browse files
committed
[lua] logging: run tostring with error handling
* proper error handler * don't raise lua error if argument couldn't be stringified (but log in error.log) * use direct conversion / lua_tolstring for simple datatypes Change-Id: Icae2639cc668f9a66c6fe3b392ecb70f92347e23
1 parent 9a1458a commit a9e4f09

File tree

3 files changed

+58
-32
lines changed

3 files changed

+58
-32
lines changed

include/lighttpd/core_lua.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ LI_API void li_lua_environment_use_globals(liLuaState *LL); /* +1 */
149149
LI_API void li_lua_environment_restore_globals(lua_State *L); /* -1 */
150150

151151
/* joinWith " " (map tostring parameter[from..to]) */
152-
LI_API GString* li_lua_print_get_string(lua_State *L, int from, int to);
152+
LI_API GString* li_lua_print_get_string(lua_State *L, liServer *srv, liVRequest *vr, int from, int to);
153153

154154
/* pairs() for a GHashTable GString -> GString:
155155
* Don't modify the hashtable while iterating:

src/main/core_lua.c

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int li_lua_fixindex(lua_State *L, int ndx) {
2727
return ndx;
2828
}
2929

30-
static int traceback (lua_State *L) {
30+
static int traceback(lua_State *L) {
3131
if (!lua_isstring(L, 1)) /* 'message' not a string? */
3232
return 1; /* keep it intact */
3333
lua_getglobal(L, "debug");
@@ -205,44 +205,70 @@ void li_lua_environment_restore_globals(lua_State *L) /* -1 */ {
205205
li_lua_set_globals(L); /* -1 */
206206
}
207207

208-
GString* li_lua_print_get_string(lua_State *L, int from, int to) {
209-
int i, n = lua_gettop(L);
208+
/* resulting object on top of the stack might eighter be the lua string (owning the returned memory) or an error */
209+
static const char *li_lua_tolstring(lua_State *L, liServer *srv, liVRequest *vr, int idx, size_t *len) { /* +1 */
210+
int errfunc;
211+
212+
switch (lua_type(L, idx)) {
213+
case LUA_TBOOLEAN:
214+
lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
215+
break;
216+
case LUA_TNIL:
217+
lua_pushlstring(L, CONST_STR_LEN("nil"));
218+
break;
219+
case LUA_TSTRING:
220+
case LUA_TNUMBER:
221+
lua_pushvalue(L, idx);
222+
break;
223+
default:
224+
idx = li_lua_fixindex(L, idx);
225+
lua_pushcfunction(L, traceback); /* +1 */
226+
errfunc = lua_gettop(L);
227+
lua_getglobal(L, "tostring"); /* +1 */
228+
lua_pushvalue(L, idx); /* +1 object to convert to string */
229+
230+
if (lua_pcall(L, 1, 1, errfunc)) { /* -2 (func + args), +1 result */
231+
_VR_ERROR(srv, vr, "li_lua_tolstring failed: %s", lua_tostring(L, -1));
232+
233+
if (len) *len = 0;
234+
return NULL;
235+
}
236+
lua_remove(L, errfunc); /* -1 */
237+
}
238+
239+
return lua_tolstring(L, -1, len);
240+
}
241+
242+
GString* li_lua_print_get_string(lua_State *L, liServer *srv, liVRequest *vr, int from, int to) {
243+
int i;
210244
GString *buf = g_string_sized_new(0);
211245

212-
lua_getglobal(L, "tostring");
213246
for (i = from; i <= to; i++) {
214247
const char *s;
215248
size_t len;
216249

217-
lua_pushvalue(L, n+1);
218-
lua_pushvalue(L, i);
219-
if (0 != lua_pcall(L, 1, 1, 0)) goto failed;
220-
s = lua_tolstring(L, -1, &len);
221-
lua_pop(L, 1);
250+
if (NULL == (s = li_lua_tolstring(L, srv, vr, i, &len))) { /* +1 */
251+
s = "<failed tostring>";
252+
len = 17;
253+
}
222254

223-
if (NULL == s) goto failed;
224-
if (0 == len) continue;
225-
if (buf->len > 0) {
226-
g_string_append_c(buf, ' ');
227-
li_g_string_append_len(buf, s, len);
228-
} else {
229-
li_g_string_append_len(buf, s, len);
255+
if (len > 0) {
256+
if (buf->len > 0) {
257+
g_string_append_c(buf, ' ');
258+
li_g_string_append_len(buf, s, len);
259+
} else {
260+
li_g_string_append_len(buf, s, len);
261+
}
230262
}
263+
lua_pop(L, 1); /* -1 */
231264
}
232-
lua_pop(L, 1);
233265
return buf;
234-
235-
failed:
236-
g_string_free(buf, TRUE);
237-
lua_pushliteral(L, "lua_print_get_string: Couldn't convert parameter to string");
238-
lua_error(L);
239-
return NULL; /* should be unreachable */
240266
}
241267

242268
static int li_lua_error(lua_State *L) {
243269
liServer *srv = lua_touserdata(L, lua_upvalueindex(1));
244270
liWorker *wrk = lua_touserdata(L, lua_upvalueindex(2));
245-
GString *buf = li_lua_print_get_string(L, 1, lua_gettop(L));
271+
GString *buf = li_lua_print_get_string(L, srv, NULL, 1, lua_gettop(L));
246272

247273
_ERROR(srv, wrk, NULL, "(lua): %s", buf->str);
248274

@@ -254,7 +280,7 @@ static int li_lua_error(lua_State *L) {
254280
static int li_lua_warning(lua_State *L) {
255281
liServer *srv = lua_touserdata(L, lua_upvalueindex(1));
256282
liWorker *wrk = lua_touserdata(L, lua_upvalueindex(2));
257-
GString *buf = li_lua_print_get_string(L, 1, lua_gettop(L));
283+
GString *buf = li_lua_print_get_string(L, srv, NULL, 1, lua_gettop(L));
258284

259285
_WARNING(srv, wrk, NULL, "(lua): %s", buf->str);
260286

@@ -266,7 +292,7 @@ static int li_lua_warning(lua_State *L) {
266292
static int li_lua_info(lua_State *L) {
267293
liServer *srv = lua_touserdata(L, lua_upvalueindex(1));
268294
liWorker *wrk = lua_touserdata(L, lua_upvalueindex(2));
269-
GString *buf = li_lua_print_get_string(L, 1, lua_gettop(L));
295+
GString *buf = li_lua_print_get_string(L, srv, NULL, 1, lua_gettop(L));
270296

271297
_INFO(srv, wrk, NULL, "(lua): %s", buf->str);
272298

@@ -278,7 +304,7 @@ static int li_lua_info(lua_State *L) {
278304
static int li_lua_debug(lua_State *L) {
279305
liServer *srv = lua_touserdata(L, lua_upvalueindex(1));
280306
liWorker *wrk = lua_touserdata(L, lua_upvalueindex(2));
281-
GString *buf = li_lua_print_get_string(L, 1, lua_gettop(L));
307+
GString *buf = li_lua_print_get_string(L, srv, NULL, 1, lua_gettop(L));
282308

283309
_DEBUG(srv, wrk, NULL, "(lua): %s", buf->str);
284310

src/main/virtualrequest_lua.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static int lua_vrequest_error(lua_State *L) {
152152
GString *buf;
153153
vr = li_lua_get_vrequest(L, 1);
154154

155-
buf = li_lua_print_get_string(L, 2, lua_gettop(L));
155+
buf = li_lua_print_get_string(L, vr->wrk->srv, vr, 2, lua_gettop(L));
156156

157157
VR_ERROR(vr, "(lua): %s", buf->str);
158158

@@ -166,7 +166,7 @@ static int lua_vrequest_warning(lua_State *L) {
166166
GString *buf;
167167
vr = li_lua_get_vrequest(L, 1);
168168

169-
buf = li_lua_print_get_string(L, 2, lua_gettop(L));
169+
buf = li_lua_print_get_string(L, vr->wrk->srv, vr, 2, lua_gettop(L));
170170

171171
VR_WARNING(vr, "(lua): %s", buf->str);
172172

@@ -180,7 +180,7 @@ static int lua_vrequest_info(lua_State *L) {
180180
GString *buf;
181181
vr = li_lua_get_vrequest(L, 1);
182182

183-
buf = li_lua_print_get_string(L, 2, lua_gettop(L));
183+
buf = li_lua_print_get_string(L, vr->wrk->srv, vr, 2, lua_gettop(L));
184184

185185
VR_INFO(vr, "(lua): %s", buf->str);
186186

@@ -194,7 +194,7 @@ static int lua_vrequest_debug(lua_State *L) {
194194
GString *buf;
195195
vr = li_lua_get_vrequest(L, 1);
196196

197-
buf = li_lua_print_get_string(L, 2, lua_gettop(L));
197+
buf = li_lua_print_get_string(L, vr->wrk->srv, vr, 2, lua_gettop(L));
198198

199199
VR_DEBUG(vr, "(lua): %s", buf->str);
200200

0 commit comments

Comments
 (0)