Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix signed 32bit integer overflow in PostgreSQL provider
Fixes #13958
Includes test for signed integer attributes
  • Loading branch information
Sandro Santilli committed Feb 16, 2016
1 parent a15f51e commit 2bd7f44
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -1221,6 +1221,9 @@ qint64 QgsPostgresConn::getBinaryInt( QgsPostgresResult &queryResult, int row, i
oid = *( quint16 * )p;
if ( mSwapEndian )
oid = ntohs( oid );
/* cast to signed 16bit
* See http://hub.qgis.org/issues/14262 */
oid = ( qint16 )oid;
break;

case 6:
Expand Down Expand Up @@ -1268,6 +1271,9 @@ qint64 QgsPostgresConn::getBinaryInt( QgsPostgresResult &queryResult, int row, i
oid = *( quint32 * )p;
if ( mSwapEndian )
oid = ntohl( oid );
/* cast to signed 32bit
* See http://hub.qgis.org/issues/14262 */
oid = ( qint32 )oid;
break;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -147,6 +147,25 @@ def test_unique(features, num_features):
self.assertTrue(vl.isValid())
test_unique([f for f in vl.getFeatures()], 4)

# See http://hub.qgis.org/issues/14262
def testSignedIdentifiers(self):
def test_query_attribute(dbconn, query, att, val, fidval):
ql = QgsVectorLayer('%s table="%s" (g) key=\'%s\' sql=' % (dbconn, query.replace('"', '\\"'), att), "testgeom", "postgres")
print query, att
assert(ql.isValid())
features = ql.getFeatures()
att_idx = ql.fieldNameIndex(att)
count = 0
for f in features:
count += 1
self.assertEqual(f.attributes()[att_idx], val)
#self.assertEqual(f.id(), val)
self.assertEqual(count, 1)
test_query_attribute(self.dbconn, '(SELECT -1::int4 i, NULL::geometry(Point) g)', 'i', -1, 1)
test_query_attribute(self.dbconn, '(SELECT -1::int2 i, NULL::geometry(Point) g)', 'i', -1, 1)
test_query_attribute(self.dbconn, '(SELECT -1::int8 i, NULL::geometry(Point) g)', 'i', -1, 1)
test_query_attribute(self.dbconn, '(SELECT -65535::int8 i, NULL::geometry(Point) g)', 'i', -65535, 1)


if __name__ == '__main__':
unittest.main()

0 comments on commit 2bd7f44

Please sign in to comment.