diff options
| author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-16 22:29:59 +0800 | 
|---|---|---|
| committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-16 22:29:59 +0800 | 
| commit | 3500f0a5cd000ff0d650ab716f92a35dcf1c1581 (patch) | |
| tree | d6184cdd74c5bc34403a1713074b308aee75336a | |
| parent | b018ae643dbba21c0060ccb0748a7b239b4ecb5a (diff) | |
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.
| -rw-r--r-- | anteraja.c | 13 | 
1 files changed, 7 insertions, 6 deletions
@@ -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)  |