From 2e6c129263a813825500dd1c3b799eac1db60892 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Thu, 17 Nov 2022 23:51:45 +0000 Subject: [PATCH] Fix a mutex bug in Db and some memory errors in Json --- src/Db.c | 4 ++-- src/Json.c | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Db.c b/src/Db.c index 48c92df..f7d45f1 100644 --- a/src/Db.c +++ b/src/Db.c @@ -353,7 +353,6 @@ DbLock(Db * db, char *prefix, char *key) } ref->json = JsonDecode(fp); - fclose(fp); if (!ref->json) @@ -383,10 +382,11 @@ DbLock(Db * db, char *prefix, char *key) } } - pthread_mutex_unlock(&db->lock); pthread_mutex_lock(&ref->lock); finish: + pthread_mutex_unlock(&db->lock); + Free(file); Free(hash); return ref; diff --git a/src/Json.c b/src/Json.c index 3438380..dda81ab 100644 --- a/src/Json.c +++ b/src/Json.c @@ -707,6 +707,7 @@ JsonTokenSeek(JsonParserState * state) return; } state->tokenType = TOKEN_STRING; + state->tokenLen = strlen(state->token); break; default: if (c == '-' || isdigit(c)) @@ -921,7 +922,7 @@ JsonDecodeObject(JsonParserState * state) JsonTokenSeek(state); if (JsonExpect(state, TOKEN_STRING)) { - char *key = Malloc(state->tokenLen); + char *key = Malloc(state->tokenLen + 1); JsonValue *value; if (!key) @@ -933,6 +934,7 @@ JsonDecodeObject(JsonParserState * state) JsonTokenSeek(state); if (!JsonExpect(state, TOKEN_COLON)) { + Free(key); goto error; } @@ -941,6 +943,7 @@ JsonDecodeObject(JsonParserState * state) if (!value) { + Free(key); goto error; } @@ -1028,6 +1031,8 @@ error: HashMap * JsonDecode(FILE * stream) { + HashMap *result; + JsonParserState state; state.stream = stream; @@ -1039,5 +1044,12 @@ JsonDecode(FILE * stream) return NULL; } - return JsonDecodeObject(&state); + result = JsonDecodeObject(&state); + + if (state.token) + { + Free(state.token); + } + + return result; }