aboutsummaryrefslogtreecommitdiffstats
path: root/ctl-lib
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2018-06-05 22:41:43 +0200
committerJan-Simon Moeller <jsmoeller@linuxfoundation.org>2018-06-07 07:23:23 +0000
commitce3e14c8214d7319a3630418c52081fe6cb9c66d (patch)
tree8d94076c8283b4f319d6b56ea2ab567a6deef725 /ctl-lib
parent6838dff13293746e5855c6e3d1031aea150a6d76 (diff)
Fix segfault when printing long message from lua
Change-Id: Idb3cb8ebe94cc1670e8757c6ff713ab7ef107a8b Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'ctl-lib')
-rw-r--r--ctl-lib/ctl-lua.c10
1 files 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 = "... <truncated> ";
+
+ 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];