From ce3e14c8214d7319a3630418c52081fe6cb9c66d Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Tue, 5 Jun 2018 22:41:43 +0200 Subject: Fix segfault when printing long message from lua Change-Id: Idb3cb8ebe94cc1670e8757c6ff713ab7ef107a8b Signed-off-by: Sebastien Douheret --- ctl-lib/ctl-lua.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c index 4342464..d891e24 100644 --- a/ctl-lib/ctl-lua.c +++ b/ctl-lib/ctl-lua.c @@ -305,11 +305,10 @@ static int LuaFormatMessage(lua_State* luaState, int verbosity, int level) { for (int idx = 0; format[idx] != '\0'; idx++) { - if (format[idx] == '%' && format[idx] != '\0') { + if (format[idx] == '%' && format[idx+1] != '\0') { json_object *slotJ = json_object_array_get_idx(responseJ, arrayIdx); //if (slotJ) AFB_NOTICE("**** idx=%d slotJ=%s", arrayIdx, json_object_get_string(slotJ)); - switch (format[++idx]) { case 'd': if (slotJ) uidIdx += snprintf(&message[uidIdx], LUA_MSG_MAX_LENGTH - uidIdx, "%d", json_object_get_int(slotJ)); @@ -340,8 +339,11 @@ static int LuaFormatMessage(lua_State* luaState, int verbosity, int level) { } else { if (uidIdx >= LUA_MSG_MAX_LENGTH) { - AFB_ApiWarning(source->api, "LuaFormatMessage: message[%s] owerverflow LUA_MSG_MAX_LENGTH=%d", format, LUA_MSG_MAX_LENGTH); - uidIdx--; // move backward for EOL + const char *trunc = "... "; + + AFB_ApiWarning(source->api, "LuaFormatMessage: message[%s] overflow LUA_MSG_MAX_LENGTH=%d\n", format, LUA_MSG_MAX_LENGTH); + uidIdx = LUA_MSG_MAX_LENGTH - 1; + memcpy(&message[uidIdx - strlen(trunc)], trunc, strlen(trunc)); break; } else { message[uidIdx++] = format[idx]; -- cgit 1.2.3-korg