From 5e4f40c860b60f76a46b999cdc2606ac287a975c Mon Sep 17 00:00:00 2001 From: jilles Date: Thu, 20 Aug 2015 22:05:55 +0000 Subject: sh: Avoid negative character values from $'\Uffffffff' etc. The negative value was not expected and generated the low 8 bits as a byte, which may be an invalid character encoding. The final shift in creating the negative value was undefined as well. Make the temporary variable unsigned to fix this. --- bin/sh/parser.c | 11 ++++++----- bin/sh/tests/parser/Makefile | 1 + bin/sh/tests/parser/dollar-quote13.0 | 8 ++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 bin/sh/tests/parser/dollar-quote13.0 (limited to 'bin') diff --git a/bin/sh/parser.c b/bin/sh/parser.c index 2bba84e..e7ec010 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -1195,7 +1195,8 @@ parsebackq(char *out, struct nodelist **pbqlist, static char * readcstyleesc(char *out) { - int c, v, i, n; + int c, vc, i, n; + unsigned int v; c = pgetc(); switch (c) { @@ -1310,12 +1311,12 @@ readcstyleesc(char *out) default: synerror("Bad escape sequence"); } - v = (char)v; + vc = (char)v; /* * We can't handle NUL bytes. * POSIX says we should skip till the closing quote. */ - if (v == '\0') { + if (vc == '\0') { while ((c = pgetc()) != '\'') { if (c == '\\') c = pgetc(); @@ -1332,9 +1333,9 @@ readcstyleesc(char *out) pungetc(); return out; } - if (SQSYNTAX[v] == CCTL) + if (SQSYNTAX[vc] == CCTL) USTPUTC(CTLESC, out); - USTPUTC(v, out); + USTPUTC(vc, out); return out; } diff --git a/bin/sh/tests/parser/Makefile b/bin/sh/tests/parser/Makefile index 83df985..cbd2907 100644 --- a/bin/sh/tests/parser/Makefile +++ b/bin/sh/tests/parser/Makefile @@ -37,6 +37,7 @@ FILES+= dollar-quote9.0 FILES+= dollar-quote10.0 FILES+= dollar-quote11.0 FILES+= dollar-quote12.0 +FILES+= dollar-quote13.0 FILES+= empty-braces1.0 FILES+= empty-cmd1.0 FILES+= for1.0 diff --git a/bin/sh/tests/parser/dollar-quote13.0 b/bin/sh/tests/parser/dollar-quote13.0 new file mode 100644 index 0000000..2247da7 --- /dev/null +++ b/bin/sh/tests/parser/dollar-quote13.0 @@ -0,0 +1,8 @@ +# $FreeBSD$ + +# This Unicode escape sequence that has never been in range should either +# fail to expand or expand to a fallback. + +c=$(eval printf %s \$\'\\Uffffff41\' 2>/dev/null) +r=$(($? != 0)) +[ "$r.$c" = '1.' ] || [ "$r.$c" = '0.?' ] || [ "$r.$c" = $'0.\u2222' ] -- cgit v1.1