Opened 11 years ago

Last modified 11 years ago

#1480 closed defect (fixed)

OpenTable bug in ogrpgdatasource.cpp

Reported by: sleary@… Owned by: warmerdam
Priority: highest Milestone:
Component: OGR_SRS Version: unspecified
Severity: major Keywords:
Cc:

Description

OpenTable does not check if the table exists before it adds it as a layer to the datasource. This then causes GetLayerByName to fail to ever return null when a layer does not exist as the layer is *ALWAYS* opened by the call to OpenTable.

This can be demonstrated by executing the following pseudocode


for (int i=0;i < 100; i++)
 {
   pgDatasource->GetLayerByName(RandomNameGenerator());
   std::cout << "LAYER COUNT " << pgDataSource->GetLayerCount() << std::endl; 
 }

The prototype for RandomNameGenerator is: - char *RandomNameGenerator();

Each iteration increases the layer count even though it should not as the random    
names do not exist in the database.

See Also GetLayerByName() which uses it as : -

    OpenTable( pszName, NULL, TRUE, FALSE );
    
    if(GetLayerCount() == count+1) return GetLayer(count);

    return NULL; // (Unreachable code)

Change History (4)

comment:1 Changed 11 years ago by sleary@…

--

OpenTable should not be used in GetLayerByName. Instead some FindTable should be used instead.


comment:2 Changed 11 years ago by sleary@…

<mloskot> plasmab: I have some considerations:
<mloskot> 1. You are opening a datasource, so OGRPGDataSource::Open() scans
          for tables and calls OpenTable for every table, to register it as a
          layer.
<mloskot> 2. You call GetLayerByName() for some layer, let's say non-existing
          one
<mloskot> 3. Then OGRPGDataSource need to provide some FindTable() function
          which is called from OpenTable and if table is found, register it
<mloskot> EOF
<mloskot> FindTable() could be based on logic from Open() function.

comment:3 Changed 11 years ago by warmerdam

I've changed OGRPGTableLayer::ReadTableDefinition() to return a NULL feature
definition if no records (fields) are found for the table under the presumption
that the table does not exist.  This means that tables with zero fields will
not be considered valid now. 

Also, the constructor, OpenTable() and GetLayerByName() are now setup properly
to recover from such failed layer initialization. 

Fixed in trunk (r10770 and r10771)

Added tests to autotest/ogr/ogr_pg.py (r10772)

migrated fix into 1.4.x branch (r10773)

comment:4 Changed 11 years ago by szekerest

*** Bug 1479 has been marked as a duplicate of this bug. ***
Note: See TracTickets for help on using tickets.