Skip to content

Commit e617e82

Browse files
committed
Fix PDO OCI Bug #60994 (Reading a multibyte CLOB caps at 8192 chars)
1 parent be4dd60 commit e617e82

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

ext/pdo_oci/config.m4

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ if test "$PHP_PDO_OCI" != "no"; then
182182
-L$PDO_OCI_LIB_DIR $PDO_OCI_SHARED_LIBADD
183183
])
184184

185+
dnl Can handle bytes vs. characters?
186+
PHP_CHECK_LIBRARY(clntsh, OCILobRead2,
187+
[
188+
AC_DEFINE(HAVE_OCILOBREAD2,1,[ ])
189+
], [], [
190+
-L$PDO_OCI_LIB_DIR $PDO_OCI_SHARED_LIBADD
191+
])
192+
185193
PHP_CHECK_PDO_INCLUDES
186194

187195
PHP_NEW_EXTENSION(pdo_oci, pdo_oci.c oci_driver.c oci_statement.c, $ext_shared,,-I$pdo_cv_inc_path)

ext/pdo_oci/oci_statement.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ struct oci_lob_self {
620620
OCILobLocator *lob;
621621
oci_lob_env *E;
622622
ub4 offset;
623+
ub1 csfrm;
623624
};
624625

625626
static ssize_t oci_blob_write(php_stream *stream, const char *buf, size_t count)
@@ -645,23 +646,34 @@ static ssize_t oci_blob_write(php_stream *stream, const char *buf, size_t count)
645646
static ssize_t oci_blob_read(php_stream *stream, char *buf, size_t count)
646647
{
647648
struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
648-
ub4 amt;
649-
sword r;
649+
#if HAVE_OCILOBREAD2
650+
oraub8 byte_amt = (oraub8) count;
651+
oraub8 char_amt = 0;
650652

651-
amt = (ub4) count;
652-
r = OCILobRead(self->E->svc, self->E->err, self->lob,
653-
&amt, self->offset, buf, (ub4) count,
653+
sword r = OCILobRead2(self->E->svc, self->E->err, self->lob,
654+
&byte_amt, &char_amt, (oraub8) self->offset, buf, (oraub8) count,
655+
OCI_ONE_PIECE, NULL, NULL, 0, self->csfrm);
656+
#else
657+
ub4 byte_amt = (ub4) count;
658+
659+
sword r = OCILobRead(self->E->svc, self->E->err, self->lob,
660+
&byte_amt, self->offset, buf, (ub4) count,
654661
NULL, NULL, 0, SQLCS_IMPLICIT);
662+
#endif
655663

656664
if (r != OCI_SUCCESS && r != OCI_NEED_DATA) {
657-
return (size_t)-1;
665+
return (ssize_t)-1;
658666
}
659667

660-
self->offset += amt;
661-
if (amt < count) {
668+
#if HAVE_OCILOBREAD2
669+
self->offset += self->csfrm == 0 ? byte_amt : char_amt;
670+
#else
671+
self->offset += byte_amt;
672+
#endif
673+
if (byte_amt < count) {
662674
stream->eof = 1;
663675
}
664-
return amt;
676+
return byte_amt;
665677
}
666678

667679
static int oci_blob_close(php_stream *stream, int close_handle)
@@ -728,6 +740,8 @@ static php_stream *oci_create_lob_stream(zval *dbh, pdo_stmt_t *stmt, OCILobLoca
728740
self->E->svc = self->S->H->svc;
729741
self->E->err = self->S->err;
730742

743+
OCILobCharSetForm(self->S->H->env, self->S->err, self->lob, &self->csfrm);
744+
731745
stm = php_stream_alloc(&oci_blob_stream_ops, self, 0, "r+b");
732746

733747
if (stm) {

ext/pdo_oci/tests/bug60994.phpt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
1818
$dbh->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
1919

2020
@$dbh->exec('DROP TABLE pdo_oci_bug60994');
21-
$dbh->exec('CREATE TABLE pdo_oci_bug60994 (id NUMBER, data CLOB)');
21+
$dbh->exec('CREATE TABLE pdo_oci_bug60994 (id NUMBER, data CLOB, data2 NCLOB)');
2222

2323
$id = null;
24-
$insert = $dbh->prepare('INSERT INTO pdo_oci_bug60994 (id, data) VALUES (:id, :data)');
24+
$insert = $dbh->prepare('INSERT INTO pdo_oci_bug60994 (id, data, data2) VALUES (:id, :data, :data2)');
2525
$insert->bindParam(':id', $id, \PDO::PARAM_STR);
26-
$select = $dbh->prepare("SELECT data FROM pdo_oci_bug60994 WHERE id = :id");
26+
$select = $dbh->prepare("SELECT data, data2 FROM pdo_oci_bug60994 WHERE id = :id");
2727

2828

2929
echo PHP_EOL, 'Test 1: j', PHP_EOL;
3030
$string1 = 'abc' . str_repeat('j', 8187) . 'xyz'; // 8193 chars total works fine here (even 1 million works fine, subject to memory_limit)
3131
$id = 1;
3232
$insert->bindParam(':data', $string1, \PDO::PARAM_STR, strlen($string1)); // length in bytes
33+
$insert->bindParam(':data2', $string1, \PDO::PARAM_STR, strlen($string1));
3334
$insert->execute();
3435
$select->bindParam(':id', $id, \PDO::PARAM_STR);
3536
$select->execute();
@@ -41,12 +42,15 @@ echo 'size of string1 is ', strlen($string1), ' bytes, ', mb_strlen($string1), '
4142
echo 'size of stream1 is ', strlen($stream1), ' bytes, ', mb_strlen($stream1), ' chars.', PHP_EOL;
4243
echo 'beg of stream1 is ', $start1, PHP_EOL;
4344
echo 'end of stream1 is ', $ending1, PHP_EOL;
44-
45+
if ($string1 != $stream1 || $stream1 != stream_get_contents($row['DATA2'])) {
46+
echo 'Expected nclob value to match clob value for stream1', PHP_EOL;
47+
}
4548

4649
echo PHP_EOL, 'Test 2: £', PHP_EOL;
4750
$string2 = 'abc' . str_repeat('£', 8187) . 'xyz'; // 8193 chars total is when it breaks
4851
$id = 2;
4952
$insert->bindParam(':data', $string2, \PDO::PARAM_STR, strlen($string2)); // length in bytes
53+
$insert->bindParam(':data2', $string2, \PDO::PARAM_STR, strlen($string2));
5054
$insert->execute();
5155
$select->bindParam(':id', $id, \PDO::PARAM_STR);
5256
$select->execute();
@@ -58,12 +62,15 @@ echo 'size of string2 is ', strlen($string2), ' bytes, ', mb_strlen($string2), '
5862
echo 'size of stream2 is ', strlen($stream2), ' bytes, ', mb_strlen($stream2), ' chars.', PHP_EOL;
5963
echo 'beg of stream2 is ', $start2, PHP_EOL;
6064
echo 'end of stream2 is ', $ending2, PHP_EOL;
61-
65+
if ($string2 != $stream2 || $stream2 != stream_get_contents($row['DATA2'])) {
66+
echo 'Expected nclob value to match clob value for stream2', PHP_EOL;
67+
}
6268

6369
echo PHP_EOL, 'Test 3: Җ', PHP_EOL;
6470
$string3 = 'abc' . str_repeat('Җ', 8187) . 'xyz'; // 8193 chars total is when it breaks
6571
$id = 3;
6672
$insert->bindParam(':data', $string3, \PDO::PARAM_STR, strlen($string3)); // length in bytes
73+
$insert->bindParam(':data2', $string3, \PDO::PARAM_STR, strlen($string3));
6774
$insert->execute();
6875
$select->bindParam(':id', $id, \PDO::PARAM_STR);
6976
$select->execute();
@@ -75,12 +82,15 @@ echo 'size of string3 is ', strlen($string3), ' bytes, ', mb_strlen($string3), '
7582
echo 'size of stream3 is ', strlen($stream3), ' bytes, ', mb_strlen($stream3), ' chars.', PHP_EOL;
7683
echo 'beg of stream3 is ', $start3, PHP_EOL;
7784
echo 'end of stream3 is ', $ending3, PHP_EOL;
78-
85+
if ($string3 != $stream3 || $stream3 != stream_get_contents($row['DATA2'])) {
86+
echo 'Expected nclob value to match clob value for stream3', PHP_EOL;
87+
}
7988

8089
echo PHP_EOL, 'Test 4: の', PHP_EOL;
8190
$string4 = 'abc' . str_repeat('', 8187) . 'xyz'; // 8193 chars total is when it breaks
8291
$id = 4;
8392
$insert->bindParam(':data', $string4, \PDO::PARAM_STR, strlen($string4)); // length in bytes
93+
$insert->bindParam(':data2', $string4, \PDO::PARAM_STR, strlen($string4));
8494
$insert->execute();
8595
$select->bindParam(':id', $id, \PDO::PARAM_STR);
8696
$select->execute();
@@ -92,13 +102,14 @@ echo 'size of string4 is ', strlen($string4), ' bytes, ', mb_strlen($string4), '
92102
echo 'size of stream4 is ', strlen($stream4), ' bytes, ', mb_strlen($stream4), ' chars.', PHP_EOL;
93103
echo 'beg of stream4 is ', $start4, PHP_EOL;
94104
echo 'end of stream4 is ', $ending4, PHP_EOL;
105+
if ($string4 != $stream4 || $stream4 != stream_get_contents($row['DATA2'])) {
106+
echo 'Expected nclob value to match clob value for stream4', PHP_EOL;
107+
}
95108
?>
96-
--XFAIL--
97-
Fails due to Bug 60994
98109
--EXPECT--
99110
Test 1: j
100-
size of string1 is 1000006 bytes, 1000006 chars.
101-
size of stream1 is 1000006 bytes, 1000006 chars.
111+
size of string1 is 8193 bytes, 8193 chars.
112+
size of stream1 is 8193 bytes, 8193 chars.
102113
beg of stream1 is abcjjjjjjj
103114
end of stream1 is jjjjjjjxyz
104115

0 commit comments

Comments
 (0)