summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Brunet <charles.brunet@optelgroup.com>2024-02-14 08:57:35 -0500
committerEli Schwartz <eschwartz93@gmail.com>2024-02-23 15:00:39 -0500
commit715dc27b2b8432d9749df985f262de8a8347c59b (patch)
tree17acc9cf96161b31fd403d0062bedc72c80fc7b2
parent138e0fe9841b44a6d0402b66bbd0c10966cfe87b (diff)
downloadmeson-715dc27b2b8432d9749df985f262de8a8347c59b.tar.gz
Detect utf8 bom from meson build files
Some text editors on Windows may use utf8bom encoding by default. Prevent crash and properly report misencoded files. Fixes #12766.
-rw-r--r--mesonbuild/interpreterbase/interpreterbase.py9
-rw-r--r--mesonbuild/mparser.py4
-rw-r--r--test cases/failing/130 utf8 with bom/meson.build3
-rw-r--r--test cases/failing/130 utf8 with bom/test.json8
-rw-r--r--test cases/failing/131 utf8 with bom subdir/meson.build3
-rw-r--r--test cases/failing/131 utf8 with bom subdir/subdir/meson.build1
-rw-r--r--test cases/failing/131 utf8 with bom subdir/test.json8
-rw-r--r--test cases/failing/132 utf8 with bom options/meson.build1
-rw-r--r--test cases/failing/132 utf8 with bom options/meson.options1
-rw-r--r--test cases/failing/132 utf8 with bom options/test.json8
10 files changed, 42 insertions, 4 deletions
diff --git a/mesonbuild/interpreterbase/interpreterbase.py b/mesonbuild/interpreterbase/interpreterbase.py
index 365e2fd4b..47dd46f32 100644
--- a/mesonbuild/interpreterbase/interpreterbase.py
+++ b/mesonbuild/interpreterbase/interpreterbase.py
@@ -107,10 +107,11 @@ class InterpreterBase:
self.handle_meson_version_from_ast()
except mparser.ParseException as me:
me.file = mesonfile
- # try to detect parser errors from new syntax added by future
- # meson versions, and just tell the user to update meson
- self.ast = me.ast
- self.handle_meson_version_from_ast()
+ if me.ast:
+ # try to detect parser errors from new syntax added by future
+ # meson versions, and just tell the user to update meson
+ self.ast = me.ast
+ self.handle_meson_version_from_ast()
raise me
def parse_project(self) -> None:
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py
index 28235b613..baccd2753 100644
--- a/mesonbuild/mparser.py
+++ b/mesonbuild/mparser.py
@@ -96,6 +96,10 @@ class Token(T.Generic[TV_TokenTypes]):
class Lexer:
def __init__(self, code: str):
+ if code.startswith(codecs.BOM_UTF8.decode('utf-8')):
+ line, *_ = code.split('\n', maxsplit=1)
+ raise ParseException('Builder file must be encoded in UTF-8 (with no BOM)', line, lineno=0, colno=0)
+
self.code = code
self.keywords = {'true', 'false', 'if', 'else', 'elif',
'endif', 'and', 'or', 'not', 'foreach', 'endforeach',
diff --git a/test cases/failing/130 utf8 with bom/meson.build b/test cases/failing/130 utf8 with bom/meson.build
new file mode 100644
index 000000000..492a0c691
--- /dev/null
+++ b/test cases/failing/130 utf8 with bom/meson.build
@@ -0,0 +1,3 @@
+project('utf8 with bom')
+
+subdir('subdir')
diff --git a/test cases/failing/130 utf8 with bom/test.json b/test cases/failing/130 utf8 with bom/test.json
new file mode 100644
index 000000000..2292bebe3
--- /dev/null
+++ b/test cases/failing/130 utf8 with bom/test.json
@@ -0,0 +1,8 @@
+{
+ "stdout": [
+ {
+ "line": "test cases/failing/130 utf8 with bom/meson.build:0:0: ERROR: Builder file must be encoded in UTF-8 (with no BOM)"
+ }
+ ]
+ }
+ \ No newline at end of file
diff --git a/test cases/failing/131 utf8 with bom subdir/meson.build b/test cases/failing/131 utf8 with bom subdir/meson.build
new file mode 100644
index 000000000..8d3bbd7e8
--- /dev/null
+++ b/test cases/failing/131 utf8 with bom subdir/meson.build
@@ -0,0 +1,3 @@
+project('utf8 with bom subdir')
+
+subdir('subdir')
diff --git a/test cases/failing/131 utf8 with bom subdir/subdir/meson.build b/test cases/failing/131 utf8 with bom subdir/subdir/meson.build
new file mode 100644
index 000000000..dbf2b9cc7
--- /dev/null
+++ b/test cases/failing/131 utf8 with bom subdir/subdir/meson.build
@@ -0,0 +1 @@
+a = 'Hello, World!'
diff --git a/test cases/failing/131 utf8 with bom subdir/test.json b/test cases/failing/131 utf8 with bom subdir/test.json
new file mode 100644
index 000000000..72cc5152d
--- /dev/null
+++ b/test cases/failing/131 utf8 with bom subdir/test.json
@@ -0,0 +1,8 @@
+{
+ "stdout": [
+ {
+ "line": "test cases/failing/131 utf8 with bom subdir/subdir/meson.build:0:0: ERROR: Builder file must be encoded in UTF-8 (with no BOM)"
+ }
+ ]
+ }
+ \ No newline at end of file
diff --git a/test cases/failing/132 utf8 with bom options/meson.build b/test cases/failing/132 utf8 with bom options/meson.build
new file mode 100644
index 000000000..50413e031
--- /dev/null
+++ b/test cases/failing/132 utf8 with bom options/meson.build
@@ -0,0 +1 @@
+project('utf8 with bom options')
diff --git a/test cases/failing/132 utf8 with bom options/meson.options b/test cases/failing/132 utf8 with bom options/meson.options
new file mode 100644
index 000000000..250c03279
--- /dev/null
+++ b/test cases/failing/132 utf8 with bom options/meson.options
@@ -0,0 +1 @@
+option('someoption', type : 'string', value : 'optval', description : 'An option')
diff --git a/test cases/failing/132 utf8 with bom options/test.json b/test cases/failing/132 utf8 with bom options/test.json
new file mode 100644
index 000000000..c09f48e6a
--- /dev/null
+++ b/test cases/failing/132 utf8 with bom options/test.json
@@ -0,0 +1,8 @@
+{
+ "stdout": [
+ {
+ "line": "test cases/failing/132 utf8 with bom options/meson.options:0:0: ERROR: Builder file must be encoded in UTF-8 (with no BOM)"
+ }
+ ]
+ }
+ \ No newline at end of file