Skip to content

Commit f82f602

Browse files
committed
ext/pgsql: few internal changes.
Z_PARAM_STR*/Z_PARAM_PATH. lo_write checks any null byte. close GH-17587
1 parent 63b6e90 commit f82f602

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ PHP NEWS
6363
. Added pg_close_stmt to close a prepared statement while allowing
6464
its name to be reused. (David Carlier)
6565
. Added Iterable support for pgsql_copy_from. (David Carlier)
66+
. pg_connect checks if connection_string contains any null byte,
67+
pg_close_stmt check if the statement contains any null byte.
68+
(David Carlier)
6669

6770
- POSIX:
6871
. Added POSIX_SC_OPEN_MAX constant to get the number of file descriptors

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ PHP 8.5 UPGRADE NOTES
125125

126126
- PGSQL:
127127
. pg_copy_from also supports inputs as Iterable.
128+
. pg_connect checks if the connection_string argument contains
129+
any null byte.
130+
. pg_close_stmt checks if the statement_name argument contains
131+
any null byte.
128132

129133
- POSIX:
130134
. posix_ttyname sets last_error to EBADF when encountering

ext/pgsql/pgsql.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
665665
PGresult *pg_result;
666666

667667
ZEND_PARSE_PARAMETERS_START(1, 2)
668-
Z_PARAM_STRING(connstring, connstring_len)
668+
Z_PARAM_PATH(connstring, connstring_len)
669669
Z_PARAM_OPTIONAL
670670
Z_PARAM_LONG(connect_type)
671671
ZEND_PARSE_PARAMETERS_END();
@@ -1123,7 +1123,7 @@ PHP_FUNCTION(pg_query)
11231123
zval *pgsql_link = NULL;
11241124
char *query;
11251125
size_t query_len;
1126-
int leftover = 0;
1126+
bool leftover = false;
11271127
pgsql_link_handle *link;
11281128
PGconn *pgsql;
11291129
PGresult *pgsql_result;
@@ -1157,7 +1157,7 @@ PHP_FUNCTION(pg_query)
11571157
}
11581158
while ((pgsql_result = PQgetResult(pgsql))) {
11591159
PQclear(pgsql_result);
1160-
leftover = 1;
1160+
leftover = true;
11611161
}
11621162
if (leftover) {
11631163
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
@@ -1220,7 +1220,7 @@ PHP_FUNCTION(pg_query_params)
12201220
zval *pv_param_arr, *tmp;
12211221
char *query;
12221222
size_t query_len;
1223-
int leftover = 0;
1223+
bool leftover = false;
12241224
int num_params = 0;
12251225
char **params = NULL;
12261226
pgsql_link_handle *link;
@@ -1259,7 +1259,7 @@ PHP_FUNCTION(pg_query_params)
12591259
}
12601260
while ((pgsql_result = PQgetResult(pgsql))) {
12611261
PQclear(pgsql_result);
1262-
leftover = 1;
1262+
leftover = true;
12631263
}
12641264
if (leftover) {
12651265
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
@@ -1335,7 +1335,7 @@ PHP_FUNCTION(pg_prepare)
13351335
zval *pgsql_link = NULL;
13361336
char *query, *stmtname;
13371337
size_t query_len, stmtname_len;
1338-
int leftover = 0;
1338+
bool leftover = false;
13391339
PGconn *pgsql;
13401340
pgsql_link_handle *link;
13411341
PGresult *pgsql_result;
@@ -1372,7 +1372,7 @@ PHP_FUNCTION(pg_prepare)
13721372
}
13731373
while ((pgsql_result = PQgetResult(pgsql))) {
13741374
PQclear(pgsql_result);
1375-
leftover = 1;
1375+
leftover = true;
13761376
}
13771377
if (leftover) {
13781378
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
@@ -1422,7 +1422,7 @@ PHP_FUNCTION(pg_execute)
14221422
zval *pv_param_arr, *tmp;
14231423
char *stmtname;
14241424
size_t stmtname_len;
1425-
int leftover = 0;
1425+
bool leftover = false;
14261426
int num_params = 0;
14271427
char **params = NULL;
14281428
PGconn *pgsql;
@@ -1461,7 +1461,7 @@ PHP_FUNCTION(pg_execute)
14611461
}
14621462
while ((pgsql_result = PQgetResult(pgsql))) {
14631463
PQclear(pgsql_result);
1464-
leftover = 1;
1464+
leftover = true;
14651465
}
14661466
if (leftover) {
14671467
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
@@ -1690,7 +1690,7 @@ PHP_FUNCTION(pg_field_table)
16901690
zval *result;
16911691
pgsql_result_handle *pg_result;
16921692
zend_long fnum = -1;
1693-
bool return_oid = 0;
1693+
bool return_oid = false;
16941694

16951695
ZEND_PARSE_PARAMETERS_START(2, 3)
16961696
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
@@ -2770,14 +2770,14 @@ PHP_FUNCTION(pg_lo_write)
27702770
zval *pgsql_id;
27712771
zend_string *str;
27722772
zend_long z_len;
2773-
bool z_len_is_null = 1;
2773+
bool z_len_is_null = true;
27742774
size_t nbytes;
27752775
size_t len;
27762776
pgLofp *pgsql;
27772777

27782778
ZEND_PARSE_PARAMETERS_START(2, 3)
27792779
Z_PARAM_OBJECT_OF_CLASS(pgsql_id, pgsql_lob_ce)
2780-
Z_PARAM_STR(str)
2780+
Z_PARAM_PATH_STR(str)
27812781
Z_PARAM_OPTIONAL
27822782
Z_PARAM_LONG_OR_NULL(z_len, z_len_is_null)
27832783
ZEND_PARSE_PARAMETERS_END();
@@ -3346,7 +3346,7 @@ PHP_FUNCTION(pg_copy_to)
33463346
switch (status) {
33473347
case PGRES_COPY_OUT:
33483348
if (pgsql_result) {
3349-
int copydone = 0;
3349+
bool copydone = false;
33503350

33513351
PQclear(pgsql_result);
33523352
array_init(return_value);
@@ -3355,7 +3355,7 @@ PHP_FUNCTION(pg_copy_to)
33553355
int ret = PQgetCopyData(pgsql, &csv, 0);
33563356
switch (ret) {
33573357
case -1:
3358-
copydone = 1;
3358+
copydone = true;
33593359
break;
33603360
case 0:
33613361
case -2:
@@ -4654,7 +4654,7 @@ PHP_FUNCTION(pg_meta_data)
46544654
zval *pgsql_link;
46554655
pgsql_link_handle *link;
46564656
zend_string *table_name;
4657-
bool extended=0;
4657+
bool extended = false;
46584658
PGconn *pgsql;
46594659

46604660
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -4832,7 +4832,7 @@ static zend_string *php_pgsql_add_quotes(zend_string *src)
48324832
/* if new_value is string "NULL" and field has default value, remove element to use default value */ \
48334833
if (!(opt & PGSQL_CONV_IGNORE_DEFAULT) && Z_TYPE_P(has_default) == IS_TRUE) { \
48344834
zval_ptr_dtor(&new_val); \
4835-
skip_field = 1; \
4835+
skip_field = true; \
48364836
} \
48374837
/* raise error if it's not null and cannot be ignored */ \
48384838
else if (!(opt & PGSQL_CONV_IGNORE_NOT_NULL) && Z_TYPE_P(not_null) == IS_TRUE) { \
@@ -4848,7 +4848,8 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
48484848
{
48494849
zend_string *field = NULL;
48504850
zval meta, *def, *type, *not_null, *has_default, *is_enum, *val, new_val;
4851-
int err = 0, skip_field;
4851+
int err = 0;
4852+
bool skip_field;
48524853
php_pgsql_data_type data_type;
48534854

48544855
ZEND_ASSERT(pg_link != NULL);
@@ -4867,7 +4868,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
48674868
}
48684869

48694870
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(values), field, val) {
4870-
skip_field = 0;
4871+
skip_field = false;
48714872
ZVAL_DEREF(val);
48724873
ZVAL_NULL(&new_val);
48734874

@@ -6324,11 +6325,11 @@ PHP_FUNCTION(pg_close_stmt)
63246325

63256326
ZEND_PARSE_PARAMETERS_START(2, 2)
63266327
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
6327-
Z_PARAM_STR(stmt)
6328+
Z_PARAM_PATH_STR(stmt)
63286329
ZEND_PARSE_PARAMETERS_END();
63296330

63306331
if (ZSTR_LEN(stmt) == 0) {
6331-
zend_argument_value_error(2, "cannot be empty");
6332+
zend_argument_must_not_be_empty_error(2);
63326333
RETURN_THROWS();
63336334
}
63346335

ext/pgsql/tests/05large_object.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ $oid = pg_lo_create ($db);
1717
if (!$oid) echo ("pg_lo_create() error\n");
1818
$handle = pg_lo_open ($db, $oid, "w");
1919
if (!$handle) echo ("pg_lo_open() error\n");
20+
try {
21+
pg_lo_write ($handle, "large\0object data");
22+
} catch (\ValueError $e) {
23+
echo $e->getMessage(), PHP_EOL;
24+
}
2025
pg_lo_write ($handle, "large object data");
2126
pg_lo_close ($handle);
2227
pg_exec ($db, "COMMIT");
@@ -105,6 +110,7 @@ echo "OK";
105110
?>
106111
--EXPECTF--
107112
create/write/close LO
113+
pg_lo_write(): Argument #2 ($data) must not contain any null bytes
108114
open/read/tell/seek/close LO
109115
string(5) "large"
110116
int(5)

ext/pgsql/tests/pg_close_stmt.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ $db = pg_connect($conn_str);
1818
$res = pg_prepare($db, 'test', $query);
1919

2020
$res = pg_execute($db, 'test', $params_null);
21+
try {
22+
pg_close_stmt($db, '');
23+
} catch (\ValueError $e) {
24+
echo $e->getMessage(), PHP_EOL;
25+
}
2126
$res = pg_close_stmt($db, 'test');
2227
var_dump($res !== false);
2328
var_dump(pg_result_status($res) === PGSQL_COMMAND_OK);
@@ -29,5 +34,6 @@ pg_close($db);
2934

3035
?>
3136
--EXPECT--
37+
pg_close_stmt(): Argument #2 ($statement_name) must not be empty
3238
bool(true)
3339
bool(true)

0 commit comments

Comments
 (0)