Skip to content

Commit

Permalink
Fix a memory leak when discarding crafted profiles
Browse files Browse the repository at this point in the history
From chrome's fuzzer
  • Loading branch information
Marti Maria committed Jan 18, 2022
1 parent e090fcf commit ab5029d
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/cmsio0.c
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,25 @@ cmsBool CMSEXPORT cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr, cmsUIn
return rc;
}

// Free one tag contents
static
void freeOneTag(_cmsICCPROFILE* Icc, cmsUInt32Number i)
{
if (Icc->TagPtrs[i]) {

cmsTagTypeHandler* TypeHandler = Icc->TagTypeHandlers[i];

if (TypeHandler != NULL) {
cmsTagTypeHandler LocalTypeHandler = *TypeHandler;

LocalTypeHandler.ContextID = Icc->ContextID;
LocalTypeHandler.ICCVersion = Icc->Version;
LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc->TagPtrs[i]);
}
else
_cmsFree(Icc->ContextID, Icc->TagPtrs[i]);
}
}

// Closes a profile freeing any involved resources
cmsBool CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile)
Expand All @@ -1454,20 +1472,7 @@ cmsBool CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile)

for (i=0; i < Icc -> TagCount; i++) {

if (Icc -> TagPtrs[i]) {

cmsTagTypeHandler* TypeHandler = Icc ->TagTypeHandlers[i];

if (TypeHandler != NULL) {
cmsTagTypeHandler LocalTypeHandler = *TypeHandler;

LocalTypeHandler.ContextID = Icc ->ContextID; // As an additional parameters
LocalTypeHandler.ICCVersion = Icc ->Version;
LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc -> TagPtrs[i]);
}
else
_cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]);
}
freeOneTag(Icc, i);
}

if (Icc ->IOhandler != NULL) {
Expand Down Expand Up @@ -1623,12 +1628,9 @@ void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig)
// Return error and unlock the data
Error:

if (Icc->TagPtrs[n] != NULL)
{
_cmsFree(Icc->ContextID, Icc->TagPtrs[n]);
Icc->TagPtrs[n] = NULL;
}

freeOneTag(Icc, n);
Icc->TagPtrs[n] = NULL;

_cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex);
return NULL;
}
Expand Down

0 comments on commit ab5029d

Please sign in to comment.