Skip to content

Commit a49555a

Browse files
committed
Fix #80147: BINARY strings may not be properly zero-terminated
We have to manually ensure that all strings fetched from a data source are zero-terminated. Closes GH-6213.
1 parent e74f89d commit a49555a

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ PHP NEWS
1414

1515
- ODBC:
1616
. Fixed bug #78470 (odbc_specialcolumns() no longer accepts $nullable). (cmb)
17+
. Fixed bug #80147 (BINARY strings may not be properly zero-terminated).
18+
(cmb)
1719

1820
- OPcache:
1921
. Fixed bug #80083 (Optimizer pass 6 removes variables used for ibm_db2 data

ext/odbc/php_odbc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,7 @@ PHP_FUNCTION(odbc_result)
22212221
if (rc != SQL_SUCCESS_WITH_INFO) {
22222222
field_str = zend_string_truncate(field_str, result->values[field_ind].vallen, 0);
22232223
}
2224+
ZSTR_VAL(field_str)[ZSTR_LEN(field_str)] = '\0';
22242225
RETURN_NEW_STR(field_str);
22252226
break;
22262227

ext/odbc/tests/bug80147.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Bug #80147 (BINARY strings may not be properly zero-terminated)
3+
--SKIPIF--
4+
<?php include 'skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
include 'config.inc';
8+
9+
$conn = odbc_connect($dsn, $user, $pass);
10+
11+
odbc_exec($conn, "CREATE TABLE bug80147 (id INT, whatever VARBINARY(50))");
12+
odbc_exec($conn, "INSERT INTO bug80147 VALUES (1, CONVERT(VARBINARY(50), 'whatever'))");
13+
14+
$res = odbc_exec($conn, "SELECT * FROM bug80147");
15+
odbc_binmode($res, ODBC_BINMODE_RETURN);
16+
odbc_fetch_row($res);
17+
var_dump(odbc_result($res, 'whatever'));
18+
?>
19+
--CLEAN--
20+
<?php
21+
include 'config.inc';
22+
23+
$conn = odbc_connect($dsn, $user, $pass);
24+
odbc_exec($conn, "DROP TABLE bug80147");
25+
?>
26+
--EXPECT--
27+
string(8) "whatever"

0 commit comments

Comments
 (0)