less/backport-Implement-osc8_open.patch
wangjiang 539cc6551c fix CVE-2024-32487
(cherry picked from commit 40e778a4dd7eb55923df57222dc3f7116eae2a7e)
2024-04-22 16:41:16 +08:00

70 lines
1.9 KiB
Diff

From 90d9d12ba9d3818a0074f33c5153b577d07aa8fd Mon Sep 17 00:00:00 2001
From: Mark Nudelman <markn@greenwoodsoftware.com>
Date: Tue, 16 Jan 2024 18:14:33 -0800
Subject: [PATCH] Implement osc8_open().
---
filename.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/filename.c b/filename.c
index 672dc94..5d7a5ef 100644
--- a/filename.c
+++ b/filename.c
@@ -136,7 +136,7 @@ static int metachar(char c)
/*
* Insert a backslash before each metacharacter in a string.
*/
-public char * shell_quote(constant char *s)
+public char * shell_quoten(constant char *s, size_t slen)
{
constant char *p;
char *np;
@@ -151,7 +151,7 @@ public char * shell_quote(constant char *s)
* Determine how big a string we need to allocate.
*/
len = 1; /* Trailing null byte */
- for (p = s; *p != '\0'; p++)
+ for (p = s; p < s + slen; p++)
{
len++;
if (*p == openquote || *p == closequote)
@@ -181,7 +181,7 @@ public char * shell_quote(constant char *s)
* We can't quote a string that contains quotes.
*/
return (NULL);
- len = (int) strlen(s) + 3;
+ len = slen + 3;
}
/*
* Allocate and construct the new string.
@@ -189,10 +189,11 @@ public char * shell_quote(constant char *s)
newstr = np = (char *) ecalloc(len, sizeof(char));
if (use_quotes)
{
- SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
+ SNPRINTF4(newstr, len, "%c%.*s%c", openquote, (int) slen, s, closequote);
} else
{
- while (*s != '\0')
+ constant char *es = s + slen;
+ while (s < es)
{
if (metachar(*s))
{
@@ -209,6 +210,11 @@ public char * shell_quote(constant char *s)
return (newstr);
}
+public char * shell_quote(char *s)
+{
+ return shell_quoten(s, strlen(s));
+}
+
/*
* Return a pathname that points to a specified file in a specified directory.
* Return NULL if the file does not exist in the directory.
--
2.43.0