Skip to content

Commit e084700

Browse files
committed
ext/pgsql: few internal changes.
Z_PARAM_STR*/Z_PARAM_PATH. lo_write checks any null byte.
1 parent 75d7684 commit e084700

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

ext/pgsql/pgsql.c

Lines changed: 23 additions & 18 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,7 +2770,7 @@ 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;
@@ -2794,6 +2794,10 @@ PHP_FUNCTION(pg_lo_write)
27942794
len = z_len;
27952795
}
27962796
else {
2797+
if (zend_str_has_nul_byte(str)) {
2798+
zend_argument_value_error(2, "must not contain any null bytes");
2799+
RETURN_THROWS();
2800+
}
27972801
len = ZSTR_LEN(str);
27982802
}
27992803

@@ -3346,7 +3350,7 @@ PHP_FUNCTION(pg_copy_to)
33463350
switch (status) {
33473351
case PGRES_COPY_OUT:
33483352
if (pgsql_result) {
3349-
int copydone = 0;
3353+
bool copydone = false;
33503354

33513355
PQclear(pgsql_result);
33523356
array_init(return_value);
@@ -3355,7 +3359,7 @@ PHP_FUNCTION(pg_copy_to)
33553359
int ret = PQgetCopyData(pgsql, &csv, 0);
33563360
switch (ret) {
33573361
case -1:
3358-
copydone = 1;
3362+
copydone = true;
33593363
break;
33603364
case 0:
33613365
case -2:
@@ -4654,7 +4658,7 @@ PHP_FUNCTION(pg_meta_data)
46544658
zval *pgsql_link;
46554659
pgsql_link_handle *link;
46564660
zend_string *table_name;
4657-
bool extended=0;
4661+
bool extended = false;
46584662
PGconn *pgsql;
46594663

46604664
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -4832,7 +4836,7 @@ static zend_string *php_pgsql_add_quotes(zend_string *src)
48324836
/* if new_value is string "NULL" and field has default value, remove element to use default value */ \
48334837
if (!(opt & PGSQL_CONV_IGNORE_DEFAULT) && Z_TYPE_P(has_default) == IS_TRUE) { \
48344838
zval_ptr_dtor(&new_val); \
4835-
skip_field = 1; \
4839+
skip_field = true; \
48364840
} \
48374841
/* raise error if it's not null and cannot be ignored */ \
48384842
else if (!(opt & PGSQL_CONV_IGNORE_NOT_NULL) && Z_TYPE_P(not_null) == IS_TRUE) { \
@@ -4848,7 +4852,8 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
48484852
{
48494853
zend_string *field = NULL;
48504854
zval meta, *def, *type, *not_null, *has_default, *is_enum, *val, new_val;
4851-
int err = 0, skip_field;
4855+
int err = 0;
4856+
bool skip_field;
48524857
php_pgsql_data_type data_type;
48534858

48544859
ZEND_ASSERT(pg_link != NULL);
@@ -4867,7 +4872,7 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *
48674872
}
48684873

48694874
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(values), field, val) {
4870-
skip_field = 0;
4875+
skip_field = false;
48714876
ZVAL_DEREF(val);
48724877
ZVAL_NULL(&new_val);
48734878

@@ -6324,7 +6329,7 @@ PHP_FUNCTION(pg_close_stmt)
63246329

63256330
ZEND_PARSE_PARAMETERS_START(2, 2)
63266331
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
6327-
Z_PARAM_STR(stmt)
6332+
Z_PARAM_PATH(stmt)
63286333
ZEND_PARSE_PARAMETERS_END();
63296334

63306335
if (ZSTR_LEN(stmt) == 0) {

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)

0 commit comments

Comments
 (0)