Skip to content

Commit f87c9a5

Browse files
committed
Promote E_NOTICE to Error in PostgreSQL extension
1 parent 189751c commit f87c9a5

File tree

1 file changed

+57
-43
lines changed

1 file changed

+57
-43
lines changed

ext/pgsql/pgsql.c

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,7 @@ PHP_FUNCTION(pg_lo_create)
23002300
RETURN_THROWS();
23012301
}
23022302

2303+
/* Overloaded method use default link if arg 1 is not a ressource, set oid pointer */
23032304
if ((argc == 1) && (Z_TYPE_P(pgsql_link) != IS_RESOURCE)) {
23042305
oid = pgsql_link;
23052306
pgsql_link = NULL;
@@ -2322,25 +2323,26 @@ PHP_FUNCTION(pg_lo_create)
23222323
switch (Z_TYPE_P(oid)) {
23232324
case IS_STRING:
23242325
{
2326+
/* TODO: Use zend_is_numeric_string/subroutine? */
23252327
char *end_ptr;
23262328
wanted_oid = (Oid)strtoul(Z_STRVAL_P(oid), &end_ptr, 10);
23272329
if ((Z_STRVAL_P(oid)+Z_STRLEN_P(oid)) != end_ptr) {
23282330
/* wrong integer format */
2329-
php_error_docref(NULL, E_NOTICE, "Invalid OID value passed");
2330-
RETURN_FALSE;
2331+
zend_value_error("Invalid OID value passed");
2332+
RETURN_THROWS();
23312333
}
23322334
}
23332335
break;
23342336
case IS_LONG:
23352337
if (Z_LVAL_P(oid) < (zend_long)InvalidOid) {
2336-
php_error_docref(NULL, E_NOTICE, "Invalid OID value passed");
2337-
RETURN_FALSE;
2338+
zend_value_error("Invalid OID value passed");
2339+
RETURN_THROWS();
23382340
}
23392341
wanted_oid = (Oid)Z_LVAL_P(oid);
23402342
break;
23412343
default:
2342-
php_error_docref(NULL, E_NOTICE, "Invalid OID value passed");
2343-
RETURN_FALSE;
2344+
zend_type_error("OID value must be of type string|int, %s given", zend_zval_type_name(oid));
2345+
RETURN_THROWS();
23442346
}
23452347
if ((pgsql_oid = lo_create(pgsql, wanted_oid)) == InvalidOid) {
23462348
php_error_docref(NULL, E_WARNING, "Unable to create PostgreSQL large object");
@@ -2374,39 +2376,41 @@ PHP_FUNCTION(pg_lo_unlink)
23742376
/* accept string type since Oid type is unsigned int */
23752377
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
23762378
"rs", &pgsql_link, &oid_string, &oid_strlen) == SUCCESS) {
2379+
/* TODO: Use zend_is_numeric_string/subroutine? */
23772380
oid = (Oid)strtoul(oid_string, &end_ptr, 10);
23782381
if ((oid_string+oid_strlen) != end_ptr) {
23792382
/* wrong integer format */
2380-
php_error_docref(NULL, E_NOTICE, "Wrong OID value passed");
2381-
RETURN_FALSE;
2383+
zend_value_error("Invalid OID value passed");
2384+
RETURN_THROWS();
23822385
}
23832386
link = Z_RES_P(pgsql_link);
23842387
}
23852388
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
23862389
"rl", &pgsql_link, &oid_long) == SUCCESS) {
23872390
if (oid_long <= (zend_long)InvalidOid) {
2388-
php_error_docref(NULL, E_NOTICE, "Invalid OID specified");
2389-
RETURN_FALSE;
2391+
zend_value_error("Invalid OID value passed");
2392+
RETURN_THROWS();
23902393
}
23912394
oid = (Oid)oid_long;
23922395
link = Z_RES_P(pgsql_link);
23932396
}
23942397
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
23952398
"s", &oid_string, &oid_strlen) == SUCCESS) {
2399+
/* TODO: Use zend_is_numeric_string/subroutine? */
23962400
oid = (Oid)strtoul(oid_string, &end_ptr, 10);
23972401
if ((oid_string+oid_strlen) != end_ptr) {
23982402
/* wrong integer format */
2399-
php_error_docref(NULL, E_NOTICE, "Wrong OID value passed");
2400-
RETURN_FALSE;
2403+
zend_value_error("Invalid OID value passed");
2404+
RETURN_THROWS();
24012405
}
24022406
link = FETCH_DEFAULT_LINK();
24032407
CHECK_DEFAULT_LINK(link);
24042408
}
24052409
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
24062410
"l", &oid_long) == SUCCESS) {
24072411
if (oid_long <= (zend_long)InvalidOid) {
2408-
php_error_docref(NULL, E_NOTICE, "Invalid OID is specified");
2409-
RETURN_FALSE;
2412+
zend_value_error("Invalid OID value passed");
2413+
RETURN_THROWS();
24102414
}
24112415
oid = (Oid)oid_long;
24122416
link = FETCH_DEFAULT_LINK();
@@ -2447,39 +2451,41 @@ PHP_FUNCTION(pg_lo_open)
24472451
/* accept string type since Oid is unsigned int */
24482452
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
24492453
"rss", &pgsql_link, &oid_string, &oid_strlen, &mode_string, &mode_strlen) == SUCCESS) {
2454+
/* TODO: Use zend_is_numeric_string/subroutine? */
24502455
oid = (Oid)strtoul(oid_string, &end_ptr, 10);
24512456
if ((oid_string+oid_strlen) != end_ptr) {
24522457
/* wrong integer format */
2453-
php_error_docref(NULL, E_NOTICE, "Wrong OID value passed");
2454-
RETURN_FALSE;
2458+
zend_value_error("Invalid OID value passed");
2459+
RETURN_THROWS();
24552460
}
24562461
link = Z_RES_P(pgsql_link);
24572462
}
24582463
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
24592464
"rls", &pgsql_link, &oid_long, &mode_string, &mode_strlen) == SUCCESS) {
24602465
if (oid_long <= (zend_long)InvalidOid) {
2461-
php_error_docref(NULL, E_NOTICE, "Invalid OID specified");
2462-
RETURN_FALSE;
2466+
zend_value_error("Invalid OID value passed");
2467+
RETURN_THROWS();
24632468
}
24642469
oid = (Oid)oid_long;
24652470
link = Z_RES_P(pgsql_link);
24662471
}
24672472
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
24682473
"ss", &oid_string, &oid_strlen, &mode_string, &mode_strlen) == SUCCESS) {
2474+
/* TODO: Use zend_is_numeric_string/subroutine? */
24692475
oid = (Oid)strtoul(oid_string, &end_ptr, 10);
24702476
if ((oid_string+oid_strlen) != end_ptr) {
24712477
/* wrong integer format */
2472-
php_error_docref(NULL, E_NOTICE, "Wrong OID value passed");
2473-
RETURN_FALSE;
2478+
zend_value_error("Invalid OID value passed");
2479+
RETURN_THROWS();
24742480
}
24752481
link = FETCH_DEFAULT_LINK();
24762482
CHECK_DEFAULT_LINK(link);
24772483
}
24782484
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
24792485
"ls", &oid_long, &mode_string, &mode_strlen) == SUCCESS) {
24802486
if (oid_long <= (zend_long)InvalidOid) {
2481-
php_error_docref(NULL, E_NOTICE, "Invalid OID specified");
2482-
RETURN_FALSE;
2487+
zend_value_error("Invalid OID value passed");
2488+
RETURN_THROWS();
24832489
}
24842490
oid = (Oid)oid_long;
24852491
link = FETCH_DEFAULT_LINK();
@@ -2525,6 +2531,7 @@ PHP_FUNCTION(pg_lo_open)
25252531
if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
25262532
if (lo_unlink(pgsql, oid) == -1) {
25272533
efree(pgsql_lofp);
2534+
/* TODO: Throw error? */
25282535
php_error_docref(NULL, E_WARNING, "Something is really messed up! Your database is badly corrupted in a way NOT related to PHP");
25292536
RETURN_FALSE;
25302537
}
@@ -2717,25 +2724,26 @@ PHP_FUNCTION(pg_lo_import)
27172724
switch (Z_TYPE_P(oid)) {
27182725
case IS_STRING:
27192726
{
2727+
/* TODO: Use zend_is_numeric_string/subroutine? */
27202728
char *end_ptr;
27212729
wanted_oid = (Oid)strtoul(Z_STRVAL_P(oid), &end_ptr, 10);
27222730
if ((Z_STRVAL_P(oid)+Z_STRLEN_P(oid)) != end_ptr) {
27232731
/* wrong integer format */
2724-
php_error_docref(NULL, E_NOTICE, "Invalid OID value passed");
2725-
RETURN_FALSE;
2732+
zend_value_error("Invalid OID value passed");
2733+
RETURN_THROWS();
27262734
}
27272735
}
27282736
break;
27292737
case IS_LONG:
27302738
if (Z_LVAL_P(oid) < (zend_long)InvalidOid) {
2731-
php_error_docref(NULL, E_NOTICE, "Invalid OID value passed");
2732-
RETURN_FALSE;
2739+
zend_value_error("Invalid OID value passed");
2740+
RETURN_THROWS();
27332741
}
27342742
wanted_oid = (Oid)Z_LVAL_P(oid);
27352743
break;
27362744
default:
2737-
php_error_docref(NULL, E_NOTICE, "Invalid OID value passed");
2738-
RETURN_FALSE;
2745+
zend_type_error("OID value must be of type string|int, %s given", zend_zval_type_name(oid));
2746+
RETURN_THROWS();
27392747
}
27402748

27412749
returned_oid = lo_import_with_oid(pgsql, file_in, wanted_oid);
@@ -2773,39 +2781,41 @@ PHP_FUNCTION(pg_lo_export)
27732781
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
27742782
"rlp", &pgsql_link, &oid_long, &file_out, &name_len) == SUCCESS) {
27752783
if (oid_long <= (zend_long)InvalidOid) {
2776-
php_error_docref(NULL, E_NOTICE, "Invalid OID specified");
2777-
RETURN_FALSE;
2784+
zend_value_error("Invalid OID value passed");
2785+
RETURN_THROWS();
27782786
}
27792787
oid = (Oid)oid_long;
27802788
link = Z_RES_P(pgsql_link);
27812789
}
27822790
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
27832791
"rsp", &pgsql_link, &oid_string, &oid_strlen, &file_out, &name_len) == SUCCESS) {
2792+
/* TODO: Use zend_is_numeric_string/subroutine? */
27842793
oid = (Oid)strtoul(oid_string, &end_ptr, 10);
27852794
if ((oid_string+oid_strlen) != end_ptr) {
27862795
/* wrong integer format */
2787-
php_error_docref(NULL, E_NOTICE, "Wrong OID value passed");
2788-
RETURN_FALSE;
2796+
zend_value_error("Invalid OID value passed");
2797+
RETURN_THROWS();
27892798
}
27902799
link = Z_RES_P(pgsql_link);
27912800
}
27922801
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
27932802
"lp", &oid_long, &file_out, &name_len) == SUCCESS) {
27942803
if (oid_long <= (zend_long)InvalidOid) {
2795-
php_error_docref(NULL, E_NOTICE, "Invalid OID specified");
2796-
RETURN_FALSE;
2804+
zend_value_error("Invalid OID value passed");
2805+
RETURN_THROWS();
27972806
}
27982807
oid = (Oid)oid_long;
27992808
link = FETCH_DEFAULT_LINK();
28002809
CHECK_DEFAULT_LINK(link);
28012810
}
28022811
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc,
28032812
"sp", &oid_string, &oid_strlen, &file_out, &name_len) == SUCCESS) {
2813+
/* TODO: Use zend_is_numeric_string/subroutine? */
28042814
oid = (Oid)strtoul(oid_string, &end_ptr, 10);
28052815
if ((oid_string+oid_strlen) != end_ptr) {
28062816
/* wrong integer format */
2807-
php_error_docref(NULL, E_NOTICE, "Wrong OID value passed");
2808-
RETURN_FALSE;
2817+
zend_value_error("Invalid OID value passed");
2818+
RETURN_THROWS();
28092819
}
28102820
link = FETCH_DEFAULT_LINK();
28112821
CHECK_DEFAULT_LINK(link);
@@ -4264,6 +4274,7 @@ PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, z
42644274
src = estrdup(table_name);
42654275
tmp_name = php_strtok_r(src, ".", &tmp_name2);
42664276
if (!tmp_name) {
4277+
// TODO ValueError (empty table name)?
42674278
efree(src);
42684279
php_error_docref(NULL, E_WARNING, "The table name must be specified");
42694280
return FAILURE;
@@ -4557,6 +4568,7 @@ static int php_pgsql_add_quotes(zval *src, zend_bool should_free)
45574568
}
45584569
/* }}} */
45594570

4571+
/* Raise E_NOTICE to E_WARNING or Error? */
45604572
#define PGSQL_CONV_CHECK_IGNORE() \
45614573
if (!err && Z_TYPE(new_val) == IS_STRING && !strcmp(Z_STRVAL(new_val), "NULL")) { \
45624574
/* if new_value is string "NULL" and field has default value, remove element to use default value */ \
@@ -4600,8 +4612,10 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
46004612
skip_field = 0;
46014613
ZVAL_NULL(&new_val);
46024614

4615+
/* TODO: Check when meta data can be broken and see if can use assertions instead */
4616+
46034617
if (!err && field == NULL) {
4604-
php_error_docref(NULL, E_WARNING, "Accepts only string key for values");
4618+
zend_value_error("Array of values must be an associated array with string keys");
46054619
err = 1;
46064620
}
46074621

@@ -4626,7 +4640,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
46264640
err = 1;
46274641
}
46284642
if (!err && (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT)) {
4629-
php_error_docref(NULL, E_NOTICE, "Expects scalar values as field values");
4643+
zend_type_error("Values must be of type string|int|float|bool|null, %s given", zend_zval_type_name(val));
46304644
err = 1;
46314645
}
46324646
if (err) {
@@ -4641,6 +4655,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
46414655
data_type = php_pgsql_get_data_type(Z_STRVAL_P(type), Z_STRLEN_P(type));
46424656
}
46434657

4658+
/* TODO: Should E_NOTICE be converted to type error if PHP type cannot be converted to field type? */
46444659
switch(data_type)
46454660
{
46464661
case PG_BOOL:
@@ -5358,7 +5373,7 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var
53585373

53595374
ZEND_HASH_FOREACH_STR_KEY(Z_ARRVAL_P(var_array), fld) {
53605375
if (fld == NULL) {
5361-
php_error_docref(NULL, E_NOTICE, "Expects associative array for values to be inserted");
5376+
zend_value_error("Array of values must be an associated array of string keys");
53625377
goto cleanup;
53635378
}
53645379
if (opt & PGSQL_DML_ESCAPE) {
@@ -5401,9 +5416,8 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var
54015416
smart_str_appendl(&querystr, "NULL", sizeof("NULL")-1);
54025417
break;
54035418
default:
5404-
php_error_docref(NULL, E_WARNING, "Expects scaler values. type = %d", Z_TYPE_P(val));
5419+
zend_type_error("Value must be of type string|int|float|null, %s given", zend_zval_type_name(val));
54055420
goto cleanup;
5406-
break;
54075421
}
54085422
smart_str_appendc(&querystr, ',');
54095423
} ZEND_HASH_FOREACH_END();
@@ -5532,7 +5546,7 @@ static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr,
55325546

55335547
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, fld, val) {
55345548
if (fld == NULL) {
5535-
php_error_docref(NULL, E_NOTICE, "Expects associative array for values to be inserted");
5549+
zend_value_error("Array of values must be an associated array of string keys");
55365550
return -1;
55375551
}
55385552
if (opt & PGSQL_DML_ESCAPE) {
@@ -5573,7 +5587,7 @@ static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr,
55735587
smart_str_appendl(querystr, "NULL", sizeof("NULL")-1);
55745588
break;
55755589
default:
5576-
php_error_docref(NULL, E_WARNING, "Expects scaler values. type=%d", Z_TYPE_P(val));
5590+
zend_type_error("Value must be of type string|int|float|null, %s given", zend_zval_type_name(val));
55775591
return -1;
55785592
}
55795593
smart_str_appendl(querystr, pad, pad_len);

0 commit comments

Comments
 (0)