@@ -207,15 +207,17 @@ copy_connection(PGconn *conn)
207207 PQconninfoOption * opts = PQconninfo (conn );
208208 const char * * keywords ;
209209 const char * * vals ;
210- int nopts = 1 ;
211- int i = 0 ;
210+ int nopts = 0 ;
211+ int i ;
212212
213213 for (PQconninfoOption * opt = opts ; opt -> keyword != NULL ; ++ opt )
214214 nopts ++ ;
215+ nopts ++ ; /* for the NULL terminator */
215216
216217 keywords = pg_malloc (sizeof (char * ) * nopts );
217218 vals = pg_malloc (sizeof (char * ) * nopts );
218219
220+ i = 0 ;
219221 for (PQconninfoOption * opt = opts ; opt -> keyword != NULL ; ++ opt )
220222 {
221223 if (opt -> val )
@@ -1406,6 +1408,110 @@ test_prepared(PGconn *conn)
14061408 fprintf (stderr , "ok\n" );
14071409}
14081410
1411+ /*
1412+ * Test max_protocol_version options.
1413+ */
1414+ static void
1415+ test_protocol_version (PGconn * conn )
1416+ {
1417+ const char * * keywords ;
1418+ const char * * vals ;
1419+ int nopts ;
1420+ PQconninfoOption * opts = PQconninfo (conn );
1421+ int protocol_version ;
1422+ int max_protocol_version_index ;
1423+ int i ;
1424+
1425+ /*
1426+ * Prepare keywords/vals arrays, copied from the existing connection, with
1427+ * an extra slot for 'max_protocol_version'.
1428+ */
1429+ nopts = 0 ;
1430+ for (PQconninfoOption * opt = opts ; opt -> keyword != NULL ; ++ opt )
1431+ nopts ++ ;
1432+ nopts ++ ; /* max_protocol_version */
1433+ nopts ++ ; /* NULL terminator */
1434+
1435+ keywords = pg_malloc0 (sizeof (char * ) * nopts );
1436+ vals = pg_malloc0 (sizeof (char * ) * nopts );
1437+
1438+ i = 0 ;
1439+ for (PQconninfoOption * opt = opts ; opt -> keyword != NULL ; ++ opt )
1440+ {
1441+ if (opt -> val )
1442+ {
1443+ keywords [i ] = opt -> keyword ;
1444+ vals [i ] = opt -> val ;
1445+ i ++ ;
1446+ }
1447+ }
1448+
1449+ max_protocol_version_index = i ;
1450+ keywords [i ] = "max_protocol_version" ; /* value is filled in below */
1451+ i ++ ;
1452+ keywords [i ] = vals [i ] = NULL ;
1453+
1454+ /*
1455+ * Test max_protocol_version=3.0
1456+ */
1457+ vals [max_protocol_version_index ] = "3.0" ;
1458+ conn = PQconnectdbParams (keywords , vals , false);
1459+
1460+ if (PQstatus (conn ) != CONNECTION_OK )
1461+ pg_fatal ("Connection to database failed: %s" ,
1462+ PQerrorMessage (conn ));
1463+
1464+ protocol_version = PQfullProtocolVersion (conn );
1465+ if (protocol_version != 30000 )
1466+ pg_fatal ("expected 30000, got %d" , protocol_version );
1467+
1468+ PQfinish (conn );
1469+
1470+ /*
1471+ * Test max_protocol_version=3.1. It's not valid, we went straight from
1472+ * 3.0 to 3.2.
1473+ */
1474+ vals [max_protocol_version_index ] = "3.1" ;
1475+ conn = PQconnectdbParams (keywords , vals , false);
1476+
1477+ if (PQstatus (conn ) != CONNECTION_BAD )
1478+ pg_fatal ("Connecting with max_protocol_version 3.1 should have failed." );
1479+
1480+ PQfinish (conn );
1481+
1482+ /*
1483+ * Test max_protocol_version=3.2
1484+ */
1485+ vals [max_protocol_version_index ] = "3.2" ;
1486+ conn = PQconnectdbParams (keywords , vals , false);
1487+
1488+ if (PQstatus (conn ) != CONNECTION_OK )
1489+ pg_fatal ("Connection to database failed: %s" ,
1490+ PQerrorMessage (conn ));
1491+
1492+ protocol_version = PQfullProtocolVersion (conn );
1493+ if (protocol_version != 30002 )
1494+ pg_fatal ("expected 30002, got %d" , protocol_version );
1495+
1496+ PQfinish (conn );
1497+
1498+ /*
1499+ * Test max_protocol_version=latest. 'latest' currently means '3.2'.
1500+ */
1501+ vals [max_protocol_version_index ] = "latest" ;
1502+ conn = PQconnectdbParams (keywords , vals , false);
1503+
1504+ if (PQstatus (conn ) != CONNECTION_OK )
1505+ pg_fatal ("Connection to database failed: %s" ,
1506+ PQerrorMessage (conn ));
1507+
1508+ protocol_version = PQfullProtocolVersion (conn );
1509+ if (protocol_version != 30002 )
1510+ pg_fatal ("expected 30002, got %d" , protocol_version );
1511+
1512+ PQfinish (conn );
1513+ }
1514+
14091515/* Notice processor: print notices, and count how many we got */
14101516static void
14111517notice_processor (void * arg , const char * message )
0 commit comments