Skip to content

gh-153731: Handle NUL and lone surrogate in sqlite3 CLI REPL - #153732

Open
tonghuaroot wants to merge 4 commits into
python:mainfrom
tonghuaroot:sqlite3-cli-nul
Open

gh-153731: Handle NUL and lone surrogate in sqlite3 CLI REPL#153732
tonghuaroot wants to merge 4 commits into
python:mainfrom
tonghuaroot:sqlite3-cli-nul

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

In the python -m sqlite3 REPL, complete_statement() raises ValueError
(embedded NUL) or UnicodeEncodeError (lone surrogate), neither of which is a
sqlite3.Error, so a single such input line crashed the shell. Catch these two
and print an error, keeping the REPL running like the other error paths.

@aisk

aisk commented Jul 15, 2026

Copy link
Copy Markdown
Member

UnicodeEncodeError is actually a subclass of ValueError, so catching both is redundant.

But the main question is that ValueError is not narrow, many code paths may raise it. I think it's better to just add a pre-check to see whether \0 is in the input, and then show an error to the user.

We can also use a similar approach to check whether a lone surrogate is in the input, but I'm not an expert on Unicode, and UnicodeEncodeError is a narrow exception, so I think it's okay to keep catching it.

@tonghuaroot

Copy link
Copy Markdown
Contributor Author

Done in ab23364: pre-check \0 for the NUL (no longer catching the broad ValueError) and keep catching the narrow UnicodeEncodeError for a lone surrogate.

Comment thread Lib/sqlite3/__main__.py
try:
if args.sql:
# SQL statement provided on the command-line; execute it directly.
execute(con, args.sql, suppress_errors=False, theme=theme)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In this code path, passing an SQL statement on the command line without using the REPL may cause the same error. Maybe we should check it here too.

@tonghuaroot

Copy link
Copy Markdown
Contributor Author

Good catch. Done in 49a5763: execute() now applies the same NUL pre-check + narrow UnicodeEncodeError catch, so the direct command-line path is guarded too; added command-line tests.

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