devicetree-compiler Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: montytyper-uAjRD0nVeow@public.gmane.org
To: david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org,
	loeliger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Jordan Montgomery <montytyper-uAjRD0nVeow@public.gmane.org>
Subject: [PATCH] dtc: Return error when reading non-ascii chars
Date: Sat, 13 Mar 2021 20:40:54 -0800	[thread overview]
Message-ID: <BY5PR14MB37654DDEFC00C8C1ABAD35DECE6D9@BY5PR14MB3765.namprd14.prod.outlook.com> (raw)

From: Jordan Montgomery <montytyper-uAjRD0nVeow@public.gmane.org>

The underlying lexer code generated by flex checks
if(yychar <= YYEOF) to detect EOF. This returns true
for any char >= 0x80, resulting in premature EOF and
causing dtc to generate an invalid FDT.

This patch adds a rule to detect these illegal
characters and throw a fatal error instead, as well as
a test case which passes if dtc errors fatally and
fails otherwise.

Parsing of valid devicetrees should be unaffected by
this change.

Signed-off-by: Jordan Montgomery <montytyper-uAjRD0nVeow@public.gmane.org>
---
 dtc-lexer.l           |  6 ++++++
 tests/bad-unicode.dts |  6 ++++++
 tests/dtc-fatal.sh    | 11 +++++++++--
 tests/run_tests.sh    |  1 +
 4 files changed, 22 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 dtc-lexer.l
 create mode 100644 tests/bad-unicode.dts

diff --git a/dtc-lexer.l b/dtc-lexer.l
old mode 100644
new mode 100755
index b3b7270..668875b
--- a/dtc-lexer.l
+++ b/dtc-lexer.l
@@ -17,6 +17,7 @@ CHAR_LITERAL	'([^']|\\')*'
 WS		[[:space:]]
 COMMENT		"/*"([^*]|\*+[^*/])*\*+"/"
 LINECOMMENT	"//".*\n
+NONASCII	[\x80-\xff]
 
 %{
 #include "dtc.h"
@@ -245,6 +246,11 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
 <*>"&&"		{ return DT_AND; };
 <*>"||"		{ return DT_OR; };
 
+<*>{NONASCII}	{
+			DPRINT("Illegal character x%X\n", (unsigned)yytext[0]);
+			YY_FATAL_ERROR("Unable to parse non-ASCII character");
+		};
+
 <*>.		{
 			DPRINT("Char: %c (\\x%02x)\n", yytext[0],
 				(unsigned)yytext[0]);
diff --git a/tests/bad-unicode.dts b/tests/bad-unicode.dts
new file mode 100644
index 0000000..19b3d2f
--- /dev/null
+++ b/tests/bad-unicode.dts
@@ -0,0 +1,6 @@
+/dts-v1/;
+
+/ {
+    prop-str = "hello world";⌘
+    prop-str2 = "hello world";
+};
diff --git a/tests/dtc-fatal.sh b/tests/dtc-fatal.sh
index 08a4d29..56cdc1c 100755
--- a/tests/dtc-fatal.sh
+++ b/tests/dtc-fatal.sh
@@ -3,13 +3,20 @@
 SRCDIR=`dirname "$0"`
 . "$SRCDIR/testutils.sh"
 
+if [ "$1" = "-r" ]; then
+    expected="$2"
+    shift 2
+else
+    expected="1"
+fi
+
 verbose_run $VALGRIND "$DTC" -o/dev/null "$@"
 ret="$?"
 
 if [ "$ret" -gt 127 ]; then
     FAIL "dtc killed by signal (ret=$ret)"
-elif [ "$ret" != "1" ]; then
-    FAIL "dtc returned incorrect status $ret instead of 1"
+elif [ "$ret" != "$expected" ]; then
+    FAIL "dtc returned incorrect status $ret instead of $expected"
 fi
 
 PASS
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 4b8dada..c9bc095 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -764,6 +764,7 @@ dtc_tests () {
     run_sh_test "$SRCDIR/dtc-fatal.sh" -I dts -O dtb nosuchfile.dts
     run_sh_test "$SRCDIR/dtc-fatal.sh" -I dtb -O dtb nosuchfile.dtb
     run_sh_test "$SRCDIR/dtc-fatal.sh" -I fs -O dtb nosuchfile
+    run_sh_test "$SRCDIR/dtc-fatal.sh" -r 2 -I dts -O dtb bad-unicode.dts
 
     # Dependencies
     run_dtc_test -I dts -O dtb -o dependencies.test.dtb -d dependencies.test.d "$SRCDIR/dependencies.dts"
-- 
2.25.1


             reply	other threads:[~2021-03-14  4:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-14  4:40 montytyper-uAjRD0nVeow [this message]
     [not found] ` <BY5PR14MB37654DDEFC00C8C1ABAD35DECE6D9-ee0W+6mJUHcCBBnP8riMx04NFAmbP9jRvxpqHgZTriW3zl9H0oFU5g@public.gmane.org>
2021-03-23  3:32   ` [PATCH] dtc: Return error when reading non-ascii chars David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BY5PR14MB37654DDEFC00C8C1ABAD35DECE6D9@BY5PR14MB3765.namprd14.prod.outlook.com \
    --to=montytyper-uajrd0nveow@public.gmane.org \
    --cc=david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org \
    --cc=devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=loeliger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).