aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/mds-kbdc/process-includes.c51
-rw-r--r--test-files/mds-kbdc/process-includes/invalid/_inclusion_circle1
-rw-r--r--test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_12
-rw-r--r--test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_22
-rw-r--r--test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_32
-rw-r--r--test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_42
-rw-r--r--test-files/mds-kbdc/process-includes/invalid/inclusion_circle22
-rw-r--r--test-files/mds-kbdc/process-includes/invalid/inclusion_rho43
-rw-r--r--test-files/mds-kbdc/process-includes/invalid/selfinclusion15
9 files changed, 138 insertions, 2 deletions
diff --git a/src/mds-kbdc/process-includes.c b/src/mds-kbdc/process-includes.c
index 8c4bbbf..64dd7eb 100644
--- a/src/mds-kbdc/process-includes.c
+++ b/src/mds-kbdc/process-includes.c
@@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "process-includes.h"
-/* TODO we need to deal with mutually recursive includes */
#include "make-tree.h"
#include "simplify-tree.h"
@@ -25,6 +24,8 @@
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
@@ -57,6 +58,21 @@ static mds_kbdc_parse_error_t* error;
*/
static mds_kbdc_parsed_t* restrict result;
+/**
+ * Stack of attributes of already included files
+ */
+static struct stat* included;
+
+/**
+ * The number elements allocated for `included`
+ */
+static size_t included_size = 0;
+
+/**
+ * The number elements stored in `included`
+ */
+static size_t included_ptr = 0;
+
/**
@@ -256,8 +272,39 @@ static int process_includes_in_tree(mds_kbdc_tree_t* restrict tree)
*/
int process_includes(mds_kbdc_parsed_t* restrict result_)
{
+ int r, saved_errno;
+ struct stat attr;
+ size_t i;
+
result = result_;
- return process_includes_in_tree(result_->tree);
+
+ fail_if (stat(result->pathname, &attr));
+
+ if (included_ptr == included_size)
+ {
+ struct stat* old;
+ if (xxrealloc(old, included, included_size += 4, struct stat))
+ return included = old, -1;
+ }
+
+ for (i = 0; i < included_ptr; i++)
+ if ((included[i].st_dev == attr.st_dev) && (included[i].st_ino == attr.st_ino))
+ {
+ NEW_ERROR_(result, ERROR, 0, 0, 0, 0, 1, "resursive inclusion");
+ return 0;
+ }
+
+ included[included_ptr++] = attr;
+
+ r = process_includes_in_tree(result_->tree);
+ saved_errno = errno;
+
+ if (--included_ptr == 0)
+ free(included), included_size = 0;
+
+ return errno = saved_errno, r;
+ pfail:
+ return -1;
}
diff --git a/test-files/mds-kbdc/process-includes/invalid/_inclusion_circle b/test-files/mds-kbdc/process-includes/invalid/_inclusion_circle
new file mode 100644
index 0000000..56d1212
--- /dev/null
+++ b/test-files/mds-kbdc/process-includes/invalid/_inclusion_circle
@@ -0,0 +1 @@
+include "inclusion_circle"
diff --git a/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_1 b/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_1
new file mode 100644
index 0000000..49647dd
--- /dev/null
+++ b/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_1
@@ -0,0 +1,2 @@
+include "_inclusion_rho_2"
+
diff --git a/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_2 b/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_2
new file mode 100644
index 0000000..b7847a6
--- /dev/null
+++ b/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_2
@@ -0,0 +1,2 @@
+include "_inclusion_rho_3"
+
diff --git a/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_3 b/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_3
new file mode 100644
index 0000000..8cdf4df
--- /dev/null
+++ b/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_3
@@ -0,0 +1,2 @@
+include "_inclusion_rho_4"
+
diff --git a/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_4 b/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_4
new file mode 100644
index 0000000..49647dd
--- /dev/null
+++ b/test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_4
@@ -0,0 +1,2 @@
+include "_inclusion_rho_2"
+
diff --git a/test-files/mds-kbdc/process-includes/invalid/inclusion_circle b/test-files/mds-kbdc/process-includes/invalid/inclusion_circle
new file mode 100644
index 0000000..11214e7
--- /dev/null
+++ b/test-files/mds-kbdc/process-includes/invalid/inclusion_circle
@@ -0,0 +1,22 @@
+include "_inclusion_circle"
+
+# (include (@ 1 0-7) ‘.../test-files/mds-kbdc/process-includes/invalid/_inclusion_circle’
+# (.inner
+# (include (@ 1 0-7) ‘.../test-files/mds-kbdc/process-includes/invalid/inclusion_circle’
+# (.inner
+# (include (@ 1 0-7) ‘"_inclusion_circle"’
+# (.inner nil)
+# )
+# )
+# )
+# )
+# )
+# .../test-files/mds-kbdc/process-includes/invalid/inclusion_circle: error: resursive inclusion
+#
+# .../test-files/mds-kbdc/process-includes/invalid/inclusion_circle:1:0–7: note: included from here
+# include "_inclusion_circle"
+# ^^^^^^^
+# .../test-files/mds-kbdc/process-includes/invalid/_inclusion_circle:1:0–7: note: included from here
+# include "inclusion_circle"
+# ^^^^^^^
+
diff --git a/test-files/mds-kbdc/process-includes/invalid/inclusion_rho b/test-files/mds-kbdc/process-includes/invalid/inclusion_rho
new file mode 100644
index 0000000..43aabc9
--- /dev/null
+++ b/test-files/mds-kbdc/process-includes/invalid/inclusion_rho
@@ -0,0 +1,43 @@
+include "_inclusion_rho_1"
+
+# (include (@ 1 0-7) ‘.../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_1’
+# (.inner
+# (include (@ 1 0-7) ‘.../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_2’
+# (.inner
+# (include (@ 1 0-7) ‘.../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_3’
+# (.inner
+# (include (@ 1 0-7) ‘.../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_4’
+# (.inner
+# (include (@ 1 0-7) ‘.../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_2’
+# (.inner
+# (include (@ 1 0-7) ‘"_inclusion_rho_3"’
+# (.inner nil)
+# )
+# )
+# )
+# )
+# )
+# )
+# )
+# )
+# )
+# )
+# )
+# .../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_2: error: resursive inclusion
+#
+# .../test-files/mds-kbdc/process-includes/invalid/inclusion_rho:1:0–7: note: included from here
+# include "_inclusion_rho_1"
+# ^^^^^^^
+# .../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_1:1:0–7: note: included from here
+# include "_inclusion_rho_2"
+# ^^^^^^^
+# .../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_2:1:0–7: note: included from here
+# include "_inclusion_rho_3"
+# ^^^^^^^
+# .../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_3:1:0–7: note: included from here
+# include "_inclusion_rho_4"
+# ^^^^^^^
+# .../test-files/mds-kbdc/process-includes/invalid/_inclusion_rho_4:1:0–7: note: included from here
+# include "_inclusion_rho_2"
+# ^^^^^^^
+
diff --git a/test-files/mds-kbdc/process-includes/invalid/selfinclusion b/test-files/mds-kbdc/process-includes/invalid/selfinclusion
new file mode 100644
index 0000000..5207dbd
--- /dev/null
+++ b/test-files/mds-kbdc/process-includes/invalid/selfinclusion
@@ -0,0 +1,15 @@
+include "selfinclusion"
+
+# (include (@ 1 0-7) ‘.../mds/test-files/mds-kbdc/process-includes/invalid/selfinclusion’
+# (.inner
+# (include (@ 1 0-7) ‘"selfinclusion"’
+# (.inner nil)
+# )
+# )
+# )
+# .../test-files/mds-kbdc/process-includes/invalid/selfinclusion: error: resursive inclusion
+#
+# .../test-files/mds-kbdc/process-includes/invalid/selfinclusion:1:0–7: note: included from here
+# include "selfinclusion"
+# ^^^^^^^
+