Blame SOURCES/0026-libsepol-cil-Create-function-cil_add_decl_to_symtab-.patch

71cd55
From 0d4e568afe5a28edc5fcdcff8e925d4ec1d0d3d0 Mon Sep 17 00:00:00 2001
71cd55
From: James Carter <jwcart2@gmail.com>
71cd55
Date: Thu, 8 Apr 2021 13:32:19 -0400
71cd55
Subject: [PATCH] libsepol/cil: Create function cil_add_decl_to_symtab() and
71cd55
 refactor
71cd55
71cd55
The functionality of adding a declaration to a symbol table is also
71cd55
needed in __cil_copy_node_helper() and not just cil_gen_node().
71cd55
71cd55
Create a new function called cil_add_decl_to_symtab() to add a
71cd55
declaration to a symtab and refactor cil_gen_node() and
71cd55
__cil_copy_node_helper() to use the new function.
71cd55
71cd55
By using the new function, __cil_copy_node_helper() will now allow
71cd55
duplicate declarations when appropriate.
71cd55
71cd55
Signed-off-by: James Carter <jwcart2@gmail.com>
71cd55
---
71cd55
 libsepol/cil/src/cil_build_ast.c | 63 +++++++++++++++++++-------------
71cd55
 libsepol/cil/src/cil_build_ast.h |  2 +
71cd55
 libsepol/cil/src/cil_copy_ast.c  |  6 ++-
71cd55
 3 files changed, 45 insertions(+), 26 deletions(-)
71cd55
71cd55
diff --git a/libsepol/cil/src/cil_build_ast.c b/libsepol/cil/src/cil_build_ast.c
71cd55
index 14cdce1482a0..ec81db554b22 100644
71cd55
--- a/libsepol/cil/src/cil_build_ast.c
71cd55
+++ b/libsepol/cil/src/cil_build_ast.c
71cd55
@@ -102,11 +102,45 @@ static int cil_allow_multiple_decls(struct cil_db *db, enum cil_flavor f_new, en
71cd55
 	return CIL_FALSE;
71cd55
 }
71cd55
 
71cd55
+int cil_add_decl_to_symtab(struct cil_db *db, symtab_t *symtab, hashtab_key_t key, struct cil_symtab_datum *datum, struct cil_tree_node *node)
71cd55
+{
71cd55
+	int rc;
71cd55
+
71cd55
+	if (symtab == NULL || datum == NULL || node == NULL) {
71cd55
+		return SEPOL_ERR;
71cd55
+	}
71cd55
+
71cd55
+	rc = cil_symtab_insert(symtab, key, datum, node);
71cd55
+	if (rc == SEPOL_EEXIST) {
71cd55
+		struct cil_symtab_datum *prev;
71cd55
+		rc = cil_symtab_get_datum(symtab, key, &prev;;
71cd55
+		if (rc != SEPOL_OK) {
71cd55
+			cil_log(CIL_ERR, "Re-declaration of %s %s, but previous declaration could not be found\n",cil_node_to_string(node), key);
71cd55
+			return SEPOL_ERR;
71cd55
+		}
71cd55
+		if (!cil_allow_multiple_decls(db, node->flavor, FLAVOR(prev))) {
71cd55
+			/* multiple_decls not ok, ret error */
71cd55
+			struct cil_tree_node *n = NODE(prev);
71cd55
+			cil_log(CIL_ERR, "Re-declaration of %s %s\n",
71cd55
+				cil_node_to_string(node), key);
71cd55
+			cil_tree_log(node, CIL_ERR, "Previous declaration of %s",
71cd55
+				     cil_node_to_string(n));
71cd55
+			return SEPOL_ERR;
71cd55
+		}
71cd55
+		/* multiple_decls is enabled and works for this datum type, add node */
71cd55
+		cil_list_append(prev->nodes, CIL_NODE, node);
71cd55
+		node->data = prev;
71cd55
+		cil_symtab_datum_destroy(datum);
71cd55
+		free(datum);
71cd55
+	}
71cd55
+
71cd55
+	return SEPOL_OK;
71cd55
+}
71cd55
+
71cd55
 int cil_gen_node(struct cil_db *db, struct cil_tree_node *ast_node, struct cil_symtab_datum *datum, hashtab_key_t key, enum cil_sym_index sflavor, enum cil_flavor nflavor)
71cd55
 {
71cd55
 	int rc = SEPOL_ERR;
71cd55
 	symtab_t *symtab = NULL;
71cd55
-	struct cil_symtab_datum *prev;
71cd55
 
71cd55
 	rc = cil_verify_name((const char*)key, nflavor);
71cd55
 	if (rc != SEPOL_OK) {
71cd55
@@ -121,30 +155,9 @@ int cil_gen_node(struct cil_db *db, struct cil_tree_node *ast_node, struct cil_s
71cd55
 	ast_node->data = datum;
71cd55
 	ast_node->flavor = nflavor;
71cd55
 
71cd55
-	if (symtab != NULL) {
71cd55
-		rc = cil_symtab_insert(symtab, (hashtab_key_t)key, datum, ast_node);
71cd55
-		if (rc == SEPOL_EEXIST) {
71cd55
-			rc = cil_symtab_get_datum(symtab, (hashtab_key_t)key, &prev;;
71cd55
-			if (rc != SEPOL_OK) {
71cd55
-				cil_log(CIL_ERR, "Re-declaration of %s %s, but previous declaration could not be found\n",cil_node_to_string(ast_node), key);
71cd55
-				goto exit;
71cd55
-			}
71cd55
-			if (!cil_allow_multiple_decls(db, nflavor, FLAVOR(prev))) {
71cd55
-				/* multiple_decls not ok, ret error */
71cd55
-				struct cil_tree_node *node = NODE(prev);
71cd55
-				cil_log(CIL_ERR, "Re-declaration of %s %s\n",
71cd55
-					cil_node_to_string(ast_node), key);
71cd55
-				cil_tree_log(node, CIL_ERR, "Previous declaration of %s",
71cd55
-					cil_node_to_string(node));
71cd55
-				rc = SEPOL_ERR;
71cd55
-				goto exit;
71cd55
-			}
71cd55
-			/* multiple_decls is enabled and works for this datum type, add node */
71cd55
-			cil_list_append(prev->nodes, CIL_NODE, ast_node);
71cd55
-			ast_node->data = prev;
71cd55
-			cil_symtab_datum_destroy(datum);
71cd55
-			free(datum);
71cd55
-		}
71cd55
+	rc = cil_add_decl_to_symtab(db, symtab, key, datum, ast_node);
71cd55
+	if (rc != SEPOL_OK) {
71cd55
+		goto exit;
71cd55
 	}
71cd55
 
71cd55
 	if (ast_node->parent->flavor == CIL_MACRO) {
71cd55
diff --git a/libsepol/cil/src/cil_build_ast.h b/libsepol/cil/src/cil_build_ast.h
71cd55
index 8153e51e3a97..fd9053ce55ca 100644
71cd55
--- a/libsepol/cil/src/cil_build_ast.h
71cd55
+++ b/libsepol/cil/src/cil_build_ast.h
71cd55
@@ -37,6 +37,8 @@
71cd55
 #include "cil_tree.h"
71cd55
 #include "cil_list.h"
71cd55
 
71cd55
+int cil_add_decl_to_symtab(struct cil_db *db, symtab_t *symtab, hashtab_key_t key, struct cil_symtab_datum *datum, struct cil_tree_node *node);
71cd55
+
71cd55
 int cil_gen_node(struct cil_db *db, struct cil_tree_node *ast_node, struct cil_symtab_datum *datum, hashtab_key_t key, enum cil_sym_index sflavor, enum cil_flavor nflavor);
71cd55
 int cil_parse_to_list(struct cil_tree_node *parse_cl_head, struct cil_list *ast_cl, enum cil_flavor flavor);
71cd55
 
71cd55
diff --git a/libsepol/cil/src/cil_copy_ast.c b/libsepol/cil/src/cil_copy_ast.c
71cd55
index ed96786115d3..12bc553c6594 100644
71cd55
--- a/libsepol/cil/src/cil_copy_ast.c
71cd55
+++ b/libsepol/cil/src/cil_copy_ast.c
71cd55
@@ -2031,7 +2031,11 @@ int __cil_copy_node_helper(struct cil_tree_node *orig, __attribute__((unused)) u
71cd55
 				rc = SEPOL_ERR;
71cd55
 				goto exit;
71cd55
 			}
71cd55
-			rc = cil_symtab_insert(symtab, ((struct cil_symtab_datum*)orig->data)->name, ((struct cil_symtab_datum*)data), new);
71cd55
+
71cd55
+			rc = cil_add_decl_to_symtab(db, symtab, DATUM(orig->data)->name, DATUM(data), new);
71cd55
+			if (rc != SEPOL_OK) {
71cd55
+				goto exit;
71cd55
+			}
71cd55
 
71cd55
 			namespace = new;
71cd55
 			while (namespace->flavor != CIL_MACRO && namespace->flavor != CIL_BLOCK && namespace->flavor != CIL_ROOT) {
71cd55
-- 
71cd55
2.32.0
71cd55