Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

sqlite3: Blob slice assignment skips validation for empty slices #150913

Copy link
Copy link
@ever0de

Description

@ever0de
Issue body actions

Bug report

Bug description:

sqlite3.Blob slice assignment skips type and size validation when the
target slice is empty (start == stop, or any slice whose computed
length is zero).

As a result, assignments that would normally raise TypeError or
IndexError complete silently when the target slice is empty.

This behavior is inconsistent with non-empty sqlite3.Blob slice
assignment and appears to be caused by an early return in
ass_subscript_slice() before validation is performed.

Reproduction

import sqlite3

con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE t(b BLOB)")
con.execute("INSERT INTO t VALUES(zeroblob(10))")
con.commit()

blob = con.blobopen("t", "b", 1)

# Non-empty slice — expected behavior
blob[0:3] = b"12345"  # IndexError
blob[0:3] = None      # TypeError

# Empty slice — BUG
blob[5:5] = b"123"    # silently succeeds
blob[5:5] = None      # silently succeeds

Expected behavior

Empty slice assignments should be validated in the same way as non-empty
slice assignments.

blob[5:5] = b"123"

should raise:

IndexError: Blob slice assignment is wrong size

and

blob[5:5] = None

should raise:

TypeError: a bytes-like object is required, not 'NoneType'

Actual behavior

Both assignments complete successfully. The blob remains unchanged and
no exception is raised.

Root cause

In Modules/_sqlite/blob.c, ass_subscript_slice() returns early when
the computed slice length is zero.

Because this early return occurs before the bytes-like object check and
slice-size validation, invalid assignments are silently accepted for
empty slices.

Related

Discovered during review of #150450 (negative-step slice handling for
sqlite3.Blob).

Review discussion:

#150450 (comment)

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for issues without a type.

    Projects

    Status
    Done
    Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.