Skip to content

Commit d18ca2f

Browse files
committed
Preliminary Type/ValueError for setting PDO attributes
1 parent bbd3548 commit d18ca2f

File tree

2 files changed

+29
-36
lines changed

2 files changed

+29
-36
lines changed

ext/pdo/pdo_dbh.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,7 @@ static int pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /*
689689
/* TODO Always TypeError (also restrict to actual integer?) */
690690
#define PDO_LONG_PARAM_CHECK \
691691
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) { \
692-
pdo_raise_impl_error(dbh, NULL, "HY000", "attribute value must be an integer"); \
693-
PDO_HANDLE_DBH_ERR(); \
692+
zend_type_error("Attribute value must be int for selected attribute, %s given", zend_zval_type_name(value)); \
694693
return FAILURE; \
695694
} \
696695

@@ -705,9 +704,7 @@ static int pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /*
705704
dbh->error_mode = lval;
706705
return SUCCESS;
707706
default:
708-
/* TODO Always ValueError */
709-
pdo_raise_impl_error(dbh, NULL, "HY000", "invalid error mode");
710-
PDO_HANDLE_DBH_ERR();
707+
zend_value_error("Error mode must be one of PDO::ERRMODE_* constants");
711708
return FAILURE;
712709
}
713710
return FAILURE;
@@ -722,9 +719,7 @@ static int pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /*
722719
dbh->desired_case = lval;
723720
return SUCCESS;
724721
default:
725-
/* TODO Always ValueError */
726-
pdo_raise_impl_error(dbh, NULL, "HY000", "invalid case folding mode");
727-
PDO_HANDLE_DBH_ERR();
722+
zend_value_error("Case folding mode must be one of PDO::CASE_* constants");
728723
return FAILURE;
729724
}
730725
return FAILURE;
@@ -739,8 +734,7 @@ static int pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /*
739734
zval *tmp;
740735
if ((tmp = zend_hash_index_find(Z_ARRVAL_P(value), 0)) != NULL && Z_TYPE_P(tmp) == IS_LONG) {
741736
if (Z_LVAL_P(tmp) == PDO_FETCH_INTO || Z_LVAL_P(tmp) == PDO_FETCH_CLASS) {
742-
/* TODO Always ValueError */
743-
pdo_raise_impl_error(dbh, NULL, "HY000", "FETCH_INTO and FETCH_CLASS are not yet supported as default fetch modes");
737+
zend_value_error("PDO::FETCH_INTO and PDO::FETCH_CLASS cannot be set as default fetch mode");
744738
return FAILURE;
745739
}
746740
}
@@ -749,8 +743,7 @@ static int pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /*
749743
}
750744
lval = zval_get_long(value);
751745
if (lval == PDO_FETCH_USE_DEFAULT) {
752-
/* TODO Always ValueError */
753-
pdo_raise_impl_error(dbh, NULL, "HY000", "invalid fetch mode type");
746+
zend_value_error("Fetch mode must be a bitmask of PDO::FETCH_* constants");
754747
return FAILURE;
755748
}
756749
dbh->default_fetch_type = lval;

ext/pdo_mysql/tests/pdo_mysql_attr_errmode.phpt

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,27 @@ error_reporting=E_ALL
1414
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
1515
$db = MySQLPDOTest::factory();
1616

17-
$valid = array(PDO::ERRMODE_SILENT, PDO::ERRMODE_WARNING, PDO::ERRMODE_EXCEPTION);
18-
do {
19-
$invalid = mt_rand(-1000, 1000);
20-
} while (in_array($invalid, $valid));
21-
22-
23-
$tmp = array();
24-
if (false != @$db->setAttribute(PDO::ATTR_ERRMODE, $tmp))
25-
printf("[001] Maybe PDO could indicate that this is not a proper way of setting the ERRMODE...\n");
26-
27-
$tmp = new stdClass();
28-
$ret = @$db->setAttribute(PDO::ATTR_ERRMODE, $tmp);
29-
if (false != $ret)
30-
printf("[002] Maybe PDO could indicate that this is not a proper way of setting the ERRMODE...%s\n",
31-
var_export($ret, true));
32-
33-
$ret = @$db->setAttribute(PDO::ATTR_ERRMODE, 'pdo');
34-
if (false != $ret)
35-
printf("[003] Maybe PDO could indicate that this is not a proper way of setting the ERRMODE...%s\n",
36-
var_export($ret, true));
37-
38-
if (false != @$db->setAttribute(PDO::ATTR_ERRMODE, $invalid))
39-
printf("[004] Invalid ERRMODE should be rejected\n");
17+
try {
18+
$db->setAttribute(PDO::ATTR_ERRMODE, []);
19+
} catch (\Error $e) {
20+
echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
21+
}
22+
try {
23+
$db->setAttribute(PDO::ATTR_ERRMODE, new stdClass());
24+
} catch (\Error $e) {
25+
echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
26+
}
27+
try {
28+
/* This currently passes */
29+
$db->setAttribute(PDO::ATTR_ERRMODE, 'pdo');
30+
} catch (\Error $e) {
31+
echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
32+
}
33+
try {
34+
$db->setAttribute(PDO::ATTR_ERRMODE, 1000);
35+
} catch (\Error $e) {
36+
echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
37+
}
4038

4139
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
4240
// no message for any PDO call but...
@@ -160,7 +158,9 @@ error_reporting=E_ALL
160158
print "done!\n";
161159
?>
162160
--EXPECTF--
163-
[003] Maybe PDO could indicate that this is not a proper way of setting the ERRMODE...true
161+
TypeError: Attribute value must be int for selected attribute, array given
162+
TypeError: Attribute value must be int for selected attribute, stdClass given
163+
ValueError: Error mode must be one of PDO::ERRMODE_* constants
164164

165165
Warning: PDO::query(): SQLSTATE[42000]: Syntax error or access violation: %d You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near '%s' at line %d in %s on line %d
166166

0 commit comments

Comments
 (0)