Fix memory leaks in database.

This commit is contained in:
Jordan Bancino 2022-12-15 20:29:19 +00:00
parent afa0d89e40
commit bee07d90fc
3 changed files with 31 additions and 14 deletions

View file

@ -11,11 +11,6 @@ Key:
Milestone: v0.1.1 Milestone: v0.1.1
----------------- -----------------
[x] DbDelete()
[x] UtilRandomString()
[x] Hex dump leaked memory
[ ] Leaks in DB
[ ] Database version file [ ] Database version file
[ ] User registration [ ] User registration

View file

@ -82,6 +82,9 @@ parser that prevented GET parameters from being parsed.
Fixed the database file name generator to prevent directory Fixed the database file name generator to prevent directory
traversal attacks by replacing special characters with traversal attacks by replacing special characters with
safer ones. safer ones.
.It
Fixed a memory leak that would occur when closing a
database that contains cached objects.
.El .El
.Pp .Pp
Misc: Misc:

View file

@ -60,6 +60,19 @@ struct DbRef
FILE *fp; FILE *fp;
}; };
static void
StringArrayFree(Array *arr)
{
size_t i;
for (i = 0; i < ArraySize(arr); i++)
{
Free(ArrayGet(arr, i));
}
ArrayFree(arr);
}
static ssize_t DbComputeSize(HashMap *); static ssize_t DbComputeSize(HashMap *);
static ssize_t static ssize_t
@ -209,22 +222,23 @@ DbFileName(Db * db, Array * args)
{ {
char *tmp, *tmp2; char *tmp, *tmp2;
char *arg = UtilStringDuplicate(ArrayGet(args, i)); char *arg = UtilStringDuplicate(ArrayGet(args, i));
size_t j = 0;
/* Sanitize name to prevent directory traversal attacks */ /* Sanitize name to prevent directory traversal attacks */
while (*arg) while (arg[j])
{ {
switch (*arg) switch (arg[j])
{ {
case '/': case '/':
*arg = '_'; arg[j] = '_';
break; break;
case '.': case '.':
*arg = '-'; arg[j] = '-';
break; break;
default: default:
break; break;
} }
arg++; j++;
} }
tmp = UtilStringConcat(str, arg); tmp = UtilStringConcat(str, arg);
@ -266,7 +280,7 @@ DbCacheEvict(Db * db)
HashMapDelete(db->cache, hash); HashMapDelete(db->cache, hash);
Free(hash); Free(hash);
ArrayFree(ref->name); StringArrayFree(ref->name);
db->cacheSize -= ref->size; db->cacheSize -= ref->size;
@ -337,7 +351,7 @@ DbClose(Db * db)
{ {
Free(key); Free(key);
JsonFree(val->json); JsonFree(val->json);
ArrayFree(val->name); StringArrayFree(val->name);
pthread_mutex_destroy(&val->lock); pthread_mutex_destroy(&val->lock);
Free(val); Free(val);
} }
@ -507,6 +521,7 @@ DbCreate(Db * db, size_t nArgs,...)
char *dir; char *dir;
va_list ap; va_list ap;
Array *args; Array *args;
DbRef *ret;
if (!db) if (!db)
{ {
@ -554,7 +569,11 @@ DbCreate(Db * db, size_t nArgs,...)
fflush(fp); fflush(fp);
fclose(fp); fclose(fp);
return DbLockFromArr(db, args); ret = DbLockFromArr(db, args);
ArrayFree(args);
return ret;
} }
int int
@ -588,7 +607,7 @@ DbDelete(Db * db, size_t nArgs,...)
HashMapDelete(db->cache, hash); HashMapDelete(db->cache, hash);
JsonFree(ref->json); JsonFree(ref->json);
ArrayFree(ref->name); StringArrayFree(ref->name);
db->cacheSize -= ref->size; db->cacheSize -= ref->size;