/* * SSL-test */ #include #include static void exit_nicely (PGconn *conn) { std::cerr << "ERROR: " << PQerrorMessage (conn) << '\n'; PQfinish (conn); exit (1); } int main (int _argc, char *_argv[]) { PGconn *conn = PQconnectdb ("user=postgres dbname=template1 host=127.0.0.1 requiressl=1"); if (PQstatus (conn) == CONNECTION_BAD) exit_nicely (conn); PQsendQuery (conn, "select * from pg_class; select * from pg_type; select * from pg_proc"); for (;;) { std::cout << "before DANGEROUS section of code\n"; #ifndef NO_STACK_OVERFLOW_DEMO // This is a DANGEROUS code. If we call PQconsumeInput here, // after getting last PGresult, we will go into infinite // recursive call of pqsecure_read on SSL_ERROR_WANT_READ. PQconsumeInput (conn); if (PQisBusy (conn)) continue; // Call PQcounsumeInput on SSL connection when server // don't send any data to us is DANGEROUS. #else // This code is safe, becouse end of data determine before // call of PQconsumeInput and we don't go into pqsecure_read // when server have no data. if (PQisBusy (conn)) { PQconsumeInput (conn); continue; } #endif // But I think PQconsumeInput must be safe to be called in // any time, becouse, for example, we can use it not only // for async query processing, but for getting asynhronous // notification from server in case when we don't send any // query to it and can't be sure, that server have data for // us. std::cout << "after DANGEROUS section of code\n"; // When we don't use SSL this code in both case is completly // safe, becouse recv simple return 0 if no data avaliable. // I think this is good for SSL connection too. PGresult *res = PQgetResult (conn); if (res) { if (PQresultStatus (res) == PGRES_TUPLES_OK) std::cout << "SELECT\n"; else std::cerr << "UNKNOWN\n"; PQclear (res); continue; } break; } PQfinish(conn); return 0; }