sed: escape unusual chars in file name diagnostics

Problem reported by Michał Majchrowicz and Marcin Wyczechowski.
* NEWS, THANKS.in: Mention this.
* bootstrap.conf (gnulib_modules): Add quotearg.
* sed/compile.c (vbad_prog):
* sed/execute.c (open_next_file):
Quote file names in diagnostics.
* sed/utils.c: Include quotearg.h.
(quotef, quotef_n): New functions, inspired by coreutils macros
of the same name.
* sed/utils.c (ck_fopen, ck_fdopen, ck_mkstemp, ck_fwrite)
(ck_fread, ck_getdelim, ck_fflush, follow_symlink, ck_rename):
This commit is contained in:
Paul Eggert committed 2026-05-25 10:02:46 -07:00
1 parent 560dc3864c
commit 3ffece093f
7 files changed
+55 -22

No files matched your search

+4
View File
@@ -8,6 +8,10 @@ GNU sed NEWS -*- outline -*-
's' command.
[bug introduced in sed 4.10]
File names with odd characters are now quoted in diagnostics.
The quoting style is the same as 'ls --quoting-style=shell-escape'.
[bug present since the beginning]
* Noteworthy changes in release 4.10 (2026-04-21) [stable]
+2
View File
@@ -43,8 +43,10 @@ Kaveh R. Ghazi ghazi@caip.rutgers.edu
Kevin Buettner kev@cujo.geg.mot.com
Laurent Vogel lvl@club-internet.fr
Maciej W. Rozycki macro@linux-mips.org
Marcin Wyczechowski mwyczechowski@afine.com
Mark Kettenis kettenis@phys.uva.nl
Michael De La Rue delarue@NTCCSC01WA.ntc.nokia.com
Michał Majchrowicz mmajchrowicz@afine.com
Michel de Ruiter mdruiter@cs.vu.nl
Randall Cotton recotton@earthlink.net
Robert A Bruce rab@allspice.berkeley.edu
+1
View File
@@ -48,6 +48,7 @@ mkostemp
obstack
perl
progname
quotearg
readlink
readme-release
regex
+1 -1
View File
@@ -119,7 +119,7 @@ vbad_prog (char const *why, va_list ap)
{
if (cur_input.name)
fprintf (stderr, _("%s: file %s line %jd: "), program_name,
cur_input.name, cur_input.line);
quotef (cur_input.name), cur_input.line);
else
fprintf (stderr, _("%s: -e expression #%d, char %td: "),
program_name,
+10 -7
View File
@@ -566,7 +566,7 @@ open_next_file (const char *name, struct input *input)
{
const char *ptr = strerror (errno);
fprintf (stderr, _("%s: can't read %s: %s\n"), program_name,
name, ptr);
quotef (name), ptr);
input->read_fn = read_always_fail; /* a redundancy */
++input->bad_count;
return;
@@ -591,12 +591,14 @@ open_next_file (const char *name, struct input *input)
strcpy (tmpdir, ".");
if (isatty (fileno (input->fp)))
panic (_("couldn't edit %s: is a terminal"), input->in_file_name);
panic (_("couldn't edit %s: is a terminal"),
quotef (input->in_file_name));
input_fd = fileno (input->fp);
fstat (input_fd, &input->st);
if (!S_ISREG (input->st.st_mode))
panic (_("couldn't edit %s: not a regular file"), input->in_file_name);
panic (_("couldn't edit %s: not a regular file"),
quotef (input->in_file_name));
if (is_selinux_enabled () > 0)
{
@@ -609,7 +611,7 @@ open_next_file (const char *name, struct input *input)
if (setfscreatecon (con) < 0)
fprintf (stderr, _("%s: warning: failed to set default" \
" file creation context to %s: %s\n"),
program_name, con, strerror (errno));
program_name, quotef (con), strerror (errno));
freecon (con);
}
else
@@ -617,7 +619,8 @@ open_next_file (const char *name, struct input *input)
if (errno != ENOSYS)
fprintf (stderr, _("%s: warning: failed to get" \
" security context of %s: %s\n"),
program_name, input->in_file_name, strerror (errno));
program_name, quotef (input->in_file_name),
strerror (errno));
}
}
@@ -633,8 +636,8 @@ open_next_file (const char *name, struct input *input)
}
if (!output_file.fp)
panic (_("couldn't open temporary file %s: %s"), input->out_file_name,
strerror (errno));
panic (_("couldn't open temporary file %s: %s"),
quotef (input->out_file_name), strerror (errno));
}
else
{
+34 -14
View File
@@ -30,6 +30,7 @@
#include "eloop-threshold.h"
#include "idx.h"
#include "minmax.h"
#include "quotearg.h"
#include "unlocked-io.h"
#include "utils.h"
#include "progname.h"
@@ -86,6 +87,20 @@ panic (const char *str, ...)
exit (EXIT_PANIC);
}
/* Quote file names. */
char *
quotef (char const *arg)
{
return quotef_n (0, arg);
}
char *
quotef_n (int n, char const *arg)
{
return quotearg_n_style_colon (n, shell_escape_quoting_style, arg);
}
/* Internal routine to get a filename from open_files */
static const char * _GL_ATTRIBUTE_PURE
utils_fp_name (FILE *fp)
@@ -126,7 +141,7 @@ ck_fopen (const char *name, const char *mode, int fail)
if (!fp)
{
if (fail)
panic (_("couldn't open file %s: %s"), name, strerror (errno));
panic (_("couldn't open file %s: %s"), quotef (name), strerror (errno));
return NULL;
}
@@ -145,7 +160,7 @@ ck_fdopen ( int fd, const char *name, const char *mode, int fail)
if (!fp)
{
if (fail)
panic (_("couldn't attach to %s: %s"), name, strerror (errno));
panic (_("couldn't attach to %s: %s"), quotef (name), strerror (errno));
return NULL;
}
@@ -209,7 +224,7 @@ ck_mkstemp (char **p_filename, const char *tmpdir,
#if O_BINARY
if (binary_mode && set_binary_mode (fd, O_BINARY) == -1)
panic (_("failed to set binary mode on '%s'"), template);
panic (_("%s: failed to set binary mode"), quotef (template));
#endif
fp = fdopen (fd, mode);
@@ -217,7 +232,7 @@ ck_mkstemp (char **p_filename, const char *tmpdir,
}
if (!fp)
panic (_("couldn't open temporary file %s: %s"), template,
panic (_("couldn't open temporary file %s: %s"), quotef (template),
strerror (err));
register_open_file (fp, template);
@@ -232,8 +247,8 @@ ck_fwrite (const void *ptr, idx_t size, idx_t nmemb, FILE *stream)
if (size && fwrite (ptr, size, nmemb, stream) != nmemb)
panic (ngettext ("couldn't write %jd item to %s: %s",
"couldn't write %jd items to %s: %s", nmemb),
nmemb, utils_fp_name (stream),
strerror (errno));
nmemb, quotef (utils_fp_name (stream)),
strerror (errno));
}
/* Panic on failing fread */
@@ -242,7 +257,8 @@ ck_fread (void *ptr, idx_t size, idx_t nmemb, FILE *stream)
{
clearerr (stream);
if (size && (nmemb=fread (ptr, size, nmemb, stream)) <= 0 && ferror (stream))
panic (_("read error on %s: %s"), utils_fp_name (stream), strerror (errno));
panic (_("read error on %s: %s"), quotef (utils_fp_name (stream)),
strerror (errno));
return nmemb;
}
@@ -251,11 +267,12 @@ ssize_t
ck_getdelim (char **text, size_t *buflen, char delim, FILE *stream)
{
if (ferror (stream))
panic (_("read error on %s"), utils_fp_name (stream));
panic (_("read error on %s"), quotef (utils_fp_name (stream)));
ssize_t result = getdelim (text, buflen, delim, stream);
if (ferror (stream)) /* implies result < 0, hence errno is set */
panic (_("read error on %s: %s"), utils_fp_name (stream), strerror (errno));
panic (_("read error on %s: %s"), quotef (utils_fp_name (stream)),
strerror (errno));
return result;
}
@@ -269,7 +286,8 @@ ck_fflush (FILE *stream)
clearerr (stream);
if (fflush (stream) == EOF && errno != EBADF)
panic ("couldn't flush %s: %s", utils_fp_name (stream), strerror (errno));
panic ("couldn't flush %s: %s", quotef (utils_fp_name (stream)),
strerror (errno));
}
/* Panic on failing fclose */
@@ -309,7 +327,7 @@ do_ck_fclose (FILE *fp, char const *name)
clearerr (fp);
if (fclose (fp) == EOF)
panic ("couldn't close %s: %s", name, strerror (errno));
panic ("couldn't close %s: %s", quotef (name), strerror (errno));
}
/* Follow symlink FNAME and return the ultimate target, stored in a
@@ -354,10 +372,11 @@ follow_symlink (const char *fname)
{
if (errno == EINVAL)
break;
panic (_("couldn't readlink %s: %s"), fn, strerror (errno));
panic (_("couldn't readlink %s: %s"), quotef (fn), strerror (errno));
}
if (__eloop_threshold () <= num_links)
panic (_("couldn't follow symlink %s: %s"), fname, strerror (ELOOP));
panic (_("couldn't follow symlink %s: %s"), quotef (fname),
strerror (ELOOP));
if ((linklen == 0 || buf[buf_used] != '/') && (c = strrchr (fn, '/')))
{
@@ -404,7 +423,8 @@ ck_rename (const char *from, const char *to)
if (rd != -1)
return;
panic (_("cannot rename %s to %s: %s"), from, to, strerror (errno));
panic (_("cannot rename %s to %s: %s"),
quotef_n (0, from), quotef_n (1, to), strerror (errno));
}
+3
View File
@@ -51,3 +51,6 @@ idx_t size_buffer (struct buffer const *b) _GL_ATTRIBUTE_PURE;
char *add_buffer (struct buffer *b, const char *p, idx_t n);
char *add1_buffer (struct buffer *b, int ch);
void free_buffer (struct buffer *b);
char *quotef (char const *arg);
char *quotef_n (int n, char const *arg);