Fix a mutex bug in Db and some memory errors in Json

This commit is contained in:
Jordan Bancino 2022-11-17 23:51:45 +00:00
parent d26fad4619
commit 2e6c129263
2 changed files with 16 additions and 4 deletions

View file

@ -353,7 +353,6 @@ DbLock(Db * db, char *prefix, char *key)
} }
ref->json = JsonDecode(fp); ref->json = JsonDecode(fp);
fclose(fp); fclose(fp);
if (!ref->json) if (!ref->json)
@ -383,10 +382,11 @@ DbLock(Db * db, char *prefix, char *key)
} }
} }
pthread_mutex_unlock(&db->lock);
pthread_mutex_lock(&ref->lock); pthread_mutex_lock(&ref->lock);
finish: finish:
pthread_mutex_unlock(&db->lock);
Free(file); Free(file);
Free(hash); Free(hash);
return ref; return ref;

View file

@ -707,6 +707,7 @@ JsonTokenSeek(JsonParserState * state)
return; return;
} }
state->tokenType = TOKEN_STRING; state->tokenType = TOKEN_STRING;
state->tokenLen = strlen(state->token);
break; break;
default: default:
if (c == '-' || isdigit(c)) if (c == '-' || isdigit(c))
@ -921,7 +922,7 @@ JsonDecodeObject(JsonParserState * state)
JsonTokenSeek(state); JsonTokenSeek(state);
if (JsonExpect(state, TOKEN_STRING)) if (JsonExpect(state, TOKEN_STRING))
{ {
char *key = Malloc(state->tokenLen); char *key = Malloc(state->tokenLen + 1);
JsonValue *value; JsonValue *value;
if (!key) if (!key)
@ -933,6 +934,7 @@ JsonDecodeObject(JsonParserState * state)
JsonTokenSeek(state); JsonTokenSeek(state);
if (!JsonExpect(state, TOKEN_COLON)) if (!JsonExpect(state, TOKEN_COLON))
{ {
Free(key);
goto error; goto error;
} }
@ -941,6 +943,7 @@ JsonDecodeObject(JsonParserState * state)
if (!value) if (!value)
{ {
Free(key);
goto error; goto error;
} }
@ -1028,6 +1031,8 @@ error:
HashMap * HashMap *
JsonDecode(FILE * stream) JsonDecode(FILE * stream)
{ {
HashMap *result;
JsonParserState state; JsonParserState state;
state.stream = stream; state.stream = stream;
@ -1039,5 +1044,12 @@ JsonDecode(FILE * stream)
return NULL; return NULL;
} }
return JsonDecodeObject(&state); result = JsonDecodeObject(&state);
if (state.token)
{
Free(state.token);
}
return result;
} }