Skip to content

Commit b5b5336

Browse files
committed
Review for pdo_stmt.c
Promote a couple of confirmed cases to unconditional error Fix error messages
1 parent 198afbe commit b5b5336

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

ext/pdo/pdo_stmt.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,8 @@ static inline void fetch_value(pdo_stmt_t *stmt, zval *dest, int colno, int *typ
470470
}
471471

472472
if (colno >= stmt->column_count) {
473-
/* TODO Should this be a ValueError? */
474-
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "Invalid column index");
475-
ZVAL_FALSE(dest);
476-
473+
zend_value_error("Invalid column index");
474+
ZVAL_NULL(dest);
477475
return;
478476
}
479477

@@ -697,7 +695,7 @@ static int make_callable_ex(pdo_stmt_t *stmt, zval *callable, zend_fcall_info *
697695
zend_type_error("%s", is_callable_error);
698696
efree(is_callable_error);
699697
} else {
700-
zend_type_error("user-supplied function must be a valid callback");
698+
zend_type_error("User-supplied function must be a valid callback");
701699
}
702700
return false;
703701
}
@@ -824,9 +822,9 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h
824822
zend_value_error("Column index must be greater than or equal to 0");
825823
return false;
826824
}
827-
/* TODO Always a ValueError? */
825+
828826
if (colno >= stmt->column_count) {
829-
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "Invalid column index");
827+
zend_value_error("Invalid column index");
830828
return false;
831829
}
832830

@@ -1150,14 +1148,14 @@ static bool pdo_stmt_verify_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode
11501148
switch(mode) {
11511149
case PDO_FETCH_FUNC:
11521150
if (!fetch_all) {
1153-
zend_argument_value_error(mode_arg_num, "can only use PDO::FETCH_FUNC in PDOStatement::fetchAll()");
1151+
zend_value_error("Can only use PDO::FETCH_FUNC in PDOStatement::fetchAll()");
11541152
return 0;
11551153
}
11561154
return 1;
11571155

11581156
case PDO_FETCH_LAZY:
11591157
if (fetch_all) {
1160-
zend_argument_value_error(mode_arg_num, "cannot use PDO::FETCH_LAZY in PDOStatement::fetchAll()");
1158+
zend_value_error("Cannot be PDO::FETCH_LAZY in PDOStatement::fetchAll()");
11611159
return 0;
11621160
}
11631161
/* fall through */
@@ -1171,7 +1169,6 @@ static bool pdo_stmt_verify_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode
11711169
return 0;
11721170
}
11731171
if (mode >= PDO_FETCH__MAX) {
1174-
/* TODO Better error message? */
11751172
zend_argument_value_error(mode_arg_num, "must be a bitmask of PDO::FETCH_* constants");
11761173
return 0;
11771174
}
@@ -1228,8 +1225,9 @@ PHP_METHOD(PDOStatement, fetchObject)
12281225
PHP_STMT_GET_OBJ;
12291226
PDO_STMT_CLEAR_ERR();
12301227

1231-
/* use pdo_stmt_verify_mode() to set fetch mode for this specific statement */
1232-
ZEND_ASSERT(pdo_stmt_verify_mode(stmt, PDO_FETCH_CLASS, 0, false));
1228+
/* Use pdo_stmt_verify_mode() to set fetch mode for this specific statement */
1229+
/* This should NOT fail */
1230+
pdo_stmt_verify_mode(stmt, PDO_FETCH_CLASS, 0, false);
12331231

12341232
old_ce = stmt->fetch.cls.ce;
12351233
ZVAL_COPY_VALUE(&old_ctor_args, &stmt->fetch.cls.ctor_args);

ext/pdo/tests/pdo_038.phpt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ try {
3838
echo $e->getMessage(), \PHP_EOL;
3939
}
4040
var_dump(fetchColumn($stmt, 0));
41-
var_dump(fetchColumn($stmt, 1));
41+
try {
42+
var_dump(fetchColumn($stmt, 1));
43+
} catch (\ValueError $e) {
44+
echo $e->getMessage(), \PHP_EOL;
45+
}
4246
?>
43-
--EXPECTF--
47+
--EXPECT--
4448
Column index must be greater than or equal to 0
4549
string(1) "1"
46-
47-
Warning: PDOStatement::fetchColumn(): SQLSTATE[HY000]: General error: Invalid column index in %s
48-
bool(false)
50+
Invalid column index

0 commit comments

Comments
 (0)