Skip to content

Commit 6cc0d7c

Browse files
committed
Boolify pdo_dbh_attribute_set()
1 parent 3e6940f commit 6cc0d7c

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

ext/pdo/pdo_dbh.c

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "zend_interfaces.h"
3535
#include "pdo_dbh_arginfo.h"
3636

37-
static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value);
37+
static bool pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value);
3838

3939
void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error)
4040
{
@@ -375,6 +375,8 @@ PHP_METHOD(PDO, __construct)
375375
php_error_docref(NULL, E_ERROR, "Out of memory");
376376
}
377377

378+
/* pdo_dbh_attribute_set() can emit a Warning if the ERR_MODE is set to warning
379+
* As we are in a constructor we override the behaviour by replacing the error handler */
378380
zend_replace_error_handling(EH_THROW, pdo_exception_ce, &zeh);
379381

380382
if (!call_factory) {
@@ -407,7 +409,8 @@ PHP_METHOD(PDO, __construct)
407409
continue;
408410
}
409411
ZVAL_DEREF(attr_value);
410-
/* TODO: Check that this doesn't fail? */
412+
413+
/* TODO: Should the constructor fail when the attribute cannot be set? */
411414
pdo_dbh_attribute_set(dbh, long_key, attr_value);
412415
} ZEND_HASH_FOREACH_END();
413416
}
@@ -675,17 +678,18 @@ PHP_METHOD(PDO, inTransaction)
675678
}
676679
/* }}} */
677680

678-
static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /* {{{ */
679-
{
680-
zend_long lval;
681-
682681
/* TODO: Make distinction between numeric and non-numeric strings */
683682
#define PDO_LONG_PARAM_CHECK \
684683
if (Z_TYPE_P(value) != IS_LONG && Z_TYPE_P(value) != IS_STRING && Z_TYPE_P(value) != IS_FALSE && Z_TYPE_P(value) != IS_TRUE) { \
685684
zend_type_error("Attribute value must be of type int for selected attribute, %s given", zend_zval_type_name(value)); \
686-
return FAILURE; \
685+
return false; \
687686
} \
688687

688+
/* Return false on failure, true otherwise */
689+
static bool pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /* {{{ */
690+
{
691+
zend_long lval;
692+
689693
switch (attr) {
690694
case PDO_ATTR_ERRMODE:
691695
PDO_LONG_PARAM_CHECK;
@@ -695,12 +699,12 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
695699
case PDO_ERRMODE_WARNING:
696700
case PDO_ERRMODE_EXCEPTION:
697701
dbh->error_mode = lval;
698-
return SUCCESS;
702+
return true;
699703
default:
700704
zend_value_error("Error mode must be one of the PDO::ERRMODE_* constants");
701-
return FAILURE;
705+
return false;
702706
}
703-
return FAILURE;
707+
return false;
704708

705709
case PDO_ATTR_CASE:
706710
PDO_LONG_PARAM_CHECK;
@@ -710,25 +714,25 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
710714
case PDO_CASE_UPPER:
711715
case PDO_CASE_LOWER:
712716
dbh->desired_case = lval;
713-
return SUCCESS;
717+
return true;
714718
default:
715719
zend_value_error("Case folding mode must be one of the PDO::CASE_* constants");
716-
return FAILURE;
720+
return false;
717721
}
718-
return FAILURE;
722+
return false;
719723

720724
case PDO_ATTR_ORACLE_NULLS:
721725
PDO_LONG_PARAM_CHECK;
722726
dbh->oracle_nulls = zval_get_long(value);
723-
return SUCCESS;
727+
return true;
724728

725729
case PDO_ATTR_DEFAULT_FETCH_MODE:
726730
if (Z_TYPE_P(value) == IS_ARRAY) {
727731
zval *tmp;
728732
if ((tmp = zend_hash_index_find(Z_ARRVAL_P(value), 0)) != NULL && Z_TYPE_P(tmp) == IS_LONG) {
729733
if (Z_LVAL_P(tmp) == PDO_FETCH_INTO || Z_LVAL_P(tmp) == PDO_FETCH_CLASS) {
730734
zend_value_error("PDO::FETCH_INTO and PDO::FETCH_CLASS cannot be set as the default fetch mode");
731-
return FAILURE;
735+
return false;
732736
}
733737
}
734738
} else {
@@ -737,15 +741,15 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
737741
lval = zval_get_long(value);
738742
if (lval == PDO_FETCH_USE_DEFAULT) {
739743
zend_value_error("Fetch mode must be a bitmask of PDO::FETCH_* constants");
740-
return FAILURE;
744+
return false;
741745
}
742746
dbh->default_fetch_type = lval;
743-
return SUCCESS;
747+
return true;
744748

745749
case PDO_ATTR_STRINGIFY_FETCHES:
746750
PDO_LONG_PARAM_CHECK;
747751
dbh->stringify = zval_get_long(value) ? 1 : 0;
748-
return SUCCESS;
752+
return true;
749753

750754
case PDO_ATTR_STATEMENT_CLASS: {
751755
/* array(string classname, array(mixed ctor_args)) */
@@ -758,29 +762,29 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
758762
"PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances"
759763
);
760764
PDO_HANDLE_DBH_ERR();
761-
return FAILURE;
765+
return false;
762766
}
763767
if (Z_TYPE_P(value) != IS_ARRAY) {
764768
zend_type_error("PDO::ATTR_STATEMENT_CLASS value must be of type array, %s given",
765769
zend_zval_type_name(value));
766-
return FAILURE;
770+
return false;
767771
}
768772
if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 0)) == NULL) {
769773
zend_value_error("PDO::ATTR_STATEMENT_CLASS value must be an array with the format "
770774
"array(classname, constructor_args)");
771-
return FAILURE;
775+
return false;
772776
}
773777
if (Z_TYPE_P(item) != IS_STRING || (pce = zend_lookup_class(Z_STR_P(item))) == NULL) {
774778
zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be a valid class");
775-
return FAILURE;
779+
return false;
776780
}
777781
if (!instanceof_function(pce, pdo_dbstmt_ce)) {
778782
zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be derived from PDOStatement");
779-
return FAILURE;
783+
return false;
780784
}
781785
if (pce->constructor && !(pce->constructor->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED))) {
782786
zend_type_error("User-supplied statement class cannot have a public constructor");
783-
return FAILURE;
787+
return false;
784788
}
785789
dbh->def_stmt_ce = pce;
786790
if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
@@ -791,11 +795,11 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
791795
if (Z_TYPE_P(item) != IS_ARRAY) {
792796
zend_type_error("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given",
793797
zend_zval_type_name(value));
794-
return FAILURE;
798+
return false;
795799
}
796800
ZVAL_COPY(&dbh->def_stmt_ctor_args, item);
797801
}
798-
return SUCCESS;
802+
return true;
799803
}
800804

801805
default:
@@ -808,7 +812,7 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
808812

809813
PDO_DBH_CLEAR_ERR();
810814
if (dbh->methods->set_attribute(dbh, attr, value)) {
811-
return SUCCESS;
815+
return true;
812816
}
813817

814818
fail:
@@ -817,7 +821,7 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
817821
} else {
818822
PDO_HANDLE_DBH_ERR();
819823
}
820-
return FAILURE;
824+
return false;
821825
}
822826
/* }}} */
823827

@@ -836,10 +840,7 @@ PHP_METHOD(PDO, setAttribute)
836840
PDO_DBH_CLEAR_ERR();
837841
PDO_CONSTRUCT_CHECK;
838842

839-
if (pdo_dbh_attribute_set(dbh, attr, value) != FAILURE) {
840-
RETURN_TRUE;
841-
}
842-
RETURN_FALSE;
843+
RETURN_BOOL(pdo_dbh_attribute_set(dbh, attr, value));
843844
}
844845
/* }}} */
845846

0 commit comments

Comments
 (0)