Opened 18 years ago

Last modified 18 years ago

#1156 closed defect (fixed)

segfault if GDAL is compiled with ECW due to incorrect GDALDeregister_ECW semantics

Reported by: nikita.shulga@… Owned by: warmerdam
Priority: high Milestone:
Component: default Version: unspecified
Severity: normal Keywords:
Cc:

Description

Please note, that in GDALRegister_ECW defined in frmts/ecw/ecwdataset.cpp one always call to 
NCSecwInit, but NCSecwShutdown is called in GDALDeregister_ECW only if gpapszCSLookup is true, 
which is wrong. 
Please consider following exampe:
#include <stdio.h>
#include <dlfcn.h>
int main(void) {
        void (*gdalReg)(void);
        void (*gdalUnreg)(void);

        void * gdal = dlopen("libgdal.so", RTLD_LAZY);

        if (!gdal) perror ("Can't find GDAL lib!\n");

        gdalReg = dlsym(gdal, "GDALAllRegister");
        gdalUnreg = dlsym(gdal, "GDALDestroyDriverManager");
        if ( !gdalReg || !gdalUnreg ) perror ("Can't find register/unregister procs");

        gdalReg();
        gdalUnreg();

        dlclose(gdal);

        sleep(1);
        printf ("Vous est ici!");
}

Compile with -ldl flag, and run it and program will crash with segfault. This is because NCSecwInit 
created four threads and noone cared to terminated this thread while calling to 
GDALDestroyDriverManager

I encountered this unpleasant crash while using php_mapscript from Apache modphp.

Change History (1)

comment:1 by warmerdam, 18 years ago

Nikita, 

I have modified the Deregister() to look like this:

void GDALDeregister_ECW( GDALDriver * )

{
    if( gpapszCSLookup )
    {
        CSLDestroy( gpapszCSLookup );
        gpapszCSLookup = NULL;
        gnTriedCSFile = FALSE;

    }

    if( hECWDatasetMutex != NULL )
    {
        CPLDestroyMutex( hECWDatasetMutex );
        hECWDatasetMutex = NULL;
    }

    NCSecwShutdown();
}

This should make it into the GDAL 1.3.2 release.  Let me know
if you still see issues.

Thanks
Note: See TracTickets for help on using tickets.