From: "Paul 'Shag' Walmsley" Date: Thu, 11 Jul 1996 03:02:44 -0500 (CDT) Subject: [PG95]: Another libpq robustness patch for the adventurous The previous patch that I posted for libpq was against fe-exec.c. The following patch is to prevent any inadvertent null pointer dereferences from fe-connect.c. To be extra-clear about what's going on here, this patch complements the one I recently posted - and re-posted - that was against src/libpq/fe-exec.c. The patch below is against src/libpq/fe-connect.c. I paid more attention to return values this time :-) - - Paul "Shag" Walmsley "Knowing is not enough." -- Hal Hartley, "Surviving Desire" - --------[ cut here ]--------------------------------------------------- *** src/libpq/fe-connect.c Thu Jul 11 02:43:34 1996 - --- src/libpq/fe-connect.c.new Thu Jul 11 02:39:44 1996 *************** *** 70,76 **** char *tmp; conn = (PGconn*)malloc(sizeof(PGconn)); ! conn->Pfout = NULL; conn->Pfin = NULL; conn->Pfdebug = NULL; - --- 70,81 ---- char *tmp; conn = (PGconn*)malloc(sizeof(PGconn)); ! ! if (!conn) { ! fprintf(stderr,"FATAL: pqsetdb() -- unable to allocate memory for a PGconn"); ! return (PGconn*)NULL; ! } ! conn->Pfout = NULL; conn->Pfin = NULL; conn->Pfdebug = NULL; *************** *** 307,315 **** void PQfinish(PGconn *conn) { ! if (conn->status == CONNECTION_OK) ! closePGconn(conn); ! freePGconn(conn); } /* PQreset : - --- 312,324 ---- void PQfinish(PGconn *conn) { ! if (!conn) { ! fprintf(stderr,"PQfinish() -- pointer to PGconn is null"); ! } else { ! if (conn->status == CONNECTION_OK) ! closePGconn(conn); ! freePGconn(conn); ! } } /* PQreset : *************** *** 319,326 **** - --- 328,339 ---- void PQreset(PGconn *conn) { + if (!conn) { + fprintf(stderr,"PQreset() -- pointer to PGconn is null"); + } else { closePGconn(conn); conn->status = connectDB(conn); + } } /* *************** *** 395,400 **** - --- 408,418 ---- char* PQdb(PGconn* conn) { + if (!conn) { + fprintf(stderr,"PQdb() -- pointer to PGconn is null"); + return (char *)NULL; + } + return conn->dbName; } *************** *** 401,406 **** - --- 419,429 ---- char* PQhost(PGconn* conn) { + if (!conn) { + fprintf(stderr,"PQhost() -- pointer to PGconn is null"); + return (char *)NULL; + } + return conn->pghost; } *************** *** 407,412 **** - --- 430,440 ---- char* PQoptions(PGconn* conn) { + if (!conn) { + fprintf(stderr,"PQoptions() -- pointer to PGconn is null"); + return (char *)NULL; + } + return conn->pgoptions; } *************** *** 413,418 **** - --- 441,451 ---- char* PQtty(PGconn* conn) { + if (!conn) { + fprintf(stderr,"PQtty() -- pointer to PGconn is null"); + return (char *)NULL; + } + return conn->pgtty; } *************** *** 419,424 **** - --- 452,462 ---- char* PQport(PGconn* conn) { + if (!conn) { + fprintf(stderr,"PQport() -- pointer to PGconn is null"); + return (char *)NULL; + } + return conn->pgport; } *************** *** 425,430 **** - --- 463,473 ---- ConnStatusType PQstatus(PGconn* conn) { + if (!conn) { + fprintf(stderr,"PQstatus() -- pointer to PGconn is null"); + return CONNECTION_BAD; + } + return conn->status; } *************** *** 431,436 **** - --- 474,484 ---- char* PQerrorMessage(PGconn* conn) { + if (!conn) { + fprintf(stderr,"PQerrorMessage() -- pointer to PGconn is null"); + return (char *)NULL; + } + return conn->errorMessage; } - ----------[ cut here ]---------------------------------------------------