Skip to content

Commit

Permalink
Fix memory leaks / cppcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
chpock committed Oct 16, 2024
1 parent cf2ee99 commit fa2e707
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 39 deletions.
13 changes: 0 additions & 13 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
* Store initial -smallfilesize / -smallfilebuffer / -pagesize values that were
specified when first mounted, and use them by default when reopening
an archive for writing. If these parameters were defined when reopening
an archive for writing, then store these new values. This will allow to
use the same parameters each time when an archive is opened for writing.

* add sha256 command if crypto is enabled

* Investigate an issue with -directory parameter for glob.
Expand All @@ -23,13 +17,6 @@
* Add ability/subcommand to copy files from/to cookfs more efficiently than using 'file copy'.
It can be something like 'cookfs copy <from disk> <cookfs endpoint>"

* Add ability to get a list of pages with their properties (compression, compression level,
encryption, compressed size, uncompressed size)

* Add ability to get information about pagesindex and fsindex (compression, compression level,
encryption, compressed size, uncompressed size). This will be useful for analyzing the contents
of a cookfs archive.

* Add ability to get information about whole archive (file size, head size, cookfs size).
It should be available from attributes to a mount point.

Expand Down
15 changes: 10 additions & 5 deletions generic/fsindex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1621,15 +1621,20 @@ void Cookfs_FsindexForeach(Cookfs_Fsindex *i, Cookfs_FsindexForeachProc *proc, C
*/

Tcl_Obj *Cookfs_FsindexGetMetadata(Cookfs_Fsindex *i, const char *paramName) {
CookfsLog(printf("name [%s]", paramName));
Cookfs_FsindexWantRead(i);
Tcl_HashEntry *hashEntry;
hashEntry = Tcl_FindHashEntry(&i->metadataHash, paramName);
if (hashEntry != NULL) {
unsigned char *value = Tcl_GetHashValue(hashEntry);
return Tcl_NewByteArrayObj(value + sizeof(Tcl_Size), *((Tcl_Size *)value));
} else {
if (hashEntry == NULL) {
CookfsLog(printf("return: NULL"));
return NULL;
}
unsigned char *value = Tcl_GetHashValue(hashEntry);
Tcl_Obj *result = Tcl_NewByteArrayObj(value + sizeof(Tcl_Size),
*((Tcl_Size *)value));
CookfsLog(printf("return: [%s] (obj: %p)", Tcl_GetString(result),
(void *)result));
return result;
}

/*
Expand Down Expand Up @@ -1672,7 +1677,7 @@ Tcl_Obj *Cookfs_FsindexGetMetadataAllKeys(Cookfs_Fsindex *i) {
{

Tcl_Obj *keyObj = Tcl_NewStringObj((const char *)Tcl_GetHashKey(
&i->metadataHash, hPtr), -1);
&i->metadataHash, hPtr), -1); // cppcheck-suppress knownArgument

Tcl_ListObjAppendElement(NULL, result, keyObj);

Expand Down
32 changes: 14 additions & 18 deletions generic/pages.c
Original file line number Diff line number Diff line change
Expand Up @@ -3212,6 +3212,7 @@ skipEncryption: ; // empty statement

if (pgindexSizeCompressed == 0) {
CookfsLog(printf("pgindex is empty and skipped"));
p->pagesIndex = Cookfs_PgIndexInit(0);
goto skipPgindex;
}

Expand Down Expand Up @@ -3250,6 +3251,11 @@ skipEncryption: ; // empty statement
goto pgindexReadError;
}

Cookfs_PgIndexAddPageSpecial(p->pagesIndex,
(Cookfs_CompressionType)pgindexCompression, pgindexCompressionLevel,
isIndexEncrypted, pgindexSizeCompressed, pgindexSizeUncompressed,
pgindexOffset, COOKFS_PGINDEX_SPECIAL_PAGE_TYPE_PGINDEX);

// We have successfully read pgindex. Let's continue with fsindex.

goto skipPgindex;
Expand Down Expand Up @@ -3303,6 +3309,11 @@ skipEncryption: ; // empty statement
goto fsindexReadError;
}

Cookfs_PgIndexAddPageSpecial(p->pagesIndex,
(Cookfs_CompressionType)fsindexCompression, fsindexCompressionLevel,
isIndexEncrypted, fsindexSizeCompressed, fsindexSizeUncompressed,
fsindexOffset, COOKFS_PGINDEX_SPECIAL_PAGE_TYPE_FSINDEX);

// We have successfully read fsindex. Let's continue.

goto skipFsindex;
Expand Down Expand Up @@ -3334,25 +3345,10 @@ skipEncryption: ; // empty statement
fsindexSizeCompressed - COOKFS_SUFFIX_BYTES;

// If we have page data, subtract the size of all pages.
if (p->pagesIndex != NULL) {
int pgindexLength = Cookfs_PgIndexGetLength(p->pagesIndex);
if (pgindexLength > 0) {
p->dataInitialOffset -= Cookfs_PgIndexGetStartOffset(p->pagesIndex,
Cookfs_PgIndexGetLength(p->pagesIndex));
} else {
p->pagesIndex = Cookfs_PgIndexInit(0);
}

if (fsindexSizeCompressed != 0) {
Cookfs_PgIndexAddPageSpecial(p->pagesIndex,
(Cookfs_CompressionType)fsindexCompression, fsindexCompressionLevel,
isIndexEncrypted, fsindexSizeCompressed, fsindexSizeUncompressed,
fsindexOffset, COOKFS_PGINDEX_SPECIAL_PAGE_TYPE_FSINDEX);
}

if (pgindexSizeCompressed != 0) {
Cookfs_PgIndexAddPageSpecial(p->pagesIndex,
(Cookfs_CompressionType)pgindexCompression, pgindexCompressionLevel,
isIndexEncrypted, pgindexSizeCompressed, pgindexSizeUncompressed,
pgindexOffset, COOKFS_PGINDEX_SPECIAL_PAGE_TYPE_PGINDEX);
pgindexLength);
}

if (p->dataInitialOffset < 0) {
Expand Down
1 change: 1 addition & 0 deletions generic/pagesCompr.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Tcl_Obj *Cookfs_CompressionToObj(Cookfs_CompressionType compression,
if (compressionLevel == -1 || compressionLevel ==
Cookfs_CompressionGetDefaultLevel(compression))
{
// cppcheck-suppress knownArgument
return Tcl_NewStringObj(Cookfs_CompressionGetName(compression), -1);
} else {
return Tcl_ObjPrintf("%s:%d", Cookfs_CompressionGetName(compression),
Expand Down
3 changes: 2 additions & 1 deletion generic/pgindex.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Tcl_Obj *Cookfs_PgIndexGetInfo(Cookfs_PgIndex *pgi, int num) {
CookfsLog(printf("info about special page %s (#%d)",
specialIndex == COOKFS_PGINDEX_SPECIAL_PAGE_TYPE_PGINDEX ?
"PGINDEX" :
specialIndex == COOKFS_PGINDEX_SPECIAL_PAGE_TYPE_PGINDEX ?
specialIndex == COOKFS_PGINDEX_SPECIAL_PAGE_TYPE_FSINDEX ?
"FSINDEX" :
"UNKNOWN", specialIndex));

Expand Down Expand Up @@ -144,6 +144,7 @@ Tcl_Obj *Cookfs_PgIndexGetInfo(Cookfs_PgIndex *pgi, int num) {
break;
case COOKFS_PGINDEX_INFO_KEY_INDEX:
if (num < 0) {
// cppcheck-suppress knownArgument
val = Tcl_NewStringObj(specialIndexName[specialIndex], -1);
} else {
val = Tcl_NewIntObj(num);
Expand Down
4 changes: 3 additions & 1 deletion generic/vfsAttributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ static int Cookfs_AttrSet_Pages(Tcl_Interp *interp, Cookfs_Vfs *vfs,
break;
case infList:
result = Tcl_NewListObj(0, NULL);
for (int i = 0; i < length; i++) {
for (i = 0; i < length; i++) {
Tcl_ListObjAppendElement(NULL, result,
Cookfs_PagesGetInfo(pages, i));
}
Expand Down Expand Up @@ -242,6 +242,8 @@ static int Cookfs_AttrSet_Fileset(Tcl_Interp *interp, Cookfs_Vfs *vfs,
Tcl_Obj **result_ptr = (interp == NULL ? NULL : &result);
int rc = Cookfs_VfsFilesetSelect(vfs, value, result_ptr, result_ptr);
CookfsLog(printf("return: %s", (rc == TCL_OK ? "OK" : "ERROR")));
// We Cookfs_VfsFilesetSelect() will set result via result_ptr
// cppcheck-suppress knownConditionTrueFalse
if (rc != TCL_OK && interp != NULL && result == NULL) {
result = Tcl_NewStringObj("unknown error", -1);
}
Expand Down
9 changes: 9 additions & 0 deletions generic/vfsCmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ int Cookfs_Mount(Tcl_Interp *interp, Tcl_Obj *archive, Tcl_Obj *local,
CookfsLog(printf("prop pagesize is defined"));
}

if (tmpObj != NULL) {
Tcl_BounceRefCount(tmpObj);
}
tmpObj = Tcl_NewWideIntObj(props->pagesize);
Cookfs_FsindexSetMetadata(index, pagesizeMetadataKey, tmpObj);

Expand Down Expand Up @@ -843,6 +846,9 @@ int Cookfs_Mount(Tcl_Interp *interp, Tcl_Obj *archive, Tcl_Obj *local,
CookfsLog(printf("prop smallfilesize is defined"));
}

if (tmpObj != NULL) {
Tcl_BounceRefCount(tmpObj);
}
tmpObj = Tcl_NewWideIntObj(props->smallfilesize);
Cookfs_FsindexSetMetadata(index, smallfilesizeMetadataKey, tmpObj);

Expand Down Expand Up @@ -873,6 +879,9 @@ int Cookfs_Mount(Tcl_Interp *interp, Tcl_Obj *archive, Tcl_Obj *local,
CookfsLog(printf("prop smallfilebuffer is defined"));
}

if (tmpObj != NULL) {
Tcl_BounceRefCount(tmpObj);
}
tmpObj = Tcl_NewWideIntObj(props->smallfilebuffer);
Cookfs_FsindexSetMetadata(index, smallfilebufferMetadataKey, tmpObj);

Expand Down
Loading

0 comments on commit fa2e707

Please sign in to comment.