Skip to content

Commit 2bd7f44

Browse files
author
Sandro Santilli
committed
Fix signed 32bit integer overflow in PostgreSQL provider
Fixes #13958 Includes test for signed integer attributes
1 parent a15f51e commit 2bd7f44

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/providers/postgres/qgspostgresconn.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,9 @@ qint64 QgsPostgresConn::getBinaryInt( QgsPostgresResult &queryResult, int row, i
12211221
oid = *( quint16 * )p;
12221222
if ( mSwapEndian )
12231223
oid = ntohs( oid );
1224+
/* cast to signed 16bit
1225+
* See http://hub.qgis.org/issues/14262 */
1226+
oid = ( qint16 )oid;
12241227
break;
12251228

12261229
case 6:
@@ -1268,6 +1271,9 @@ qint64 QgsPostgresConn::getBinaryInt( QgsPostgresResult &queryResult, int row, i
12681271
oid = *( quint32 * )p;
12691272
if ( mSwapEndian )
12701273
oid = ntohl( oid );
1274+
/* cast to signed 32bit
1275+
* See http://hub.qgis.org/issues/14262 */
1276+
oid = ( qint32 )oid;
12711277
break;
12721278
}
12731279

tests/src/python/test_provider_postgres.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ def test_unique(features, num_features):
147147
self.assertTrue(vl.isValid())
148148
test_unique([f for f in vl.getFeatures()], 4)
149149

150+
# See http://hub.qgis.org/issues/14262
151+
def testSignedIdentifiers(self):
152+
def test_query_attribute(dbconn, query, att, val, fidval):
153+
ql = QgsVectorLayer('%s table="%s" (g) key=\'%s\' sql=' % (dbconn, query.replace('"', '\\"'), att), "testgeom", "postgres")
154+
print query, att
155+
assert(ql.isValid())
156+
features = ql.getFeatures()
157+
att_idx = ql.fieldNameIndex(att)
158+
count = 0
159+
for f in features:
160+
count += 1
161+
self.assertEqual(f.attributes()[att_idx], val)
162+
#self.assertEqual(f.id(), val)
163+
self.assertEqual(count, 1)
164+
test_query_attribute(self.dbconn, '(SELECT -1::int4 i, NULL::geometry(Point) g)', 'i', -1, 1)
165+
test_query_attribute(self.dbconn, '(SELECT -1::int2 i, NULL::geometry(Point) g)', 'i', -1, 1)
166+
test_query_attribute(self.dbconn, '(SELECT -1::int8 i, NULL::geometry(Point) g)', 'i', -1, 1)
167+
test_query_attribute(self.dbconn, '(SELECT -65535::int8 i, NULL::geometry(Point) g)', 'i', -65535, 1)
168+
150169

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

0 commit comments

Comments
 (0)