php-src icon indicating copy to clipboard operation
php-src copied to clipboard

PDO_DBLIB overwrites check constraint custom messages

Open cnelms opened this issue 2 weeks ago • 1 comments

Description

It appears the PDO_DBLIB driver I am using for my PHP PDO connection is overwriting custom check constraint messages. When I bypass the driver and run the same queries this is what I get:

1>> insert into Local_contact (loc_contact, group_id, lc_name, updated)
2>> values ('****', 1, 'test', getDate())
3>> go
Msg 30078, Level 16, State 1:
Server 'ZING', Line 1:
Local Contact contains illegal characters

Command has been aborted.

1>> delete from Depot where depot = 'AARA'
2>> go
Msg 547, Level 16, State 1:
Server 'ZING', Procedure 'Depot_del_trig', Line 3:
Dependent foreign key constraint violation in a referential integrity constraint. dbname =  'caiprod', table name = 'Location', constraint name = 'History_location_fkey'.

Command has been aborted.

The first query, insert into Local_contact, returns this error "SQLSTATE[HY000]: General error: 20018 Command has been aborted.\n [20018] (severity 16) [insert into Local_contact ...". I was expecting the custom check constraint message "Local Contact contains illegal characters", but it returned "Command has been aborted."

The second query, delete from Depot, returns "Dependent foreign key constraint violation in a referential integrity constraint. dbname = 'caiprod', table name = 'Location', constraint name = 'History_location_fkey'.\n [20018] (severity 16) [delete from Depot ...". This is the correct error message that I was expecting.

I patched it in my own repo but wanted to report the issue and PR my fix.

Best, Carey Nelms

PHP Version

PHP 8.4.27 (cli) (built: Nov  5 2026 07:23:55) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.4.27, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.27, Copyright (c), by Zend Technologies

Operating System

Debian GNU/Linux 13 (trixie)

Database

Adaptive Server Enterprise/16.0 SP04 GA/EBF 29650 SMP/P/x86_64/SLES 12.4/asecorona/3202/64-bit/FBO/Wed Nov 18 09:12:58 2020

cnelms avatar Nov 14 '26 15:11 cnelms

I attempted to PR a patch to the 8.4 branch but received a permission denied message: Pushing to github.com:php/php-src.git ERROR: Permission to php/php-src.git denied to cnelms.

So I will send diffs and code for the patch. It involves two files.

  1. ext/pdo_dblib/pdo_dblib.c Screenshot 2026-11-14 at 8 37 15 AM
int pdo_dblib_msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate,
	int severity, char *msgtext, char *srvname, char *procname, int line)
{
	pdo_dblib_err *einfo;

	if (severity >= 11 && msgtext != NULL && *msgtext != '\0') {
		einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
		if (!einfo) {
			einfo = &DBLIB_G(err);
		}

		/*
		 * Preserve the first actual server error.
		 * Ignore subsequent messages, including severity 10
		 * "Command has been aborted."
		 */
		if (einfo->lastmsg == NULL) {
				einfo->lastmsg = estrdup(msgtext);
		}
	}

	return 0;
}
  1. ext/pdo_dblib/dblib_stmt.c Screenshot 2026-11-14 at 8 36 01 AM
static int pdo_dblib_stmt_execute(pdo_stmt_t *stmt)
{
	pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
	pdo_dblib_db_handle *H = S->H;

	dbsetuserdata(H->link, (BYTE*) &S->err);

	pdo_dblib_stmt_cursor_closer(stmt);

	/* Clear the previous statement's error information. */
	pdo_dblib_err_dtor(&S->err);
	S->err.severity = 0;
	S->err.oserr = 0;
	S->err.dberr = 0;
	strcpy(S->err.sqlstate, "00000");

	if (FAIL == dbcmd(H->link, ZSTR_VAL(stmt->active_query_string))) {
		return 0;
	}

	if (FAIL == dbsqlexec(H->link)) {
		return 0;
	}

	pdo_dblib_stmt_next_rowset_no_cancel(stmt);

	stmt->row_count = DBCOUNT(H->link);
	stmt->column_count = dbnumcols(H->link);

	return 1;
}

cnelms avatar Nov 14 '26 16:11 cnelms