Skip to content

Commit 44f72e9

Browse files
committed
avoid a possible double-free at the cost of a possible leak
1 parent 6a3a708 commit 44f72e9

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Modules/_sqlite/cursor.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,12 +1367,18 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self)
13671367
return NULL;
13681368
}
13691369

1370-
if (self->statement && stmt_reset(self->statement) != SQLITE_OK) {
1371-
cursor_cannot_reset_stmt_error(self, 0);
1372-
return NULL;
1370+
if (self->statement) {
1371+
int rc = stmt_reset(self->statement);
1372+
// Force self->statement to be NULL even if stmt_reset() may have
1373+
// failed to avoid a possible double-free if someone calls close()
1374+
// twice as a leak here would be better than a double-free.
1375+
Py_CLEAR(self->statement);
1376+
if (rc != SQLITE_OK) {
1377+
cursor_cannot_reset_stmt_error(self, 0);
1378+
return NULL;
1379+
}
13731380
}
13741381

1375-
Py_CLEAR(self->statement);
13761382
self->closed = 1;
13771383

13781384
Py_RETURN_NONE;

0 commit comments

Comments
 (0)