Skip to content

Commit 6eb23e2

Browse files
committed
ValueError if lengths is less than 0
1 parent 13693aa commit 6eb23e2

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

ext/pgsql/pgsql.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,30 +2569,31 @@ PHP_FUNCTION(pg_lo_close)
25692569
PHP_FUNCTION(pg_lo_read)
25702570
{
25712571
zval *pgsql_id;
2572-
zend_long len;
2573-
size_t buf_len = PGSQL_LO_READ_BUF_SIZE;
2572+
zend_long buffer_length = PGSQL_LO_READ_BUF_SIZE;
25742573
int nbytes;
25752574
zend_string *buf;
25762575
pgLofp *pgsql;
25772576

2578-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pgsql_id, &len) == FAILURE) {
2577+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pgsql_id, &buffer_length) == FAILURE) {
25792578
RETURN_THROWS();
25802579
}
25812580

25822581
if ((pgsql = (pgLofp *)zend_fetch_resource(Z_RES_P(pgsql_id), "PostgreSQL large object", le_lofp)) == NULL) {
25832582
RETURN_THROWS();
25842583
}
25852584

2586-
if (ZEND_NUM_ARGS() > 1) {
2587-
buf_len = len < 0 ? 0 : len;
2585+
if (buffer_length < 0) {
2586+
zend_argument_value_error(2, "must be greater or equal than 0");
2587+
RETURN_THROWS();
25882588
}
25892589

2590-
buf = zend_string_alloc(buf_len, 0);
2590+
buf = zend_string_alloc(buffer_length, 0);
25912591
if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, ZSTR_VAL(buf), ZSTR_LEN(buf)))<0) {
25922592
zend_string_efree(buf);
25932593
RETURN_FALSE;
25942594
}
25952595

2596+
/* TODO Use truncate API? */
25962597
ZSTR_LEN(buf) = nbytes;
25972598
ZSTR_VAL(buf)[ZSTR_LEN(buf)] = '\0';
25982599
RETURN_NEW_STR(buf);

ext/pgsql/tests/05large_object.phpt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,32 @@ $oid = pg_lo_create ($db);
1515
if (!$oid) echo ("pg_lo_create() error\n");
1616
$handle = pg_lo_open ($db, $oid, "w");
1717
if (!$handle) echo ("pg_lo_open() error\n");
18-
pg_lo_write ($handle, "large object data\n");
18+
pg_lo_write ($handle, "large object data");
1919
pg_lo_close ($handle);
2020
pg_exec ($db, "commit");
2121

2222
echo "open/read/tell/seek/close LO\n";
2323
pg_exec ($db, "begin");
2424
$handle = pg_lo_open ($db, $oid, "w");
25-
pg_lo_read($handle, 100);
26-
pg_lo_tell($handle);
27-
pg_lo_seek($handle, 2);
25+
var_dump(pg_lo_read($handle, 5));
26+
var_dump(pg_lo_tell($handle));
27+
var_dump(pg_lo_seek($handle, 2, /* PGSQL_SEEK_CUR */)); // This is the default so move cursor from 5
28+
var_dump(pg_lo_read($handle, 100)); // Read to the end because chunk is larger then remaining content
29+
var_dump(pg_lo_tell($handle));
30+
var_dump(pg_lo_seek($handle, 0, PGSQL_SEEK_SET)); /* Reset cursor to beginning */
31+
var_dump(pg_lo_read($handle));
32+
var_dump(pg_lo_seek($handle, -4, PGSQL_SEEK_END)); /* Seek from the end */
33+
var_dump(pg_lo_read($handle));
2834
pg_lo_close($handle);
2935
pg_exec ($db, "commit");
3036

3137
echo "open/read_all/close LO\n";
3238
pg_exec ($db, "begin");
3339
$handle = pg_lo_open ($db, $oid, "w");
34-
pg_lo_read_all($handle);
40+
/* Will write to stdout */
41+
$bytesWritten = pg_lo_read_all($handle);
42+
echo "\n";
43+
var_dump($bytesWritten);
3544
if (pg_last_error()) echo "pg_lo_read_all() error\n".pg_last_error();
3645
pg_lo_close($handle);
3746
pg_exec ($db, "commit");
@@ -95,8 +104,18 @@ echo "OK";
95104
--EXPECT--
96105
create/write/close LO
97106
open/read/tell/seek/close LO
107+
string(5) "large"
108+
int(5)
109+
bool(true)
110+
string(10) "bject data"
111+
int(17)
112+
bool(true)
113+
string(17) "large object data"
114+
bool(true)
115+
string(4) "data"
98116
open/read_all/close LO
99117
large object data
118+
int(17)
100119
unlink LO
101120
Test without connection
102121
Test with string oid value

0 commit comments

Comments
 (0)