@@ -484,7 +484,7 @@ GDALDriverH CPL_STDCALL GDALGetDriver(int iDriver)
484484 *
485485 * @param poDriver the driver to register.
486486 *
487- * @return the index of the new installed driver.
487+ * @return the index of the new installed driver, or -1 in case of error
488488 */
489489
490490int GDALDriverManager::RegisterDriver (GDALDriver *poDriver)
@@ -496,12 +496,29 @@ int GDALDriverManager::RegisterDriver(GDALDriver *poDriver, bool bHidden)
496496{
497497 CPLMutexHolderD (&hDMMutex);
498498
499+ const char *pszDriverName = poDriver->GetDescription ();
500+ if (pszDriverName[0 ] == 0 )
501+ {
502+ CPLError (CE_Failure, CPLE_AppDefined,
503+ " Cannot register driver with empty name" );
504+ return -1 ;
505+ }
506+ if (pszDriverName[0 ] == ' -' )
507+ {
508+ // Because GDALDataset::Open() considers that strings in the allowed
509+ // drivers list starting with dash mean to exclude a driver.
510+ CPLError (
511+ CE_Failure, CPLE_AppDefined,
512+ " Cannot register driver whose name starts with a dash character" );
513+ return -1 ;
514+ }
515+
499516 /* -------------------------------------------------------------------- */
500517 /* If it is already registered, just return the existing */
501518 /* index. */
502519 /* -------------------------------------------------------------------- */
503520 if (!m_bInDeferredDriverLoading &&
504- GetDriverByName_unlocked (poDriver-> GetDescription () ) != nullptr )
521+ GetDriverByName_unlocked (pszDriverName ) != nullptr )
505522 {
506523 for (int i = 0 ; i < nDrivers; ++i)
507524 {
@@ -536,39 +553,38 @@ int GDALDriverManager::RegisterDriver(GDALDriver *poDriver, bool bHidden)
536553 poDriver->GetMetadataItem (GDAL_DCAP_GNM) == nullptr )
537554 {
538555 CPLDebug (" GDAL" , " Assuming DCAP_RASTER for driver %s. Please fix it." ,
539- poDriver-> GetDescription () );
556+ pszDriverName );
540557 poDriver->SetMetadataItem (GDAL_DCAP_RASTER, " YES" );
541558 }
542559
543560 if (poDriver->GetMetadataItem (GDAL_DMD_OPENOPTIONLIST) != nullptr &&
544561 poDriver->pfnIdentify == nullptr &&
545562 poDriver->pfnIdentifyEx == nullptr &&
546- !STARTS_WITH_CI (poDriver-> GetDescription () , " Interlis" ))
563+ !STARTS_WITH_CI (pszDriverName , " Interlis" ))
547564 {
548565 CPLDebug (" GDAL" ,
549566 " Driver %s that defines GDAL_DMD_OPENOPTIONLIST must also "
550567 " implement Identify(), so that it can be used" ,
551- poDriver-> GetDescription () );
568+ pszDriverName );
552569 }
553570
554571 if (poDriver->pfnVectorTranslateFrom != nullptr )
555572 poDriver->SetMetadataItem (GDAL_DCAP_VECTOR_TRANSLATE_FROM, " YES" );
556573
557574 if (m_bInDeferredDriverLoading &&
558- cpl::contains (oMapNameToDrivers,
559- CPLString (poDriver->GetDescription ()).toupper ()))
575+ cpl::contains (oMapNameToDrivers, CPLString (pszDriverName).toupper ()))
560576 {
561- if (cpl::contains (m_oMapRealDrivers, poDriver-> GetDescription () ))
577+ if (cpl::contains (m_oMapRealDrivers, pszDriverName ))
562578 {
563579 CPLError (
564580 CE_Failure, CPLE_AppDefined,
565581 " RegisterDriver() in m_bInDeferredDriverLoading: %s already "
566582 " registered!" ,
567- poDriver-> GetDescription () );
583+ pszDriverName );
568584 delete poDriver;
569585 return -1 ;
570586 }
571- m_oMapRealDrivers[poDriver-> GetDescription () ] =
587+ m_oMapRealDrivers[pszDriverName ] =
572588 std::unique_ptr<GDALDriver>(poDriver);
573589 return -1 ;
574590 }
@@ -592,10 +608,9 @@ int GDALDriverManager::RegisterDriver(GDALDriver *poDriver, bool bHidden)
592608 papoDrivers[nDrivers] = poDriver;
593609 ++nDrivers;
594610
595- oMapNameToDrivers[CPLString (poDriver->GetDescription ()).toupper ()] =
596- poDriver;
611+ oMapNameToDrivers[CPLString (pszDriverName).toupper ()] = poDriver;
597612
598- if (EQUAL (poDriver-> GetDescription () , " MEM" ) &&
613+ if (EQUAL (pszDriverName , " MEM" ) &&
599614 oMapNameToDrivers.find (" MEMORY" ) == oMapNameToDrivers.end ())
600615 {
601616 // Instantiate a Memory driver, that is the same as the MEM one,
0 commit comments