Skip to content

sqlite3.Cursor.fetchmany(0) fetches all values instead of none #139283

@mendlero

Description

@mendlero

Bug report

Bug description:

If you run the following code, which uses fetchmany(0), you see it will act like fetchall by fetching everything instead of just returning an empty list, hypothetically, this could even be a security exploit if mismanaged.
This code was tested on both python 3.12.0 and python 3.13.7

import sqlite3

# setup
conn = sqlite3.connect(":memory:")
conn.execute('CREATE TABLE IF NOT EXISTS example_table (id INTEGER PRIMARY KEY);')
conn.execute('INSERT INTO example_table (id) VALUES (1), (2), (3);')
conn.commit()

# bug
cursor = conn.execute('SELECT id FROM example_table;')
ids = cursor.fetchmany(0)
print(ids)  # Expected: [], Actual: [(1,), (2,), (3,)]
remaining = cursor.fetchall() # will be []
print(remaining)

"""
TERMINAL EXECUTION ON WINDOWS (CMD):
>python test.py
[(1,), (2,), (3,)]
[]
"""

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

Labels

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions