aboutsummaryrefslogtreecommitdiffstats
path: root/ctl-lib
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2018-06-05 22:41:43 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-12-13 15:02:55 +0100
commit7b807d877716ecd37cefbe536d5a700ae3c8f481 (patch)
tree8d94076c8283b4f319d6b56ea2ab567a6deef725 /ctl-lib
parentf207ab581021fb4f4b9996b9feb4b67ea51933ed (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];