xref: /PHP-8.1/ext/pdo_pgsql/tests/bug46274_2.phpt (revision 39131219)
1--TEST--
2Bug #46274 (pdo_pgsql - Segfault when using PDO::ATTR_STRINGIFY_FETCHES and blob)
3--EXTENSIONS--
4pdo
5pdo_pgsql
6--SKIPIF--
7<?php
8require __DIR__ . '/config.inc';
9require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
10PDOTest::skip();
11?>
12--FILE--
13<?php
14require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
15$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
16
17$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
18
19try {
20        @$db->query("SET bytea_output = 'escape'");
21} catch (Exception $e) {
22}
23
24$db->query('CREATE TABLE test_one_blob (id SERIAL NOT NULL, blob1 BYTEA)');
25
26$stmt = $db->prepare("INSERT INTO test_one_blob (blob1) VALUES (:foo)");
27
28$data = 'foo';
29$blob = fopen('php://memory', 'a');
30fwrite($blob, $data);
31rewind($blob);
32
33$stmt->bindparam(':foo', $blob, PDO::PARAM_LOB);
34$stmt->execute();
35
36$blob = '';
37$stmt->bindparam(':foo', $blob, PDO::PARAM_LOB);
38$stmt->execute();
39
40$data = '';
41$blob = fopen('php://memory', 'a');
42fwrite($blob, $data);
43rewind($blob);
44
45$stmt->bindparam(':foo', $blob, PDO::PARAM_LOB);
46$stmt->execute();
47
48$blob = NULL;
49$stmt->bindparam(':foo', $blob, PDO::PARAM_LOB);
50$stmt->execute();
51
52$res = $db->query("SELECT blob1 from test_one_blob");
53// Resource
54var_dump($x = $res->fetch());
55var_dump(fread($x['blob1'], 10));
56
57// Resource
58var_dump($res->fetch());
59var_dump(fread($x['blob1'], 10));
60
61// Resource
62var_dump($res->fetch());
63var_dump(fread($x['blob1'], 10));
64
65// NULL
66var_dump($res->fetch());
67
68$db->query('DROP TABLE test_one_blob');
69
70?>
71--EXPECTF--
72array(2) {
73  ["blob1"]=>
74  resource(%d) of type (stream)
75  [0]=>
76  resource(%d) of type (stream)
77}
78string(3) "foo"
79array(2) {
80  ["blob1"]=>
81  resource(%d) of type (stream)
82  [0]=>
83  resource(%d) of type (stream)
84}
85string(0) ""
86array(2) {
87  ["blob1"]=>
88  resource(%d) of type (stream)
89  [0]=>
90  resource(%d) of type (stream)
91}
92string(0) ""
93array(2) {
94  ["blob1"]=>
95  NULL
96  [0]=>
97  NULL
98}
99