[New-bugs-announce] [issue13676] sqlite3: Zero byte truncates string contents

Petri Lehtinen report at bugs.python.org
Thu Dec 29 13:27:34 CET 2011

New submission from Petri Lehtinen <petri at digip.org>:

Inserting a string with embedded zero byte only inserts the string up to the first zero byte:

import sqlite3

connection = sqlite3.connect(':memory:')
cursor = connection.cursor()

cursor.execute('CREATE TABLE test (value TEXT)')
cursor.execute('INSERT INTO test (value) VALUES (?)', ('foo\x00bar',))

cursor.execute('SELECT value FROM test')
# expected output: (u'foo\x00bar',)
# actual output: (u'foo',)

Also, if there's already data inserted to a table like above with embedded zero bytes, the sqlite-API-to-Python-string conversion truncates the strings to just before the first zero byte.

Attaching a patch against 3.3 that fixes the problem. Basically, it uses PyUnicode_AsStringAndSize and PyUnicode_FromStringAndSize instead of the non-size variants.

Please review, as I'm not sure it covers each possible case.

components: Library (Lib)
files: sqlite3_zero_byte.patch
keywords: needs review, patch
messages: 150329
nosy: ghaering, petri.lehtinen
priority: normal
severity: normal
status: open
title: sqlite3: Zero byte truncates string contents
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3
Added file: http://bugs.python.org/file24104/sqlite3_zero_byte.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list