Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ext/pdo: Convert FETCH_INTO zval to a zend_object pointer #17525

Merged
merged 2 commits into from
Jan 24, 2025

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Jan 19, 2025

No description provided.

@Girgias Girgias force-pushed the pdo-fetch-into-refactor branch from d006118 to b442b69 Compare January 19, 2025 21:48
@Girgias Girgias marked this pull request as ready for review January 19, 2025 23:52
@Girgias Girgias requested a review from nielsdos January 19, 2025 23:52
@@ -1786,7 +1786,8 @@ bool pdo_stmt_setup_fetch_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_a
return false;
}

ZVAL_COPY(&stmt->fetch.into, &args[0]);
GC_TRY_ADDREF(Z_OBJ(args[0]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why try?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why I did this, I think I didn't want to get bit by SHM stuff, forgetting that objects can't live in it. (although can enums live in SHM?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects indeed can't live inside shm, and so enums can't either. If you use a default value with an enum you're actually storing an IS_CONSTANT_AST and deferring the evaluation into an object to the runtime.

*gc_data = &stmt->fetch.into;
*gc_count = 1;

if (stmt->default_fetch_type & PDO_FETCH_INTO) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bitwise AND check seems wrong, PDO_FETCH_INTO is part of an enum with sequential integer indices so that means that there will be fetch mode with overlapping bits.
That said, I also think there's a pre-existing bug here: because into is part of a union, we should indeed only add the object/zval to the gc buffer if it is in this mode...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of how the current struct is organized, this "into" zval also handles the class fetch ctor arguments.

Which means that I do have a bug indeed, thanks for making me realize this, I guess time to write even more tests to handle these cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but the zval also overlap with an int, which is the bigger problem.

@Girgias Girgias force-pushed the pdo-fetch-into-refactor branch from bbb2669 to 842f8ef Compare January 24, 2025 20:33
@Girgias Girgias requested a review from nielsdos January 24, 2025 20:33
Comment on lines 2034 to 2036
if ((stmt->default_fetch_type & PDO_FETCH_INTO) == PDO_FETCH_INTO) {
zend_get_gc_buffer_add_obj(gc_buffer, stmt->fetch.into);
} else if ((stmt->default_fetch_type & PDO_FETCH_CLASS) == PDO_FETCH_CLASS) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a bitwise AND here seems wrong, as they're not bitmasks, so you're going to hit the condition for more than just PDO_FETCH_INTO or PDO_FETCH_CLASS.
See

enum pdo_fetch_type {
PDO_FETCH_USE_DEFAULT,
PDO_FETCH_LAZY,
PDO_FETCH_ASSOC,
PDO_FETCH_NUM,
PDO_FETCH_BOTH,
PDO_FETCH_OBJ,
PDO_FETCH_BOUND, /* return true/false only; rely on bound columns */
PDO_FETCH_COLUMN, /* fetch a numbered column only */
PDO_FETCH_CLASS, /* create an instance of named class, call ctor and set properties */
PDO_FETCH_INTO, /* fetch row into an existing object */
PDO_FETCH_FUNC, /* fetch into function and return its result */
PDO_FETCH_NAMED, /* like PDO_FETCH_ASSOC, but can handle duplicate names */
PDO_FETCH_KEY_PAIR, /* fetch into an array where the 1st column is a key and all subsequent columns are values */
PDO_FETCH__MAX /* must be last */
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the default_fetch_type is a union of the fetch mode, with the fetch flags. I could bitwise not it with PDO_FETCH_FLAGS to separate out the flags from the mode as a bunch of other places do it.

Copy link
Member

@nielsdos nielsdos Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I remember now, but it's still wrong: you should AND with ~PDO_FETCH_FLAGS otherwise the following can happen:

Known: PDO_FETCH_CLASS equals 8 (0b1000), PDO_FETCH_INTO equals 9 (0b1001).
Problem example: let's say default_fetch_type is PDO_FETCH_INTO, and we look at the condition (stmt->default_fetch_type & PDO_FETCH_CLASS) == PDO_FETCH_CLASS. This condition expands to: (0b1001 & 0b1000) == 0b1000. We know that 0b1001 & 0b1000 is 0b1000, so it will be equal to PDO_FETCH_CLASS even though that's not what we want. In this particular case you don't run into it because of the ordering of the branches but that's extremely fragile and I'm sure you can come up with another combination of modes to trigger this kind of behaviour.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right now, let's wait for CI though.

@Girgias Girgias merged commit 6fc49ab into php:master Jan 24, 2025
8 of 9 checks passed
@Girgias Girgias deleted the pdo-fetch-into-refactor branch January 24, 2025 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants