Skip to content

unsafe destroy when the program call OGRCoordinateTransformation::DestoryCT in loop #11607

@myBestLove

Description

@myBestLove

What is the bug?

Using OGRCoordinateTransformation::DestoryCT in a loop during which multiple threads are executed can cause segment fault.

when OGRCoordinateTransformation::DestoryCT,OGRProjCT cache will be destroyed.

Steps to reproduce the issue

Environment:
centos 7
c++ 4.8.5

reproduce:

int thread_test1(const char * filename)
{
    for (int r=0; r<100; r++)
    {
        GDALDataset* dataset = (GDALDataset*)GDALOpen(filename, GA_ReadOnly);
        if (dataset == NULL)
        {
            printf("error open %s\n", filename);
            return -1;
        }
      
        GDALRasterBand* band = dataset->GetRasterBand(1);
        GDALDataType datatype = band->GetRasterDataType();
        double data;
        for(int i=0;i<10;i++)
            band->RasterIO(GF_Read, i, i, 1, 1, &data, 1, 1, datatype, 0, 0);
        OGRCoordinateTransformation* ct = NULL;
        OGRSpatialReference mctRef;
        mctRef.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
        mctRef.importFromEPSG(3857);
        OGRSpatialReference wgsRef;
        wgsRef.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
        wgsRef.importFromEPSG(4326);
        ct = OGRCreateCoordinateTransformation(&mctRef,&wgsRef);
        double x = 10, y=10;
        if (nullptr == ct)continue;
        ct->Transform(1, &x, &y);
        if (ct) 
            OGRCoordinateTransformation::DestroyCT(ct);


        GDALClose(dataset);
    }
    return 0;
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        return -1;
    }
    const char * filename = argv[1];    
    const int numThreads = 10;
    GDALAllRegister();  
    for(int i=0; i < 100; i++)
    {

        std::thread threads[numThreads];

        for (int i = 0; i < numThreads; ++i)
        {
            threads[i] = std::thread(thread_test1, filename);
        }

        std::cout << "Hello from main thread!" << std::endl;
        
        for (int i = 0; i < numThreads; ++i)
        {
            threads[i].join();
        }

    }
    GDALDestroyDriverManager(); 
    return 0;
}

Versions and provenance

gdal3.7.0 from https://github.com/OSGeo/gdal/releases/tag/v3.7.0

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions