From 3500f0a5cd000ff0d650ab716f92a35dcf1c1581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Fri, 16 Jul 2021 22:29:59 +0800 Subject: Fix the truncated items JSON By making sure it's always concatenated with a null terminated string, AND fix the index that gets null terminated in the end. --- anteraja.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/anteraja.c b/anteraja.c index ad194f5..c64ac68 100644 --- a/anteraja.c +++ b/anteraja.c @@ -102,21 +102,22 @@ void anteraja_order(const char *trx_id, const char *service, const char *sender_ char *json = NULL; for (int i = 0; i < nitems; i++) { size_t length = strlen(ORDER_ITEM) + strlen(items[i][DESCRIPTION]) + ORDER_ITEM_QUANTITY - + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") + 1; - char item[length]; + + ORDER_ITEM_PRICE + ORDER_ITEM_WEIGHT - strlen("%s") - 3 * strlen("%d") + + strlen(","); + char item[length + 1]; sprintf(item, ORDER_ITEM, items[i][DESCRIPTION], atoi(items[i][QUANTITY]), atoi(items[i][PRICE]), atoi(items[i][WEIGHT]) * 1000); if (json) - json = realloc(json, strlen(json) + length); + json = realloc(json, strlen(json) + length + 1); else { - json = malloc(length); + json = malloc(length + 1); memset(json, '\0', strlen(json)); } strcat(json, item); if (i + 1 < nitems) - json[length] = ','; + strcat(json, ","); else - json[length] = '\0'; + json[strlen(json)] = '\0'; } *post = malloc(strlen(ORDER_POST) + strlen(prefix) + strlen(trx_id) + strlen(service) + strlen(sender_name) + strlen(sender_phone) + strlen(origin) -- cgit v1.2.3