Check that the connection pointer is not null before trying to call PQfinish after a PostgreSQL connection attempt failure
Also, add some documentation explaining why PQfinish must be called in this instance
@@ -33,7 +33,12 @@ impl RawConnection {
_ => {
let message = last_error_message(connection_ptr);
+ if !connection_ptr.is_null() {
+ // Note that even if the server connection attempt fails (as indicated by PQstatus),
+ // the application should call PQfinish to free the memory used by the PGconn object.
+ // https://www.postgresql.org/docs/current/libpq-connect.html
unsafe { PQfinish(connection_ptr) }
+ }
Err(ConnectionError::BadConnection(message))
}
