Skip to content

Commit

Permalink
fix memory corruption when unregistering plugins
Browse files Browse the repository at this point in the history
unregistering plugins twice may corrupt memory. This commit fixes that.
  • Loading branch information
mm2 committed Nov 12, 2022
1 parent 5cab2e5 commit a9e4601
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions plugins/fast_float/testbed/fast_float_testbed.c
Original file line number Diff line number Diff line change
Expand Up @@ -2468,7 +2468,7 @@ int main()
trace("Installing plug-in ... ");
cmsPlugin(cmsFastFloatExtensions());
trace("done.\n\n");

CheckComputeIncrements();

// 15 bit functionality
Expand Down Expand Up @@ -2508,7 +2508,7 @@ int main()

trace("\nAll tests passed OK\n");

cmsUnregisterPlugins();
cmsDeleteContext(0);

return 0;
}
Expand Down
16 changes: 9 additions & 7 deletions src/cmsplugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,7 @@ void* _cmsContextGetClientChunk(cmsContext ContextID, _cmsMemoryClient mc)
// many different plug-ins simultaneously, then there is no way to
// identify which plug-in to unregister.
void CMSEXPORT cmsUnregisterPluginsTHR(cmsContext ContextID)
{
struct _cmsContext_struct* ctx = _cmsGetContext(ContextID);

{
_cmsRegisterMemHandlerPlugin(ContextID, NULL);
_cmsRegisterInterpPlugin(ContextID, NULL);
_cmsRegisterTagTypePlugin(ContextID, NULL);
Expand All @@ -811,9 +809,6 @@ void CMSEXPORT cmsUnregisterPluginsTHR(cmsContext ContextID)
_cmsRegisterMutexPlugin(ContextID, NULL);
_cmsRegisterParallelizationPlugin(ContextID, NULL);

if (ctx->MemPool != NULL)
_cmsSubAllocDestroy(ctx->MemPool);
ctx->MemPool = NULL;
}


Expand Down Expand Up @@ -981,7 +976,14 @@ cmsContext CMSEXPORT cmsDupContext(cmsContext ContextID, void* NewUserData)
// The ContextID can no longer be used in any THR operation.
void CMSEXPORT cmsDeleteContext(cmsContext ContextID)
{
if (ContextID != NULL) {
if (ContextID == NULL) {

cmsUnregisterPlugins();
if (globalContext.MemPool != NULL)
_cmsSubAllocDestroy(globalContext.MemPool);
globalContext.MemPool = NULL;
}
else {

struct _cmsContext_struct* ctx = (struct _cmsContext_struct*) ContextID;
struct _cmsContext_struct fakeContext;
Expand Down

0 comments on commit a9e4601

Please sign in to comment.