Repository: mesonbuild/meson

Issue:
Spurious sandbox violations from name clash with subprojects dir
The check to determine whether a sandbox violation has occurred appears to be overly zealous: it flags as a violation any file path that contains the `subproject_dir` string, even if it's a completely unrelated directory. This breaks my project in 0.43.

I will be submitting a pull request shortly with a test case that demonstrates the problem (done: #2482).


Discussion:

Resolving patch:
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -2799,6 +2799,16 @@ def run(self):
         super().run()
         mlog.log('Build targets in project:', mlog.bold(str(len(self.build.targets))))
 
+    def evaluate_subproject_info(self, path_from_source_root, subproject_dirname):
+        depth = 0
+        subproj_name = ''
+        segs = path_from_source_root.split(os.path.sep)
+        while segs and segs[0] == subproject_dirname:
+            depth += 1
+            subproj_name = segs[1]
+            segs = segs[2:]
+        return (depth, subproj_name)
+
     # Check that the indicated file is within the same subproject
     # as we currently are. This is to stop people doing
     # nasty things like:
@@ -2820,17 +2830,16 @@ def validate_within_subproject(self, subdir, fname):
                 return
             norm = os.path.relpath(norm, self.environment.source_dir)
             assert(not os.path.isabs(norm))
-        segments = norm.split(os.path.sep)
-        num_sps = segments.count(self.subproject_dir)
+        (num_sps, sproj_name) = self.evaluate_subproject_info(norm, self.subproject_dir)
+        plain_filename = os.path.split(norm)[-1]
         if num_sps == 0:
             if self.subproject == '':
                 return
-            raise InterpreterException('Sandbox violation: Tried to grab file %s from a different subproject.' % segments[-1])
+            raise InterpreterException('Sandbox violation: Tried to grab file %s from a different subproject.' % plain_filename)
         if num_sps > 1:
-            raise InterpreterException('Sandbox violation: Tried to grab file %s from a nested subproject.' % segments[-1])
-        sproj_name = segments[segments.index(self.subproject_dir) + 1]
+            raise InterpreterException('Sandbox violation: Tried to grab file %s from a nested subproject.' % plain_filename)
         if sproj_name != self.subproject_directory_name:
-            raise InterpreterException('Sandbox violation: Tried to grab file %s from a different subproject.' % segments[-1])
+            raise InterpreterException('Sandbox violation: Tried to grab file %s from a different subproject.' % plain_filename)
 
     def source_strings_to_files(self, sources):
         results = []


Test patch:
diff --git a/test cases/common/163 subproject dir name collision/a.c b/test cases/common/163 subproject dir name collision/a.c
new file mode 100644
--- /dev/null
+++ b/test cases/common/163 subproject dir name collision/a.c	
@@ -0,0 +1,13 @@
+#include<assert.h>
+char func_b();
+char func_c();
+
+int main(int argc, char **argv) {
+    if(func_b() != 'b') {
+        return 1;
+    }
+    if(func_c() != 'c') {
+        return 2;
+    }
+    return 0;
+}
diff --git a/test cases/common/163 subproject dir name collision/custom_subproject_dir/B/b.c b/test cases/common/163 subproject dir name collision/custom_subproject_dir/B/b.c
new file mode 100644
--- /dev/null
+++ b/test cases/common/163 subproject dir name collision/custom_subproject_dir/B/b.c	
@@ -0,0 +1,20 @@
+#include<stdlib.h>
+char func_c();
+
+#if defined _WIN32 || defined __CYGWIN__
+#define DLL_PUBLIC __declspec(dllexport)
+#else
+  #if defined __GNUC__
+    #define DLL_PUBLIC __attribute__ ((visibility("default")))
+  #else
+    #pragma message ("Compiler does not support symbol visibility.")
+    #define DLL_PUBLIC
+  #endif
+#endif
+
+char DLL_PUBLIC func_b() {
+    if(func_c() != 'c') {
+        exit(3);
+    }
+    return 'b';
+}
diff --git a/test cases/common/163 subproject dir name collision/custom_subproject_dir/B/meson.build b/test cases/common/163 subproject dir name collision/custom_subproject_dir/B/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/common/163 subproject dir name collision/custom_subproject_dir/B/meson.build	
@@ -0,0 +1,4 @@
+project('B', 'c')
+C = subproject('C')
+c = C.get_variable('c')
+b = shared_library('b', 'b.c', link_with : c)
diff --git a/test cases/common/163 subproject dir name collision/custom_subproject_dir/C/c.c b/test cases/common/163 subproject dir name collision/custom_subproject_dir/C/c.c
new file mode 100644
--- /dev/null
+++ b/test cases/common/163 subproject dir name collision/custom_subproject_dir/C/c.c	
@@ -0,0 +1,14 @@
+#if defined _WIN32 || defined __CYGWIN__
+#define DLL_PUBLIC __declspec(dllexport)
+#else
+  #if defined __GNUC__
+    #define DLL_PUBLIC __attribute__ ((visibility("default")))
+  #else
+    #pragma message ("Compiler does not support symbol visibility.")
+    #define DLL_PUBLIC
+  #endif
+#endif
+
+char DLL_PUBLIC func_c() {
+    return 'c';
+}
diff --git a/test cases/common/163 subproject dir name collision/custom_subproject_dir/C/meson.build b/test cases/common/163 subproject dir name collision/custom_subproject_dir/C/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/common/163 subproject dir name collision/custom_subproject_dir/C/meson.build	
@@ -0,0 +1,2 @@
+project('C', 'c')
+c = shared_library('c', 'c.c')
diff --git a/test cases/common/163 subproject dir name collision/meson.build b/test cases/common/163 subproject dir name collision/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/common/163 subproject dir name collision/meson.build	
@@ -0,0 +1,12 @@
+project('A', 'c', subproject_dir:'custom_subproject_dir')
+
+B = subproject('B')
+b = B.get_variable('b')
+
+C = subproject('C')
+c = C.get_variable('c')
+
+subdir('other_subdir')
+
+a = executable('a', 'a.c', link_with : [b, c])
+test('a test', a)
diff --git a/test cases/common/163 subproject dir name collision/other_subdir/custom_subproject_dir/other.c b/test cases/common/163 subproject dir name collision/other_subdir/custom_subproject_dir/other.c
new file mode 100644
--- /dev/null
+++ b/test cases/common/163 subproject dir name collision/other_subdir/custom_subproject_dir/other.c	
@@ -0,0 +1,19 @@
+#include<stdlib.h>
+
+#if defined _WIN32 || defined __CYGWIN__
+#define DLL_PUBLIC __declspec(dllexport)
+#else
+  #if defined __GNUC__
+    #define DLL_PUBLIC __attribute__ ((visibility("default")))
+  #else
+    #pragma message ("Compiler does not support symbol visibility.")
+    #define DLL_PUBLIC
+  #endif
+#endif
+
+char DLL_PUBLIC func_b() {
+    if('c' != 'c') {
+        exit(3);
+    }
+    return 'b';
+}
diff --git a/test cases/common/163 subproject dir name collision/other_subdir/meson.build b/test cases/common/163 subproject dir name collision/other_subdir/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/common/163 subproject dir name collision/other_subdir/meson.build	
@@ -0,0 +1 @@
+other = shared_library('other', 'custom_subproject_dir/other.c')