Skip to content

Commit bd7946f

Browse files
committed
Promote warnings to Errors in PostgreSQL extension
1 parent 71de8fe commit bd7946f

File tree

7 files changed

+164
-82
lines changed

7 files changed

+164
-82
lines changed

ext/opcache/Optimizer/zend_func_info.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ static const func_info_t func_infos[] = {
727727
F1("pg_result_error", MAY_BE_FALSE | MAY_BE_STRING),
728728
F1("pg_result_error_field", MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_STRING),
729729
F1("pg_get_result", MAY_BE_FALSE | MAY_BE_RESOURCE),
730-
F1("pg_result_status", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_STRING),
730+
F1("pg_result_status", MAY_BE_LONG | MAY_BE_STRING),
731731
F1("pg_get_notify", MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY),
732732
F1("pg_socket", MAY_BE_FALSE | MAY_BE_RESOURCE),
733733
F1("pg_meta_data", MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_STRING | MAY_BE_ARRAY_OF_ARRAY),

ext/pgsql/pgsql.c

+123-55
Original file line numberDiff line numberDiff line change
@@ -1468,8 +1468,8 @@ PHP_FUNCTION(pg_last_notice)
14681468
RETURN_TRUE;
14691469
break;
14701470
default:
1471-
php_error_docref(NULL, E_WARNING,
1472-
"Invalid option specified (" ZEND_LONG_FMT ")", option);
1471+
zend_argument_value_error(2, "must be one of PGSQL_NOTICE_LAST, PGSQL_NOTICE_ALL, or PGSQL_NOTICE_CLEAR");
1472+
RETURN_THROWS();
14731473
}
14741474
RETURN_FALSE;
14751475
}
@@ -1555,7 +1555,12 @@ PHP_FUNCTION(pg_field_table)
15551555
RETURN_THROWS();
15561556
}
15571557

1558-
if (fnum < 0 || fnum >= PQnfields(pg_result->result)) {
1558+
if (fnum < 0) {
1559+
zend_argument_value_error(2, "must be greater than or equal to 0");
1560+
RETURN_THROWS();
1561+
}
1562+
1563+
if (fnum >= PQnfields(pg_result->result)) {
15591564
php_error_docref(NULL, E_WARNING, "Bad field offset specified");
15601565
RETURN_FALSE;
15611566
}
@@ -1638,10 +1643,14 @@ static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_typ
16381643
RETURN_THROWS();
16391644
}
16401645

1646+
if (field < 0) {
1647+
zend_argument_value_error(2, "must be greater than or equal to 0");
1648+
RETURN_THROWS();
1649+
}
16411650

16421651
pgsql_result = pg_result->result;
16431652

1644-
if (field < 0 || field >= PQnfields(pgsql_result)) {
1653+
if (field >= PQnfields(pgsql_result)) {
16451654
php_error_docref(NULL, E_WARNING, "Bad field offset specified");
16461655
RETURN_FALSE;
16471656
}
@@ -1758,7 +1767,11 @@ PHP_FUNCTION(pg_fetch_result)
17581767
}
17591768
pg_result->row++;
17601769
} else {
1761-
if (row < 0 || row >= PQntuples(pgsql_result)) {
1770+
if (row < 0) {
1771+
zend_argument_value_error(2, "must be greater than or equal to 0");
1772+
RETURN_THROWS();
1773+
}
1774+
if (row >= PQntuples(pgsql_result)) {
17621775
php_error_docref(NULL, E_WARNING, "Unable to jump to row " ZEND_LONG_FMT " on PostgreSQL result index " ZEND_LONG_FMT,
17631776
row, Z_LVAL_P(result));
17641777
RETURN_FALSE;
@@ -1767,12 +1780,17 @@ PHP_FUNCTION(pg_fetch_result)
17671780
}
17681781
if (field_name) {
17691782
field_offset = PQfnumber(pgsql_result, ZSTR_VAL(field_name));
1783+
// TODO Split into 2 and ValueError for negative index?
17701784
if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {
17711785
php_error_docref(NULL, E_WARNING, "Bad column offset specified");
17721786
RETURN_FALSE;
17731787
}
17741788
} else {
1775-
if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {
1789+
if (field_offset < 0) {
1790+
zend_argument_value_error(argc, "must be greater than or equal to 0");
1791+
RETURN_THROWS();
1792+
}
1793+
if (field_offset >= PQnfields(pgsql_result)) {
17761794
php_error_docref(NULL, E_WARNING, "Bad column offset specified");
17771795
RETURN_FALSE;
17781796
}
@@ -1815,13 +1833,13 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
18151833
}
18161834

18171835
if (!row_is_null && row < 0) {
1818-
php_error_docref(NULL, E_WARNING, "The row parameter must be greater or equal to zero");
1819-
RETURN_FALSE;
1836+
zend_argument_value_error(2, "must be greater than or equal to 0");
1837+
RETURN_THROWS();
18201838
}
18211839

18221840
if (!(result_type & PGSQL_BOTH)) {
1823-
php_error_docref(NULL, E_WARNING, "Invalid result type");
1824-
RETURN_FALSE;
1841+
zend_argument_value_error(3, "must be one of PGSQL_ASSOC, PGSQL_NUM, or PGSQL_BOTH");
1842+
RETURN_THROWS();
18251843
}
18261844

18271845
if ((pg_result = (pgsql_result_handle *)zend_fetch_resource(Z_RES_P(result), "PostgreSQL result", le_result)) == NULL) {
@@ -1831,7 +1849,7 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
18311849
pgsql_result = pg_result->result;
18321850

18331851
if (!row_is_null) {
1834-
if (row < 0 || row >= PQntuples(pgsql_result)) {
1852+
if (row >= PQntuples(pgsql_result)) {
18351853
php_error_docref(NULL, E_WARNING, "Unable to jump to row " ZEND_LONG_FMT " on PostgreSQL result index " ZEND_LONG_FMT,
18361854
row, Z_LVAL_P(result));
18371855
RETURN_FALSE;
@@ -1977,8 +1995,8 @@ PHP_FUNCTION(pg_fetch_all)
19771995
}
19781996

19791997
if (!(result_type & PGSQL_BOTH)) {
1980-
php_error_docref(NULL, E_WARNING, "Invalid result type");
1981-
RETURN_FALSE;
1998+
zend_argument_value_error(4, "must be one of PGSQL_ASSOC, PGSQL_NUM, or PGSQL_BOTH");
1999+
RETURN_THROWS();
19822000
}
19832001

19842002
if ((pg_result = (pgsql_result_handle *)zend_fetch_resource(Z_RES_P(result), "PostgreSQL result", le_result)) == NULL) {
@@ -2012,10 +2030,15 @@ PHP_FUNCTION(pg_fetch_all_columns)
20122030
RETURN_THROWS();
20132031
}
20142032

2033+
if (colno < 0) {
2034+
zend_argument_value_error(2, "must be greater than or equal to 0");
2035+
RETURN_THROWS();
2036+
}
2037+
20152038
pgsql_result = pg_result->result;
20162039

20172040
num_fields = PQnfields(pgsql_result);
2018-
if (colno >= (zend_long)num_fields || colno < 0) {
2041+
if (colno >= (zend_long)num_fields) {
20192042
php_error_docref(NULL, E_WARNING, "Invalid column number '" ZEND_LONG_FMT "'", colno);
20202043
RETURN_FALSE;
20212044
}
@@ -2097,11 +2120,16 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
20972120
pg_result->row = 0;
20982121
}
20992122
pgsql_row = pg_result->row;
2123+
// TODO Split into 2 and ValueError for negative index?
21002124
if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
21012125
RETURN_FALSE;
21022126
}
21032127
} else {
2104-
if (row < 0 || row >= PQntuples(pgsql_result)) {
2128+
if (row < 0) {
2129+
zend_argument_value_error(2, "must be greater than or equal to 0");
2130+
RETURN_THROWS();
2131+
}
2132+
if (row >= PQntuples(pgsql_result)) {
21052133
php_error_docref(NULL, E_WARNING, "Unable to jump to row " ZEND_LONG_FMT " on PostgreSQL result index " ZEND_LONG_FMT,
21062134
row, Z_LVAL_P(result));
21072135
RETURN_FALSE;
@@ -2111,12 +2139,16 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
21112139

21122140
if (field_name) {
21132141
field_offset = PQfnumber(pgsql_result, ZSTR_VAL(field_name));
2142+
// TODO Split into 2 and ValueError for negative index?
21142143
if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {
21152144
php_error_docref(NULL, E_WARNING, "Bad column offset specified");
21162145
RETURN_FALSE;
21172146
}
21182147
} else {
2119-
if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {
2148+
if (field_offset < 0) {
2149+
zend_argument_value_error(argc, "must be greater than or equal to 0");
2150+
}
2151+
if (field_offset >= PQnfields(pgsql_result)) {
21202152
php_error_docref(NULL, E_WARNING, "Bad column offset specified");
21212153
RETURN_FALSE;
21222154
}
@@ -2603,14 +2635,15 @@ PHP_FUNCTION(pg_lo_write)
26032635
}
26042636

26052637
if (argc > 2) {
2638+
if (z_len < 0) {
2639+
zend_argument_value_error(3, "must be greater than or equal to 0");
2640+
RETURN_THROWS();
2641+
}
26062642
if (z_len > (zend_long)str_len) {
2643+
/* TODO Promote to ValueError? */
26072644
php_error_docref(NULL, E_WARNING, "Cannot write more than buffer size %zu. Tried to write " ZEND_LONG_FMT, str_len, z_len);
26082645
RETURN_FALSE;
26092646
}
2610-
if (z_len < 0) {
2611-
php_error_docref(NULL, E_WARNING, "Buffer size must be larger than 0, but " ZEND_LONG_FMT " was specified", z_len);
2612-
RETURN_FALSE;
2613-
}
26142647
len = z_len;
26152648
}
26162649
else {
@@ -2816,9 +2849,10 @@ PHP_FUNCTION(pg_lo_seek)
28162849
if (zend_parse_parameters(argc, "rl|l", &pgsql_id, &offset, &whence) == FAILURE) {
28172850
RETURN_THROWS();
28182851
}
2852+
/* TODO Error for < 0 offset? */
28192853
if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) {
2820-
php_error_docref(NULL, E_WARNING, "Invalid whence parameter");
2821-
return;
2854+
zend_argument_value_error(3, "must be one of PGSQL_SEEK_SET, PGSQL_SEEK_CUR, or PGSQL_SEEK_END");
2855+
RETURN_THROWS();
28222856
}
28232857

28242858
if ((pgsql = (pgLofp *)zend_fetch_resource(Z_RES_P(pgsql_id), "PostgreSQL large object", le_lofp)) == NULL) {
@@ -3352,6 +3386,7 @@ PHP_FUNCTION(pg_unescape_bytea)
33523386
to = estrndup(tmp, to_len);
33533387
PQfreemem(tmp);
33543388
if (!to) {
3389+
/* TODO Promote to Error? */
33553390
php_error_docref(NULL, E_WARNING,"Invalid parameter");
33563391
RETURN_FALSE;
33573392
}
@@ -4020,10 +4055,9 @@ PHP_FUNCTION(pg_result_status)
40204055
}
40214056
else if (result_type == PGSQL_STATUS_STRING) {
40224057
RETURN_STRING(PQcmdStatus(pgsql_result));
4023-
}
4024-
else {
4025-
php_error_docref(NULL, E_WARNING, "Optional 2nd parameter should be PGSQL_STATUS_LONG or PGSQL_STATUS_STRING");
4026-
RETURN_FALSE;
4058+
} else {
4059+
zend_argument_value_error(2, "must be either PGSQL_STATUS_LONG or PGSQL_STATUS_STRING");
4060+
RETURN_THROWS();
40274061
}
40284062
}
40294063
/* }}} */
@@ -4045,8 +4079,8 @@ PHP_FUNCTION(pg_get_notify)
40454079
}
40464080

40474081
if (!(result_type & PGSQL_BOTH)) {
4048-
php_error_docref(NULL, E_WARNING, "Invalid result type");
4049-
RETURN_FALSE;
4082+
zend_argument_value_error(2, "must be one of PGSQL_ASSOC, PGSQL_NUM, or PGSQL_BOTH");
4083+
RETURN_THROWS();
40504084
}
40514085

40524086
PQconsumeInput(pgsql);
@@ -4246,6 +4280,7 @@ PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, z
42464280
zval elem;
42474281

42484282
if (!*table_name) {
4283+
// CHeck if can be TODO
42494284
php_error_docref(NULL, E_WARNING, "The table name must be specified");
42504285
return FAILURE;
42514286
}
@@ -4364,6 +4399,12 @@ PHP_FUNCTION(pg_meta_data)
43644399
RETURN_THROWS();
43654400
}
43664401

4402+
/* php_pgsql_meta_data() warns on empty table_name */
4403+
if (table_name_len == 0) {
4404+
zend_argument_value_error(2, "cannot be empty");
4405+
RETURN_THROWS();
4406+
}
4407+
43674408
array_init(return_value);
43684409
if (php_pgsql_meta_data(pgsql, table_name, return_value, extended) == FAILURE) {
43694410
zend_array_destroy(Z_ARR_P(return_value)); /* destroy array */
@@ -4564,14 +4605,11 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
45644605
int err = 0, skip_field;
45654606
php_pgsql_data_type data_type;
45664607

4567-
assert(pg_link != NULL);
4568-
assert(Z_TYPE_P(values) == IS_ARRAY);
4569-
assert(Z_TYPE_P(result) == IS_ARRAY);
4570-
assert(!(opt & ~PGSQL_CONV_OPTS));
4571-
4572-
if (!table_name) {
4573-
return FAILURE;
4574-
}
4608+
ZEND_ASSERT(pg_link != NULL);
4609+
ZEND_ASSERT(Z_TYPE_P(values) == IS_ARRAY);
4610+
ZEND_ASSERT(Z_TYPE_P(result) == IS_ARRAY);
4611+
ZEND_ASSERT(!(opt & ~PGSQL_CONV_OPTS));
4612+
ZEND_ASSERT(table_name);
45754613

45764614
array_init(&meta);
45774615
/* table_name is escaped by php_pgsql_meta_data */
@@ -5222,13 +5260,16 @@ PHP_FUNCTION(pg_convert)
52225260
"rsa|l", &pgsql_link, &table_name, &table_name_len, &values, &option) == FAILURE) {
52235261
RETURN_THROWS();
52245262
}
5225-
if (option & ~PGSQL_CONV_OPTS) {
5226-
php_error_docref(NULL, E_WARNING, "Invalid option is specified");
5227-
RETURN_FALSE;
5263+
5264+
if (table_name_len == 0) {
5265+
zend_argument_value_error(2, "cannot be empty");
5266+
RETURN_THROWS();
52285267
}
5229-
if (!table_name_len) {
5230-
php_error_docref(NULL, E_NOTICE, "Table name is invalid");
5231-
RETURN_FALSE;
5268+
5269+
if (option & ~PGSQL_CONV_OPTS) {
5270+
zend_argument_value_error(4, "must be a valid bit mask of PGSQL_CONV_IGNORE_DEFAULT, "
5271+
"PGSQL_CONV_FORCE_NULL, or PGSQL_CONV_IGNORE_NOT_NULL");
5272+
RETURN_THROWS();
52325273
}
52335274

52345275
if ((pg_link = (PGconn *)zend_fetch_resource2(Z_RES_P(pgsql_link), "PostgreSQL link", le_link, le_plink)) == NULL) {
@@ -5433,9 +5474,16 @@ PHP_FUNCTION(pg_insert)
54335474
&pgsql_link, &table, &table_len, &values, &option) == FAILURE) {
54345475
RETURN_THROWS();
54355476
}
5477+
5478+
if (table_len == 0) {
5479+
zend_argument_value_error(2, "cannot be empty");
5480+
RETURN_THROWS();
5481+
}
5482+
54365483
if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
5437-
php_error_docref(NULL, E_WARNING, "Invalid option is specified");
5438-
RETURN_FALSE;
5484+
zend_argument_value_error(4, "must be a valid bit mask of PGSQL_CONV_FORCE_NULL, PGSQL_DML_NO_CONV, "
5485+
"PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC or PGSQL_DML_STRING");
5486+
RETURN_THROWS();
54395487
}
54405488

54415489
if ((pg_link = (PGconn *)zend_fetch_resource2(Z_RES_P(pgsql_link), "PostgreSQL link", le_link, le_plink)) == NULL) {
@@ -5643,9 +5691,16 @@ PHP_FUNCTION(pg_update)
56435691
&pgsql_link, &table, &table_len, &values, &ids, &option) == FAILURE) {
56445692
RETURN_THROWS();
56455693
}
5694+
5695+
if (table_len == 0) {
5696+
zend_argument_value_error(2, "cannot be empty");
5697+
RETURN_THROWS();
5698+
}
5699+
56465700
if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
5647-
php_error_docref(NULL, E_WARNING, "Invalid option is specified");
5648-
RETURN_FALSE;
5701+
zend_argument_value_error(5, "must be a valid bit mask of PGSQL_CONV_FORCE_NULL, PGSQL_DML_NO_CONV, "
5702+
"PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC or PGSQL_DML_STRING");
5703+
RETURN_THROWS();
56495704
}
56505705

56515706
if ((pg_link = (PGconn *)zend_fetch_resource2(Z_RES_P(pgsql_link), "PostgreSQL link", le_link, le_plink)) == NULL) {
@@ -5733,9 +5788,16 @@ PHP_FUNCTION(pg_delete)
57335788
&pgsql_link, &table, &table_len, &ids, &option) == FAILURE) {
57345789
RETURN_THROWS();
57355790
}
5791+
5792+
if (table_len == 0) {
5793+
zend_argument_value_error(2, "cannot be empty");
5794+
RETURN_THROWS();
5795+
}
5796+
57365797
if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
5737-
php_error_docref(NULL, E_WARNING, "Invalid option is specified");
5738-
RETURN_FALSE;
5798+
zend_argument_value_error(4, "must be a valid bit mask of PGSQL_CONV_FORCE_NULL, PGSQL_DML_NO_CONV, "
5799+
"PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC or PGSQL_DML_STRING");
5800+
RETURN_THROWS();
57395801
}
57405802

57415803
if ((pg_link = (PGconn *)zend_fetch_resource2(Z_RES_P(pgsql_link), "PostgreSQL link", le_link, le_plink)) == NULL) {
@@ -5865,20 +5927,26 @@ PHP_FUNCTION(pg_select)
58655927
long result_type = PGSQL_ASSOC;
58665928
PGconn *pg_link;
58675929
zend_string *sql = NULL;
5868-
int argc = ZEND_NUM_ARGS();
58695930

5870-
// TODO: result_type is unused by zpp!
5871-
if (zend_parse_parameters(argc, "rsa|l",
5931+
/* TODO Document result_type param on php.net (apparently it was added in PHP 7.1) */
5932+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsa|ll",
58725933
&pgsql_link, &table, &table_len, &ids, &option, &result_type) == FAILURE) {
58735934
RETURN_THROWS();
58745935
}
5936+
5937+
if (table_len == 0) {
5938+
zend_argument_value_error(2, "cannot be empty");
5939+
RETURN_THROWS();
5940+
}
5941+
58755942
if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
5876-
php_error_docref(NULL, E_WARNING, "Invalid option is specified");
5877-
RETURN_FALSE;
5943+
zend_argument_value_error(4, "must be a valid bit mask of PGSQL_CONV_FORCE_NULL, PGSQL_DML_NO_CONV, "
5944+
"PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC or PGSQL_DML_STRING");
5945+
RETURN_THROWS();
58785946
}
58795947
if (!(result_type & PGSQL_BOTH)) {
5880-
php_error_docref(NULL, E_WARNING, "Invalid result type");
5881-
RETURN_FALSE;
5948+
zend_argument_value_error(5, "must be one of PGSQL_ASSOC, PGSQL_NUM, or PGSQL_BOTH");
5949+
RETURN_THROWS();
58825950
}
58835951

58845952
if ((pg_link = (PGconn *)zend_fetch_resource2(Z_RES_P(pgsql_link), "PostgreSQL link", le_link, le_plink)) == NULL) {

0 commit comments

Comments
 (0)