Skip to content

Conversation

picnixz
Copy link
Member

@picnixz picnixz commented Sep 25, 2025

@picnixz picnixz force-pushed the feat/sqlite/consolidate-api-usage-139327 branch from 5843251 to b50713c Compare September 26, 2025 09:58
Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

Is there a way to test this?

* Checks the SQLite error code and sets the appropriate DB-API exception.
*/
void
int
Copy link
Member

Choose a reason for hiding this comment

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

In blob.c, should blob_seterror assert that set_error_from_db didn't return SQLITE_OK?
Same for when connection.c and the files changed here call blob_seterror without checking the result.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I missed the blob stuff. I think we should also assert this indeed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, I think there was an API misuse here. We pass rc but we never use it. I think we were meant to use set_error_from_code instead.

_PyErr_ChainExceptions1(exc);
}
else {
// assert(!PyErr_Occurred());
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment about why this isn't asserted.

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 think it's because I had an abort here since PyErr_SetString can be called with an exception already set. I wanted to clean up the usages but I think it was too many changes that are not really related (see #139447 for the ongoing work). I'll retry now to see if this is indeed the case (it might also be a relicate of me testing the calls)

@picnixz
Copy link
Member Author

picnixz commented Oct 1, 2025

Er... the docs do not mention exactly the way to go wrong but I can try

(void)stmt_reset(self->statement);
Py_CLEAR(self->statement);
if (self->statement && stmt_reset(self->statement) != SQLITE_OK) {
cursor_cannot_reset_stmt_error(self, 0);
Copy link
Member Author

@picnixz picnixz Oct 5, 2025

Choose a reason for hiding this comment

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

Here I'm a bit unsure whether we should call Py_CLEAR(self->statement) after a reset failure. If we cannot reset the statement, that means we were not able to free the memory (or maybe it's something else). However, the exact return codes being returned are not documented on sqlite3's side so I can't have a finer check.

In other places, Py_CLEAR is always called even in the case of a reset failure, however here we're in the "close" method and thus I don't know if I should call Py_CLEAR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, actually I could have a double-free:

  • stmt_clear fails but internally manages to free the memory.
  • self->statement isnot cleared so still not NULL
  • someone recalls close() -> we retry to clear the memory -> double free.

@picnixz picnixz force-pushed the feat/sqlite/consolidate-api-usage-139327 branch from 446f580 to 44f72e9 Compare October 5, 2025 12:47
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