From: Christoph M. Becker Date: Thu, 2 Jul 2015 22:04:50 +0000 (+0200) Subject: Fix #69975: PHP segfaults when accessing nvarchar(max) defined columns X-Git-Tag: php-5.6.12RC1~62 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=16db4d1462bf3eacb93c0cd940f799160a284b24;p=php Fix #69975: PHP segfaults when accessing nvarchar(max) defined columns The SQL Server Native Client 11.0 and maybe other ODBC drivers report NVARCHAR(MAX) columns as SQL_WVARCHAR with size 0. This causes too small a buffer to be emalloc'd, likely causing a segfault in the following. As we don't know the real size of the column data, we treat such colums as SQL_WLONGVARCHAR. The related bug #67437 suggests that some drivers report a size of ~4GB. It is not certain that this is really the case (there might be some integer overflow involved, and anyway, there has been no feedback), so we do not cater for this now. However, it would not be hard to treat all sizes above a certain threshold in a similar way, i.e. as SQL_WLONGVARCHAR. --- diff --git a/ext/odbc/php_odbc.c b/ext/odbc/php_odbc.c index ddfbc4edbe..80af492638 100644 --- a/ext/odbc/php_odbc.c +++ b/ext/odbc/php_odbc.c @@ -1008,6 +1008,13 @@ int odbc_bindcols(odbc_result *result TSRMLS_DC) NULL, 0, NULL, &displaysize); } #endif + /* Workaround for drivers that report NVARCHAR(MAX) columns as SQL_WVARCHAR with size 0 (bug #69975) */ + if (result->values[i].coltype == SQL_WVARCHAR && displaysize == 0) { + result->values[i].coltype = SQL_WLONGVARCHAR; + result->values[i].value = NULL; + break; + } + /* Workaround for Oracle ODBC Driver bug (#50162) when fetching TIMESTAMP column */ if (result->values[i].coltype == SQL_TIMESTAMP) { displaysize += 3; diff --git a/ext/odbc/tests/bug69975.phpt b/ext/odbc/tests/bug69975.phpt new file mode 100644 index 0000000000..eca7564519 --- /dev/null +++ b/ext/odbc/tests/bug69975.phpt @@ -0,0 +1,32 @@ +--TEST-- +Bug #69975 (PHP segfaults when accessing nvarchar(max) defined columns) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +array(1) { + ["VARCHAR_COL"]=> + string(3) "foo" +} +ready +--CLEAN-- +