aboutsummaryrefslogtreecommitdiffstats
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-06-29 19:38:23 +0200
commit516394580b0ec92e19f677ff13eab16d30738a3a (patch)
tree5004a2c681176dd0e241490b22badda2e2b96b2d
parent63ea15852243014dc7fd63a398e9986b3b9faaa9 (diff)
Fix segfault when printing long message from lua
Change-Id: Idb3cb8ebe94cc1670e8757c6ff713ab7ef107a8b Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
-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];