From: Ilia Alshanetsky Date: Tue, 25 Nov 2003 22:23:54 +0000 (+0000) Subject: MFH: Fixed bug #25963 (PostgreSQL error message include \n characters). X-Git-Tag: php-4.3.5RC1~171 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=67ba3f4c30dd8ad5a85526ccb80547c92813a775;p=php MFH: Fixed bug #25963 (PostgreSQL error message include \n characters). --- diff --git a/NEWS b/NEWS index 9dfa825b6c..23b1897dcd 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,8 @@ PHP 4 NEWS ?? ??? 2003, Version 4.3.5 - Fixed header handler in NSAPI SAPI module (header->replace was ignored, send_default_content_type now sends value from php.ini). (Uwe Schindler) +- Fixed bug #25963 (PostgreSQL error message include \n characters). + (Marcus, Ilia) - Fixed bug #26376 (typo in ext/odbc/config.m4: DBMaker test fails). (Jani) - Fixed bug #26384 (crash in domxslt->process()). (Ilia) - Fixed bug #26355 (flock() doesn't initialize the wouldblock argument). (Ilia) diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 2f478bab3c..4adafa3463 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -214,6 +214,31 @@ static int le_link, le_plink, le_result, le_lofp, le_string; ZEND_DECLARE_MODULE_GLOBALS(pgsql); +/* {{{ _php_pgsql_trim_message */ +static char * _php_pgsql_trim_message(const char *message, int *len) +{ + register int i = strlen(message)-1; + + if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') { + --i; + } + while (i && (message[i] == '\r' || message[i] == '\n')) { + --i; + } + ++i; + if (len) { + *len = i; + } + return estrndup(message, i); +} +/* }}} */ + +#define PHP_PQ_ERROR(text, pgsql) { \ + char *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql), NULL); \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, text, msgbuf); \ + efree(msgbuf); \ +} \ + /* {{{ php_pgsql_set_default_link */ static void php_pgsql_set_default_link(int id TSRMLS_DC) @@ -558,8 +583,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) pgsql=PQsetdb(host,port,options,tty,dbname); } if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, - "Unable to connect to PostgreSQL server: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s.", pgsql) if (pgsql) { PQfinish(pgsql); } @@ -644,7 +668,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) pgsql = PQsetdb(host,port,options,tty,dbname); } if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to PostgreSQL server: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s.", pgsql); goto err; } @@ -789,7 +813,8 @@ static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type Z_STRVAL_P(return_value) = PQdb(pgsql); break; case PHP_PG_ERROR_MESSAGE: - Z_STRVAL_P(return_value) = PQerrorMessage(pgsql); + Z_STRVAL_P(return_value) = _php_pgsql_trim_message(PQerrorMessage(pgsql), &(Z_STRLEN_P(return_value))); + goto done; break; case PHP_PG_OPTIONS: Z_STRVAL_P(return_value) = PQoptions(pgsql); @@ -813,6 +838,7 @@ static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type Z_STRLEN_P(return_value) = 0; Z_STRVAL_P(return_value) = (char *) estrdup(""); } +done: Z_TYPE_P(return_value) = IS_STRING; } /* }}} */ @@ -963,7 +989,7 @@ PHP_FUNCTION(pg_query) case PGRES_BAD_RESPONSE: case PGRES_NONFATAL_ERROR: case PGRES_FATAL_ERROR: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Query failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("Query failed: %s.", pgsql); PQclear(pgsql_result); RETURN_FALSE; break; @@ -2395,7 +2421,7 @@ PHP_FUNCTION(pg_end_copy) result = PQendcopy(pgsql); if (result!=0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Query failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("Query failed: %s.", pgsql); RETURN_FALSE; } RETURN_TRUE; @@ -2439,7 +2465,7 @@ PHP_FUNCTION(pg_put_line) result = PQputline(pgsql, Z_STRVAL_PP(query)); if (result==EOF) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Query failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("Query failed: %s.", pgsql); RETURN_FALSE; } RETURN_TRUE; @@ -2504,7 +2530,7 @@ PHP_FUNCTION(pg_copy_to) while (!copydone) { if ((ret = PQgetline(pgsql, copybuf, COPYBUFSIZ))) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "getline failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("getline failed: %s.", pgsql); RETURN_FALSE; } @@ -2538,7 +2564,7 @@ PHP_FUNCTION(pg_copy_to) } } if (PQendcopy(pgsql)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "endcopy failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("endcopy failed: %s.", pgsql); RETURN_FALSE; } while ((pgsql_result = PQgetResult(pgsql))) { @@ -2551,7 +2577,7 @@ PHP_FUNCTION(pg_copy_to) break; default: PQclear(pgsql_result); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Copy command failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("Copy command failed: %s.", pgsql); RETURN_FALSE; break; } @@ -2619,18 +2645,18 @@ PHP_FUNCTION(pg_copy_from) strcat(query, "\n"); if (PQputline(pgsql, query)) { efree(query); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "copy failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("copy failed: %s.", pgsql); RETURN_FALSE; } efree(query); zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos); } if (PQputline(pgsql, "\\.\n") == EOF) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "putline failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("putline failed: %s.", pgsql); RETURN_FALSE; } if (PQendcopy(pgsql)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "endcopy failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("endcopy failed: %s.", pgsql); RETURN_FALSE; } while ((pgsql_result = PQgetResult(pgsql))) { @@ -2644,7 +2670,7 @@ PHP_FUNCTION(pg_copy_from) break; default: PQclear(pgsql_result); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Copy command failed: %s.", PQerrorMessage(pgsql)); + PHP_PQ_ERROR("Copy command failed: %s.", pgsql); RETURN_FALSE; break; }