less/backport-CVE-2024-32487.patch
wangjiang 5cc1abcb8a fix CVE-2024-32487
(cherry picked from commit af9f9c64c33c5dcc7eb71d2046b649321fbac25f)
2024-04-22 16:43:52 +08:00

71 lines
1.5 KiB
Diff

From 007521ac3c95bc76e3d59c6dbfe75d06c8075c33 Mon Sep 17 00:00:00 2001
From: Mark Nudelman <markn@greenwoodsoftware.com>
Date: Thu, 11 Apr 2024 17:49:48 -0700
Subject: [PATCH] Fix bug when viewing a file whose name contains a newline.
---
filename.c | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/filename.c b/filename.c
index 64d9ded..8b7d800 100644
--- a/filename.c
+++ b/filename.c
@@ -135,6 +135,15 @@ metachar(c)
return (strchr(metachars(), c) != NULL);
}
+/*
+ * Must use quotes rather than escape char for this metachar?
+ */
+static int must_quote(char c)
+{
+ /* {{ Maybe the set of must_quote chars should be configurable? }} */
+ return (c == '\n');
+}
+
/*
* Insert a backslash before each metacharacter in a string.
*/
@@ -170,6 +179,9 @@ shell_quoten(s)
* doesn't support escape chars. Use quotes.
*/
use_quotes = 1;
+ } else if (must_quote(*p))
+ {
+ len += 3; /* open quote + char + close quote */
} else
{
/*
@@ -200,15 +212,25 @@ shell_quoten(s)
constant char *es = s + slen;
while (s < es)
{
- if (metachar(*s))
+ if (!metachar(*s))
{
/*
* Add the escape char.
*/
+ *np++ = *s++;
+ } else if (must_quote(*s))
+ {
+ /* Surround the char with quotes. */
+ *np++ = openquote;
+ *np++ = *s++;
+ *np++ = closequote;
+ } else
+ {
+ /* Insert an escape char before the char. */
strcpy(np, esc);
np += esclen;
+ *np++ = *s++;
}
- *np++ = *s++;
}
*np = '\0';
}
--
2.43.0