[PATCH] builtin: Add CTLESC before all CCTLs in dollar single quote

Herbert Xu <[email protected]> Sat, 4 Jul 2026 12:17:45 +0800
Newsgroups org.kernel.vger.dash
Message-ID <[email protected]>
Martijn Dekker <[email protected]> wrote:
> The quoted strings '\\' and $'\\\\' are identical, but the 'case' statement in 
> the current git version of dash does not match them as such.
> 
> Reproducer:
> 
> case '\\' in
> ( $'\\\\' )     echo ok ;;
> ( * )           echo WRONG ;;
> esac

Thanks for the report.  This patch should fix the problem:

---8<---
If a backslash that comes from dollar-single quote escape sequence
is used as a pattern, it should be escaped automatically so that
it's interpreted as a literal backslash, rather than as a quoting
character.

Reported-by: Martijn Dekker <[email protected]>
Fixes: 776424a8f915 ("parser: Add dollar single quote")
Signed-off-by: Herbert Xu <[email protected]>

diff --git a/src/bltin/printf.c b/src/bltin/printf.c
index c2c3615..5355f5d 100644
--- a/src/bltin/printf.c
+++ b/src/bltin/printf.c
@@ -56,6 +56,7 @@ static char  **gargv;
 
 #include "bltin.h"
 #include "parser.h"
+#include "syntax.h"
 #include "system.h"
 
 #define PF(f, func) { \
@@ -350,7 +351,7 @@ unsigned conv_escape(char *str0, char *out0, bool mbchar)
 
 		value = '\\';
 
-		if (isodigit(ch)) {
+		if (unlikely(isodigit(ch))) {
 			ch = 3;
 			value = 0;
 			do {
@@ -362,8 +363,12 @@ unsigned conv_escape(char *str0, char *out0, bool mbchar)
 		str--;
 
 check_value:
-		if (mbchar && (signed char)value >= CTL_FIRST &&
-		    (signed char)value <= CTL_LAST)
+		if (SQSYNTAX[(signed char)value] != CCTL)
+			break;
+		/* fall through */
+
+	case '\\':
+		if (mbchar)
 			USTPUTC(CTLESC, out);
 		break;
 
@@ -395,12 +400,12 @@ hex:
 			value += d;
 		} while (--ch);
 
-		if (value < 0x80)
-			break;
-
 		if (och <= 2)
 			goto check_value;
 
+		if (value < 0x80)
+			goto check_value;
+
 		if (value < 0x110000) {
 			int mboff = (mbchar - 1) * 2;
 			unsigned uni = value;
@@ -442,9 +447,6 @@ hex:
 		ch = 4;
 		goto hex;
 
-	case '\\':
-		break;
-
 	case 'a':				/* alert */
 	case 'b':				/* backspace */
 	case 'f':				/* form-feed */
-- 
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt