diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | README | LICENSE

commit f61358134173ed79d40dfa9e76af0653a2b186e8
parent 551393fa46e4052d0af0c66e840cf77d9419810c
Author: Andreas Gruhler <agruhl@gmx.ch>
Date:   Mon, 31 May 2021 23:22:27 +0200

free json copy in extract_json_value

Diffstat:
Mutils.c | 12++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/utils.c b/utils.c @@ -2,10 +2,10 @@ char* extract_json_value(char* json, char* key, bool quoted) { // work on a copy of the json - char* tok = (char *) malloc(strlen(json) * sizeof(char)); - strcpy(tok, json); + char* jsoncp = (char*) malloc(strlen(json) * sizeof(char)); + strcpy(jsoncp, json); - tok = strtok(tok, " "); + char* tok = strtok(jsoncp, " "); while (tok != NULL) { if (strstr(tok, key) != NULL) { tok = strtok(NULL, " "); // value @@ -21,7 +21,11 @@ char* extract_json_value(char* json, char* key, bool quoted) { } else { tok = strtok(tok, ","); } - return tok; + + char* res = (char*) malloc(strlen(tok) * sizeof(char)); + strcpy(res, tok); + free(jsoncp); + return res; } char* unfold(const char* str) {