pgsql: psql: Avoid returning oom_buffer from psql slash command scanner

From: Fujii Masao <fujii(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: psql: Avoid returning oom_buffer from psql slash command scanner
Date: 2026-08-19 03:40:39
Message-ID: E1wwXAM-000000019VS-2qaw@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

psql: Avoid returning oom_buffer from psql slash command scanner

psql_scan_slash_command() builds the command name in a local
PQExpBufferData and returns the buffer's data pointer to its caller. If
either the initial allocation or a later enlargement failed, that data
pointer could be the static PQExpBuffer OOM buffer rather than malloc-owned
storage. HandleSlashCmds() could then eventually pass it to free(), causing
undefined behavior.

Detect a broken command-name buffer before returning it, report OOM, and
return NULL instead. Teach HandleSlashCmds() to treat a NULL command name
as a command error before trying to compare or dispatch it.

Backpatch to all supported versions.

Reported-by: Junwang Zhao <zhjwpku(at)gmail(dot)com>
Author: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Reviewed-by: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Reviewed-by: Junwang Zhao <zhjwpku(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAHGQGwEh3R3=1tx_a5=fTDJ+ycuwxWMEn6bG_Yt4B5P+hE7AVw@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/3d41f014f1a09d800b40f3da169de8b77c6bc577

Modified Files
--------------
src/bin/psql/command.c | 4 +++-
src/bin/psql/psqlscanslash.l | 15 +++++++++++++--
2 files changed, 16 insertions(+), 3 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2026-08-19 03:40:52 pgsql: psql: Avoid returning oom_buffer from psql slash command scanner
Previous Message Fujii Masao 2026-08-19 03:40:29 pgsql: psql: Fix psql slash option leaks