From a6aebe155c45d43fb217dfd96bb67ec3e21d9fd1 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sat, 11 Apr 2026 12:45:49 -0700 Subject: [PATCH] sed: don't segfault for "sed -f<(echo ':label') --debug" Using --debug after -f on the command line would deref uninitialized memory usually evoking a segfault. * sed/compile.c (next_cmd_entry): Zero-initialize the struct. * sed/debug.c (debug_print_function): Print only when label_name is non-NULL. * testsuite/debug-label.sh: New test. * testsuite/local.mk (T): Add it. Reported in https://debbugs.gnu.org/70232 --- NEWS | 5 ++++- sed/compile.c | 5 +---- sed/debug.c | 3 ++- testsuite/debug-label.sh | 34 ++++++++++++++++++++++++++++++++++ testsuite/local.mk | 1 + 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 testsuite/debug-label.sh diff --git a/NEWS b/NEWS index 96818c3..c72761a 100644 --- a/NEWS +++ b/NEWS @@ -4,13 +4,16 @@ GNU sed NEWS -*- outline -*- ** Bug fixes + sed --debug no longer crashes when a label (":") command is compiled + before the --debug option is processed, e.g., sed -f<(...) --debug. + [bug introduced in sed 4.7 with --debug] + sed no longer rejects the documented GNU extension 'a**' (equivalent to 'a*') in Basic Regular Expression (BRE) mode. Previously, this worked only with -E (ERE mode), even though grep has always accepted it in BRE mode. [bug present since "the beginning"] - sed no longer rejects "\c[" in regular expressions [bug present since the beginning] diff --git a/sed/compile.c b/sed/compile.c index cf64679..bc7e9dc 100644 --- a/sed/compile.c +++ b/sed/compile.c @@ -362,10 +362,7 @@ next_cmd_entry (struct vector *v) v->v = xpalloc (v->v, &v->v_allocated, 1, -1, sizeof *v->v); cmd = v->v + v->v_length; - cmd->a1 = NULL; - cmd->a2 = NULL; - cmd->range_state = RANGE_INACTIVE; - cmd->addr_bang = false; + memset (cmd, 0, sizeof *cmd); cmd->cmd = '\0'; /* something invalid, to catch bugs early */ return cmd; diff --git a/sed/debug.c b/sed/debug.c index a15c0a8..0af1157 100644 --- a/sed/debug.c +++ b/sed/debug.c @@ -276,7 +276,8 @@ debug_print_function (const struct vector *program, const struct sed_cmd *sc) break; case ':': - printf ("%s", sc->x.label_name); + if (sc->x.label_name) + printf ("%s", sc->x.label_name); break; case '{': diff --git a/testsuite/debug-label.sh b/testsuite/debug-label.sh new file mode 100644 index 0000000..b15b508 --- /dev/null +++ b/testsuite/debug-label.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# Ensure --debug doesn't crash with labels compiled before --debug is set + +# Copyright (C) 2026 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +. "${srcdir=.}/testsuite/init.sh"; path_prepend_ ./sed +print_ver_ sed + +# When --debug appears after -f, the label command is compiled +# with debug=false, so label_name is not set. This must not crash. +printf ':label\n' > prog || framework_failure_ + +sed -f prog --debug < /dev/null > out 2>&1 || fail=1 + +# The label name may be absent (compiled without debug), but no crash. +grep '^SED PROGRAM:' out > /dev/null || fail=1 + +# When --debug appears before -f, the label name should be printed. +sed --debug -f prog < /dev/null > out2 2>&1 || fail=1 +grep ':label' out2 > /dev/null || fail=1 + +Exit $fail diff --git a/testsuite/local.mk b/testsuite/local.mk index 62802a6..bebd0fa 100644 --- a/testsuite/local.mk +++ b/testsuite/local.mk @@ -44,6 +44,7 @@ LOG_COMPILER = false T = \ testsuite/misc.pl \ testsuite/bre-star-star.sh \ + testsuite/debug-label.sh \ testsuite/bug80573.sh \ testsuite/bug32082.sh \ testsuite/bug32271-1.sh \