summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFujii Masao2026-08-19 03:34:28 +0000
committerFujii Masao2026-08-19 03:36:14 +0000
commitd2e19268dc9f2fc9a28eeb8052b96d5f3f417f00 (patch)
tree343f820bbda2953637d469b845223b23287f4590
parentab001a3a6925e882d2585903a7448a250ec7b428 (diff)
psql: Avoid returning oom_buffer from psql slash option scanner
psql_scan_slash_option() builds option text 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. The callers could then eventually pass it to free(), causing undefined behavior. Detect a broken option buffer before returning it, report OOM, and return NULL instead. Also avoid evaluating a backtick substitution when the option buffer is already broken, since doing so could otherwise touch the static OOM buffer. This keeps the existing NULL-return convention for slash options. Callers are not generally changed to distinguish OOM from no option. Backpatch to all supported versions. Reported-by: Junwang Zhao <zhjwpku@gmail.com> Author: Fujii Masao <masao.fujii@gmail.com> Reviewed-by: Chao Li <li.evan.chao@gmail.com> Reviewed-by: Junwang Zhao <zhjwpku@gmail.com> Discussion: https://postgr.es/m/CAHGQGwEh3R3=1tx_a5=fTDJ+ycuwxWMEn6bG_Yt4B5P+hE7AVw@mail.gmail.com Backpatch-through: 14
-rw-r--r--src/bin/psql/command.c2
-rw-r--r--src/bin/psql/psqlscanslash.l24
2 files changed, 23 insertions, 3 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 85c39c4eda5..13eaf467968 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2014,7 +2014,7 @@ exec_command_lo(PsqlScanState scan_state, bool active_branch, const char *cmd)
if (strcmp(cmd + 3, "export") == 0)
{
- if (!opt2)
+ if (!opt1 || !opt2)
{
pg_log_error("\\%s: missing required argument", cmd);
success = false;
diff --git a/src/bin/psql/psqlscanslash.l b/src/bin/psql/psqlscanslash.l
index 695f312bfd5..82e2745da7c 100644
--- a/src/bin/psql/psqlscanslash.l
+++ b/src/bin/psql/psqlscanslash.l
@@ -538,7 +538,8 @@ psql_scan_slash_command(PsqlScanState state)
/*
* Parse off the next argument for a backslash command, and return it as a
- * malloc'd string. If there are no more arguments, returns NULL.
+ * malloc'd string. If there are no more arguments or on out-of-memory,
+ * returns NULL.
*
* type tells what processing, if any, to perform on the option string;
* for example, if it's a SQL identifier, we want to downcase any unquoted
@@ -615,6 +616,16 @@ psql_scan_slash_option(PsqlScanState state,
*/
Assert(lexresult == LEXRES_EOL || lexresult == LEXRES_OK);
+ /*
+ * yylex() appends option text to mybuf, so a buffer enlargement failure
+ * during lexing can leave mybuf broken even if initialization succeeded.
+ */
+ if (PQExpBufferDataBroken(mybuf))
+ {
+ pg_log_error("out of memory");
+ return NULL;
+ }
+
switch (final_state)
{
case xslashargstart:
@@ -838,7 +849,7 @@ static void
evaluate_backtick(PsqlScanState state)
{
PQExpBuffer output_buf = state->output_buf;
- char *cmd = output_buf->data + backtick_start_offset;
+ char *cmd;
PQExpBufferData cmd_output;
FILE *fd;
bool error = false;
@@ -846,6 +857,15 @@ evaluate_backtick(PsqlScanState state)
char buf[512];
size_t result;
+ /*
+ * The option buffer is already broken; avoid touching the static
+ * oom_buffer and let psql_scan_slash_option() return NULL.
+ */
+ if (PQExpBufferBroken(output_buf))
+ return;
+
+ cmd = output_buf->data + backtick_start_offset;
+
initPQExpBuffer(&cmd_output);
fflush(NULL);