Skip to contentSkip to conversation

libsepol: fix -Wwrite-strings warnings

PATCH In review Nicolas Iooss 2 participants 10 messages Feb 5, 2017 – Feb 6, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/8 libsepol: fix -Wwrite-strings warnings

When compiling with -Wwrite-strings, clang reports some warnings like:

module_to_cil.c:784:13: error: assigning to 'char *' from 'const char [5]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
statement = "type";
^ ~~~~~~
module_to_cil.c:787:13: error: assigning to 'char *' from 'const char [5]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
statement = "role";
^ ~~~~~~

Add a const type attribute to local variables which only handle constant strings.

---
 libsepol/cil/src/cil_binary.c |  4 ++--
 libsepol/cil/src/cil_policy.c | 18 +++++++++---------
 libsepol/src/module_to_cil.c  |  2 +-
 3 files changed, 12 insertions(+), 12 deletions(-)
libsepol/cil/src/cil_binary.c +2 -2
index 19cbc1b87c1e..ac371aef7b2d 100644
@@ -1923,7 +1923,7 @@ static void __cil_expr_to_string(struct cil_list *expr, enum cil_flavor flavor,
 				cil_asprintf(out, "%s %s", CIL_KEY_NOT, s1);
 				free(s1);
 			} else {
-				char *opstr = "";
+				const char *opstr = "";
 
 				__cil_expr_to_string_helper(curr->next->next, flavor, &s2);
 
@@ -4376,7 +4376,7 @@ static void __cil_print_classperm(struct cil_list *cp_list)
 
 static void __cil_print_permissionx(struct cil_permissionx *px)
 {
-	char *kind_str = "";
+	const char *kind_str = "";
 	char *expr_str;
 
 	switch (px->kind) {
libsepol/cil/src/cil_policy.c +9 -9
index bb832f250db7..2e6814a60eae 100644
@@ -183,7 +183,7 @@ static void cil_gather_statements(struct cil_tree_node *start, struct cil_list *
 	cil_tree_walk(start, __cil_gather_statements_helper, NULL, NULL, lists);
 }
 
-static void cil_simple_rules_to_policy(FILE *out, struct cil_list *rules, char *kind)
+static void cil_simple_rules_to_policy(FILE *out, struct cil_list *rules, const char *kind)
 {
 	struct cil_list_item *i1;
 
@@ -194,7 +194,7 @@ static void cil_simple_rules_to_policy(FILE *out, struct cil_list *rules, char *
 
 static void cil_cats_to_policy(FILE *out, struct cil_cats *cats)
 {
-	char *lead = "";
+	const char *lead = "";
 	struct cil_cat *first = NULL, *last = NULL, *cat;
 	struct cil_list_item *i1;
 
@@ -471,7 +471,7 @@ static char *__cil_cons_leaf_operand_to_string(struct cil_db *db, struct cil_lis
 {
 	struct cil_list_item *i1;
 	enum cil_flavor flavor = operand->flavor;
-	char *o_str;
+	const char *o_str;
 	size_t o_len;
 
 	if (flavor == CIL_CONS_OPERAND) {
@@ -559,7 +559,7 @@ static char *__cil_cons_leaf_operand_to_string(struct cil_db *db, struct cil_lis
 static char *__cil_cons_leaf_op_to_string(struct cil_list_item *op, char *new)
 {
 	enum cil_flavor flavor = (enum cil_flavor)op->data;
-	char *op_str;
+	const char *op_str;
 	size_t len;
 
 	switch (flavor) {
@@ -1009,7 +1009,7 @@ static void cil_bools_to_policy(FILE *out, struct cil_list *bools)
 {
 	struct cil_list_item *i1;
 	struct cil_bool *bool;
-	char *value;
+	const char *value;
 
 	cil_list_for_each(i1, bools) {
 		bool = i1->data;
@@ -1108,7 +1108,7 @@ static void cil_xperms_to_policy(FILE *out, struct cil_permissionx *permx)
 	ebitmap_node_t *node;
 	unsigned int i, first = 0, last = 0;
 	int need_first = CIL_TRUE, need_last = CIL_TRUE;
-	char *kind;
+	const char *kind;
 
 	if (permx->kind == CIL_PERMX_KIND_IOCTL) {
 		kind = "ioctl";
@@ -1156,7 +1156,7 @@ static void cil_xperms_to_policy(FILE *out, struct cil_permissionx *permx)
 
 static void cil_av_rulex_to_policy(FILE *out, struct cil_avrule *rule)
 {
-	char *kind;
+	const char *kind;
 	struct cil_symtab_datum *src, *tgt;
 
 	src = rule->src;
@@ -1187,7 +1187,7 @@ static void cil_av_rulex_to_policy(FILE *out, struct cil_avrule *rule)
 
 static void cil_av_rule_to_policy(FILE *out, struct cil_avrule *rule)
 {
-	char *kind;
+	const char *kind;
 	struct cil_symtab_datum *src, *tgt;
 	struct cil_list *classperms_strs;
 	struct cil_list_item *i1;
@@ -1225,7 +1225,7 @@ static void cil_av_rule_to_policy(FILE *out, struct cil_avrule *rule)
 
 static void cil_type_rule_to_policy(FILE *out, struct cil_type_rule *rule)
 {
-	char *kind;
+	const char *kind;
 	struct cil_symtab_datum *src, *tgt, *res;
 	struct cil_list *class_list;
 	struct cil_list_item *i1;
libsepol/src/module_to_cil.c +1 -1
index 2acb600dddae..8c4fff9c3d9f 100644
@@ -775,7 +775,7 @@ static int cil_print_attr_strs(int indent, struct policydb *pdb, int is_type, st
 	int rc = 0;
 	struct ebitmap_node *node;
 	unsigned int i;
-	char *statement;
+	const char *statement;
 	int has_positive = pos && (ebitmap_cardinality(pos) > 0);
 	int has_negative = neg && (ebitmap_cardinality(neg) > 0);
 	char **val_to_name;
Signature · 1 line
2.11.0
#1 lore raw

PATCH 2/8 libsemanage: make lang_ext parameter const in semanage_direct_write_langext()

When building with "clang -Wwrite-strings", the compiler reports the following warnings:

    direct_api.c:1030:46: error: passing 'const char [4]' to parameter
    of type 'char *' discards qualifiers
    [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
                    status = semanage_direct_write_langext(sh, "cil", modinfo);
                                                               ^~~~~
    direct_api.c:898:11: note: passing argument to parameter 'lang_ext'
    here
                                    char *lang_ext,
                                          ^
    direct_api.c:1030:46: error: passing 'const char [4]' to parameter
    of type 'char *' discards qualifiers
    [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
                    status = semanage_direct_write_langext(sh, "cil", modinfo);
                                                               ^~~~~
    direct_api.c:898:11: note: passing argument to parameter 'lang_ext'
    here
                                    char *lang_ext,
                                          ^
---
 libsemanage/src/direct_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsemanage/src/direct_api.c +1 -1
index 742c54f037c7..c23494bb4270 100644
@@ -895,7 +895,7 @@ cleanup:
 }
 
 static int semanage_direct_write_langext(semanage_handle_t *sh,
-				char *lang_ext,
+				const char *lang_ext,
 				const semanage_module_info_t *modinfo)
 {
 	int ret = -1;
Signature · 1 line
2.11.0
#2 lore raw

PATCH 3/8 policycoreutils/semodule: hide -Wwrite-strings warnings

In reply to Nicolas Iooss

When building with "clang -Wwrite-strings", the compiler complains about initializing a char* array (variable genhomedirconargv) with literal strings (which are in read-only memory).

However the programs needs to use a non-const char* array in order to fake arguments of getopt_long() (called by parse_command_line()). Silent the compiler warnings by introducing casts to char*.

---
 policycoreutils/semodule/semodule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
policycoreutils/semodule/semodule.c +1 -1
index ce048bc061b9..36686d0230a8 100644
@@ -341,7 +341,7 @@ int main(int argc, char *argv[])
 	int i, commit = 0;
 	int result;
 	int status = EXIT_FAILURE;
-	char *genhomedirconargv[] = { "genhomedircon", "-B", "-n" };
+	char *genhomedirconargv[] = { (char *)"genhomedircon", (char *)"-B", (char *)"-n" };
 	create_signal_handlers();
 	if (strcmp(basename(argv[0]), "genhomedircon") == 0) {
 		argc = 3;
Signature · 1 line
2.11.0
#3 lore raw

Re: [PATCH 3/8] policycoreutils/semodule: hide -Wwrite-strings warnings

Quoted 19 lines · Nicolas Iooss

When building with "clang -Wwrite-strings", the compiler complains about initializing a char* array (variable genhomedirconargv) with literal strings (which are in read-only memory).

However the programs needs to use a non-const char* array in order to fake arguments of getopt_long() (called by parse_command_line()). Silent the compiler warnings by introducing casts to char*.

---
 policycoreutils/semodule/semodule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
policycoreutils/semodule/semodule.c +0 -0
index ce048bc061b9..36686d0230a8 100644

policycoreutils/semodule/semodule.c int main(int argc, char *argv[]) · line 347

Quoted 8 lines
policycoreutils/semodule/semodule.c +1 -1
@@ -341,7 +341,7 @@ int main(int argc, char *argv[])
 	int i, commit = 0;
 	int result;
 	int status = EXIT_FAILURE;
-	char *genhomedirconargv[] = { "genhomedircon", "-B", "-n" };
+	char *genhomedirconargv[] = { (char *)"genhomedircon", (char *)"-B", (char *)"-n" };
 	create_signal_handlers();
 	if (strcmp(basename(argv[0]), "genhomedircon") == 0) {
 		argc = 3;

I think that I would prefer the following for this one:

policycoreutils/semodule/semodule.c +2 -2
index ce048bc..c63a864 100644
@@ -341,11 +341,11 @@ int main(int argc, char *argv[])
 	int i, commit = 0;
 	int result;
 	int status = EXIT_FAILURE;
-	char *genhomedirconargv[] = { "genhomedircon", "-B", "-n" };
+	const char *genhomedirconargv[] = { "genhomedircon", "-B", "-n" };
 	create_signal_handlers();
 	if (strcmp(basename(argv[0]), "genhomedircon") == 0) {
 		argc = 3;
-		argv=genhomedirconargv;
+		argv = (char **)genhomedirconargv;
 	}
 	parse_command_line(argc, argv);
 
Signature · 2 lines
James Carter <[email protected]>
National Security Agency
#4 lore raw

PATCH 4/8 policycoreutils/hll/pp: fix -Wwrite-strings warnings

In reply to Nicolas Iooss

In main(), variable ifile can be made const char*. While at it, make ofile const char* too.

---
 policycoreutils/hll/pp/pp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
policycoreutils/hll/pp/pp.c +2 -2
index 7f9afb01dc23..e4fa4c4b467c 100644
@@ -69,8 +69,8 @@ int main(int argc, char **argv)
 		{ NULL, 0, NULL, 0 }
 	};
 	struct sepol_module_package *mod_pkg = NULL;
-	char *ifile = NULL;
-	char *ofile = NULL;
+	const char *ifile = NULL;
+	const char *ofile = NULL;
 	FILE *in = NULL;
 	FILE *out = NULL;
 	int outfd = -1;
Signature · 1 line
2.11.0
#5 lore raw

PATCH 5/8 mcstrans: fix -Wwrite-strings warnings

In reply to Nicolas Iooss
---
 mcstrans/src/mcscolor.c  | 2 +-
 mcstrans/src/mcstransd.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mcstrans/src/mcscolor.c +1 -1
index 558c2a0efc4b..e713da9aea1f 100644
@@ -38,7 +38,7 @@ typedef struct setab {
 #define N_COLOR		4
 
 #define AUX_RULE_COLOR "color"
-static char *rules[] = { "user", "role", "type", "range" };
+static const char *rules[] = { "user", "role", "type", "range" };
 
 static setab_t *clist[N_COLOR];
 static setab_t *cend[N_COLOR];
mcstrans/src/mcstransd.c +1 -1
index a5ed36181fc6..d7fc5dea3dd3 100644
@@ -90,7 +90,7 @@ send_response(int fd, uint32_t function, char *data, int32_t ret_val)
 	ssize_t count;
 
 	if (!data)
-		data = "";
+		data = (char *)"";
 
 	data_size = strlen(data) + 1;
 
Signature · 1 line
2.11.0
#6 lore raw

PATCH 6/8 semodule_deps: hide -Wwrite-strings warnings

In reply to Nicolas Iooss

generate_requires() may use a non-freable strings, BASE_NAME, as key in a hashtable. The compiler complains about this small abuse of hashtab_insert() interface (because its argument key is char* without "const"). Nevertheless this is all right because the hashtab keys are never freed directly (this behavior is documented in a comment in free_requires()).

Therefore, hide the compiler warning by casting BASE_NAME to char*.

---
 semodule-utils/semodule_deps/semodule_deps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
semodule-utils/semodule_deps/semodule_deps.c +1 -1
index ab99d159f788..8048ee02d3ca 100644
@@ -40,7 +40,7 @@ extern int optind;
  * thing is done in the linker for displaying error
  * messages.
  */
-#define BASE_NAME "BASE"
+#define BASE_NAME ((char *)"BASE")
 
 static void usage(char *program_name)
 {
Signature · 1 line
2.11.0
#7 lore raw

PATCH 7/8 libsepol/tests: fix -Wwrite-strings warnings

In reply to Nicolas Iooss

Since fd9e5ef7b78b ("libsepol: use constant keys in hashtab functions") it is possible to call hashtab_search() with a const char* key value. Doing so fixes compiler warnings about non-const char* string literals (-Wwrite-strings flag).

---
 libsepol/tests/helpers.c                |  2 +-
 libsepol/tests/helpers.h                |  2 +-
 libsepol/tests/test-common.c            |  8 ++++----
 libsepol/tests/test-common.h            |  8 ++++----
 libsepol/tests/test-deps.c              |  4 ++--
 libsepol/tests/test-expander-attr-map.c | 34 ++++++++++++++++-----------------
 libsepol/tests/test-expander-roles.c    |  2 +-
 libsepol/tests/test-expander-users.c    |  4 ++--
 libsepol/tests/test-expander.c          | 14 +++++++-------
 libsepol/tests/test-linker-cond-map.c   |  4 ++--
 libsepol/tests/test-linker-roles.c      |  4 ++--
 libsepol/tests/test-linker-types.c      |  6 +++---
 12 files changed, 46 insertions(+), 46 deletions(-)
libsepol/tests/helpers.c +1 -1
index 542e46743aa0..1192734b971f 100644
@@ -63,7 +63,7 @@ int test_load_policy(policydb_t * p, int policy_type, int mls, const char *test_
 	return 0;
 }
 
-avrule_decl_t *test_find_decl_by_sym(policydb_t * p, int symtab, char *sym)
+avrule_decl_t *test_find_decl_by_sym(policydb_t * p, int symtab, const char *sym)
 {
 	scope_datum_t *scope = (scope_datum_t *) hashtab_search(p->scope[symtab].table, sym);
 
libsepol/tests/helpers.h +1 -1
index 418ee9541959..10d390946499 100644
@@ -54,6 +54,6 @@ extern int test_load_policy(policydb_t * p, int policy_type, int mls, const char
  *  decl      success 
  *  NULL      error (including more than one declaration)
  */
-extern avrule_decl_t *test_find_decl_by_sym(policydb_t * p, int symtab, char *sym);
+extern avrule_decl_t *test_find_decl_by_sym(policydb_t * p, int symtab, const char *sym);
 
 #endif
libsepol/tests/test-common.c +4 -4
index dae47df3dd01..81074031844d 100644
@@ -26,7 +26,7 @@
 
 #include <CUnit/Basic.h>
 
-void test_sym_presence(policydb_t * p, char *id, int sym_type, unsigned int scope_type, unsigned int *decls, unsigned int len)
+void test_sym_presence(policydb_t * p, const char *id, int sym_type, unsigned int scope_type, unsigned int *decls, unsigned int len)
 {
 	scope_datum_t *scope;
 	int found;
@@ -147,7 +147,7 @@ void test_policydb_indexes(policydb_t * p)
 	}
 }
 
-void test_alias_datum(policydb_t * p, char *id, char *primary_id, char mode, unsigned int flavor)
+void test_alias_datum(policydb_t * p, const char *id, const char *primary_id, char mode, unsigned int flavor)
 {
 	type_datum_t *type, *primary;
 	unsigned int my_primary, my_flavor, my_value;
@@ -181,7 +181,7 @@ void test_alias_datum(policydb_t * p, char *id, char *primary_id, char mode, uns
 	}
 }
 
-role_datum_t *test_role_type_set(policydb_t * p, char *id, avrule_decl_t * decl, char **types, unsigned int len, unsigned int flags)
+role_datum_t *test_role_type_set(policydb_t * p, const char *id, avrule_decl_t * decl, const char **types, unsigned int len, unsigned int flags)
 {
 	ebitmap_node_t *tnode;
 	unsigned int i, j, new, found = 0;
@@ -222,7 +222,7 @@ role_datum_t *test_role_type_set(policydb_t * p, char *id, avrule_decl_t * decl,
 	return role;
 }
 
-void test_attr_types(policydb_t * p, char *id, avrule_decl_t * decl, char **types, int len)
+void test_attr_types(policydb_t * p, const char *id, avrule_decl_t * decl, const char **types, int len)
 {
 	ebitmap_node_t *tnode;
 	int j, new, found = 0;
libsepol/tests/test-common.h +4 -4
index 5a1e65070b54..1e31c8d1f80c 100644
@@ -34,7 +34,7 @@
  * This is a utility function to test for the symbol's presence in the global symbol table, 
  * the scope table, and that the decl blocks we think this symbol is in are correct
  */
-extern void test_sym_presence(policydb_t * p, char *id, int sym_type, unsigned int scope_type, unsigned int *decls, unsigned int len);
+extern void test_sym_presence(policydb_t * p, const char *id, int sym_type, unsigned int scope_type, unsigned int *decls, unsigned int len);
 
 /* Test the indexes in the policydb to ensure their correctness. These include
  * the sym_val_to_name[], class_val_to_struct, role_val_to_struct, type_val_to_struct,
@@ -50,7 +50,7 @@ extern void test_policydb_indexes(policydb_t * p);
          1 = automatically detect the flavor value and test the datum accordingly
  * flavor = flavor value if in mode 0
  */
-extern void test_alias_datum(policydb_t * p, char *id, char *primary_id, char mode, unsigned int flavor);
+extern void test_alias_datum(policydb_t * p, const char *id, const char *primary_id, char mode, unsigned int flavor);
 
 /* p		the policy being inspected
  * id		string role identifier
@@ -62,7 +62,7 @@ extern void test_alias_datum(policydb_t * p, char *id, char *primary_id, char mo
  * This is a utility function to test whether the type set associated with a role in a specific
  * avrule decl block matches our expectations
  */
-extern role_datum_t *test_role_type_set(policydb_t * p, char *id, avrule_decl_t * decl, char **types, unsigned int len, unsigned int flags);
+extern role_datum_t *test_role_type_set(policydb_t * p, const char *id, avrule_decl_t * decl, const char **types, unsigned int len, unsigned int flags);
 
 /* p		the policy being inspected
  * id		string attribute identifier
@@ -73,6 +73,6 @@ extern role_datum_t *test_role_type_set(policydb_t * p, char *id, avrule_decl_t
  * This is a utility function to test whether the type set associated with an attribute in a specific
  * avrule decl block matches our expectations 
  */
-extern void test_attr_types(policydb_t * p, char *id, avrule_decl_t * decl, char **types, int len);
+extern void test_attr_types(policydb_t * p, const char *id, avrule_decl_t * decl, const char **types, int len);
 
 #endif
libsepol/tests/test-deps.c +2 -2
index ed23e0992ced..f495087ac41a 100644
@@ -135,7 +135,7 @@ int deps_test_cleanup(void)
  *  decl_type          name of the unique type found in the module's global
  *                       section is to find that avrule_decl.
  */
-static void do_deps_modreq_global(int req_met, int b, char *policy, char *decl_type)
+static void do_deps_modreq_global(int req_met, int b, const char *policy, const char *decl_type)
 {
 	policydb_t *base;
 	policydb_t mod;
@@ -218,7 +218,7 @@ static void deps_modreq_global(void)
  *  decl_type          name of the unique type found in the module's global
  *                       section is to find that avrule_decl.
  */
-static void do_deps_modreq_opt(int req_met, int ret_val, int b, char *policy, char *decl_type)
+static void do_deps_modreq_opt(int req_met, int ret_val, int b, const char *policy, const char *decl_type)
 {
 	policydb_t *base;
 	policydb_t mod;
libsepol/tests/test-expander-attr-map.c +17 -17
index 5c24ced237dd..b2f59aee42c8 100644
@@ -35,35 +35,35 @@ void test_expander_attr_mapping(void)
 	   because declare in optional then require in a different optional
 	   logic still doesn't work */
 
-	char *typesb1[] = { "attr_check_base_1_1_t", "attr_check_base_1_2_t" };
-	char *typesb2[] = { "attr_check_base_2_1_t", "attr_check_base_2_2_t" };
-	char *typesb3[] = { "attr_check_base_3_1_t", "attr_check_base_3_2_t",
+	const char *typesb1[] = { "attr_check_base_1_1_t", "attr_check_base_1_2_t" };
+	const char *typesb2[] = { "attr_check_base_2_1_t", "attr_check_base_2_2_t" };
+	const char *typesb3[] = { "attr_check_base_3_1_t", "attr_check_base_3_2_t",
 		"attr_check_base_3_3_t", "attr_check_base_3_4_t"
 	};
-	char *typesb4[] = { "attr_check_base_4_1_t", "attr_check_base_4_2_t" };
-	char *typesb5[] = { "attr_check_base_5_1_t", "attr_check_base_5_2_t" };
-	char *typesb6[] = { "attr_check_base_6_1_t", "attr_check_base_6_2_t",
+	const char *typesb4[] = { "attr_check_base_4_1_t", "attr_check_base_4_2_t" };
+	const char *typesb5[] = { "attr_check_base_5_1_t", "attr_check_base_5_2_t" };
+	const char *typesb6[] = { "attr_check_base_6_1_t", "attr_check_base_6_2_t",
 		"attr_check_base_6_3_t", "attr_check_base_6_4_t"
 	};
-	char *typesbo2[] = { "attr_check_base_optional_2_1_t",
+	const char *typesbo2[] = { "attr_check_base_optional_2_1_t",
 		"attr_check_base_optional_2_2_t"
 	};
-	char *typesbo5[] = { "attr_check_base_optional_5_1_t",
+	const char *typesbo5[] = { "attr_check_base_optional_5_1_t",
 		"attr_check_base_optional_5_2_t"
 	};
-	char *typesm2[] = { "attr_check_mod_2_1_t", "attr_check_mod_2_2_t" };
-	char *typesm4[] = { "attr_check_mod_4_1_t", "attr_check_mod_4_2_t" };
-	char *typesm5[] = { "attr_check_mod_5_1_t", "attr_check_mod_5_2_t" };
-	char *typesm6[] = { "attr_check_mod_6_1_t", "attr_check_mod_6_2_t",
+	const char *typesm2[] = { "attr_check_mod_2_1_t", "attr_check_mod_2_2_t" };
+	const char *typesm4[] = { "attr_check_mod_4_1_t", "attr_check_mod_4_2_t" };
+	const char *typesm5[] = { "attr_check_mod_5_1_t", "attr_check_mod_5_2_t" };
+	const char *typesm6[] = { "attr_check_mod_6_1_t", "attr_check_mod_6_2_t",
 		"attr_check_mod_6_3_t", "attr_check_mod_6_4_t"
 	};
-	char *typesmo2[] = { "attr_check_mod_optional_4_1_t",
+	const char *typesmo2[] = { "attr_check_mod_optional_4_1_t",
 		"attr_check_mod_optional_4_2_t"
 	};
-	char *typesb10[] = { "attr_check_base_10_1_t", "attr_check_base_10_2_t" };
-	char *typesb11[] = { "attr_check_base_11_3_t", "attr_check_base_11_4_t" };
-	char *typesm10[] = { "attr_check_mod_10_1_t", "attr_check_mod_10_2_t" };
-	char *typesm11[] = { "attr_check_mod_11_3_t", "attr_check_mod_11_4_t" };
+	const char *typesb10[] = { "attr_check_base_10_1_t", "attr_check_base_10_2_t" };
+	const char *typesb11[] = { "attr_check_base_11_3_t", "attr_check_base_11_4_t" };
+	const char *typesm10[] = { "attr_check_mod_10_1_t", "attr_check_mod_10_2_t" };
+	const char *typesm11[] = { "attr_check_mod_11_3_t", "attr_check_mod_11_4_t" };
 
 	test_attr_types(&base_expanded2, "attr_check_base_1", NULL, typesb1, 2);
 	test_attr_types(&base_expanded2, "attr_check_base_2", NULL, typesb2, 2);
libsepol/tests/test-expander-roles.c +1 -1
index ecfa88e338d0..aba3c9bd19c4 100644
@@ -31,7 +31,7 @@ extern policydb_t role_expanded;
 
 void test_expander_role_mapping(void)
 {
-	char *types1[] = { "role_check_1_1_t", "role_check_1_2_t" };
+	const char *types1[] = { "role_check_1_1_t", "role_check_1_2_t" };
 
 	test_role_type_set(&role_expanded, "role_check_1", NULL, types1, 2, 0);
 }
libsepol/tests/test-expander-users.c +2 -2
index 549492b183a6..f3b98a6c224e 100644
@@ -28,7 +28,7 @@
 
 extern policydb_t user_expanded;
 
-static void check_user_roles(policydb_t * p, char *user_name, char **role_names, int num_roles)
+static void check_user_roles(policydb_t * p, const char *user_name, const char **role_names, int num_roles)
 {
 	user_datum_t *user;
 	ebitmap_node_t *tnode;
@@ -69,7 +69,7 @@ static void check_user_roles(policydb_t * p, char *user_name, char **role_names,
 
 void test_expander_user_mapping(void)
 {
-	char *roles1[] = { "user_check_1_1_r", "user_check_1_2_r" };
+	const char *roles1[] = { "user_check_1_1_r", "user_check_1_2_r" };
 
 	check_user_roles(&user_expanded, "user_check_1", roles1, 2);
 }
libsepol/tests/test-expander.c +7 -7
index bc946c778b1d..ee70e220918c 100644
@@ -67,7 +67,7 @@ extern int mls;
 /* Takes base, some number of modules, links them, and expands them
    reads source from myfiles array, which has the base string followed by
    each module string */
-int expander_policy_init(policydb_t * mybase, int num_modules, policydb_t ** mymodules, policydb_t * myexpanded, char **myfiles)
+int expander_policy_init(policydb_t * mybase, int num_modules, policydb_t ** mymodules, policydb_t * myexpanded, const char *const *myfiles)
 {
 	char *filename[num_modules + 1];
 	int i;
@@ -130,14 +130,14 @@ int expander_policy_init(policydb_t * mybase, int num_modules, policydb_t ** mym
 
 int expander_test_init(void)
 {
-	char *small_base_file = "small-base.conf";
-	char *base_only_file = "base-base-only.conf";
+	const char *small_base_file = "small-base.conf";
+	const char *base_only_file = "base-base-only.conf";
 	int rc;
 	policydb_t *mymod2;
-	char *files2[] = { "small-base.conf", "module.conf" };
-	char *role_files[] = { "role-base.conf", "role-module.conf" };
-	char *user_files[] = { "user-base.conf", "user-module.conf" };
-	char *alias_files[] = { "alias-base.conf", "alias-module.conf" };
+	const char *files2[] = { "small-base.conf", "module.conf" };
+	const char *role_files[] = { "role-base.conf", "role-module.conf" };
+	const char *user_files[] = { "user-base.conf", "user-module.conf" };
+	const char *alias_files[] = { "alias-base.conf", "alias-module.conf" };
 
 	rc = expander_policy_init(&basemod, 0, NULL, &base_expanded, &small_base_file);
 	if (rc != 0)
libsepol/tests/test-linker-cond-map.c +2 -2
index 0ef0d698d58c..712d99149883 100644
@@ -50,7 +50,7 @@
  */
 
 typedef struct test_cond_expr {
-	char *bool;
+	const char *bool;
 	uint32_t expr_type;
 } test_cond_expr_t;
 
@@ -75,7 +75,7 @@ void test_cond_expr_mapping(policydb_t * p, avrule_decl_t * d, test_cond_expr_t
 	}
 }
 
-void test_bool_state(policydb_t * p, char *bool, int state)
+void test_bool_state(policydb_t * p, const char *bool, int state)
 {
 	cond_bool_datum_t *b;
 
libsepol/tests/test-linker-roles.c +2 -2
index 569e2ccf5ee7..c9e2f2ba7e90 100644
@@ -72,7 +72,7 @@ void base_role_tests(policydb_t * base)
 	avrule_decl_t *decl;
 	role_datum_t *role;
 	unsigned int decls[2];
-	char *types[2];
+	const char *types[2];
 
 	/* These tests look at roles in the base only, the desire is to ensure that
 	 * roles are not destroyed or otherwise removed during the link process */
@@ -102,7 +102,7 @@ void module_role_tests(policydb_t * base)
 	role_datum_t *role;
 	avrule_decl_t *decl;
 	unsigned int decls[3];
-	char *types[3];
+	const char *types[3];
 
 	/* These tests are run when the base is linked with 2 modules,
 	 * They should test whether the roles get copied correctly from the 
libsepol/tests/test-linker-types.c +3 -3
index 94f16ac1615c..b41e08e0099c 100644
@@ -97,7 +97,7 @@
  */
 
 /* Don't pass in decls from global blocks since symbols aren't stored in their symtab */
-static void test_type_datum(policydb_t * p, char *id, unsigned int *decls, int len, unsigned int primary)
+static void test_type_datum(policydb_t * p, const char *id, unsigned int *decls, int len, unsigned int primary)
 {
 	int i;
 	unsigned int value;
@@ -125,7 +125,7 @@ static void test_type_datum(policydb_t * p, char *id, unsigned int *decls, int l
 void base_type_tests(policydb_t * base)
 {
 	unsigned int decls[2];
-	char *types[2];
+	const char *types[2];
 
 	/* These tests look at types in the base only, the desire is to ensure that
 	 * types are not destroyed or otherwise removed during the link process.
@@ -163,7 +163,7 @@ void base_type_tests(policydb_t * base)
 void module_type_tests(policydb_t * base)
 {
 	unsigned int decls[2];
-	char *types[2];
+	const char *types[2];
 	avrule_decl_t *d;
 
 	/* These tests look at types that were copied from modules or attributes
Signature · 1 line
2.11.0
#8 lore raw

PATCH 8/8 libsemanage/tests: fix -Wwrite-strings warnings

In reply to Nicolas Iooss
---
 libsemanage/src/utilities.c        | 2 +-
 libsemanage/src/utilities.h        | 2 +-
 libsemanage/tests/test_utilities.c | 8 ++++----
 3 files changed, 6 insertions(+), 6 deletions(-)
libsemanage/src/utilities.c +1 -1
index 0d50d99026db..ba6dc85ab23b 100644
@@ -199,7 +199,7 @@ int semanage_cmp_plist_t(const semanage_list_t ** x, const semanage_list_t ** y)
 	return strcmp((*x)->data, (*y)->data);
 }
 
-int semanage_str_count(char *data, char what)
+int semanage_str_count(const char *data, char what)
 {
 	int count = 0;
 
libsemanage/src/utilities.h +1 -1
index f2ff31f0f5b6..ba1ed0297ed7 100644
@@ -105,7 +105,7 @@ int semanage_cmp_plist_t(const semanage_list_t ** x,
  * @param      what  a charachter
  * @returns    the number of times the char appears in the string
  */
-int semanage_str_count(char *data, char what);
+int semanage_str_count(const char *data, char what);
 /**
  * @param      - a string
  * @param            the charachter to trim to
libsemanage/tests/test_utilities.c +4 -4
index 30ac7f5823e6..601508c2c386 100644
@@ -122,9 +122,9 @@ int semanage_utilities_add_tests(CU_pSuite suite)
 
 void test_semanage_is_prefix(void)
 {
-	char *str = "some string";
-	char *pre = "some";
-	char *not_pre = "not this";
+	const char *str = "some string";
+	const char *pre = "some";
+	const char *not_pre = "not this";
 
 	CU_ASSERT_TRUE(semanage_is_prefix(str, pre));
 	CU_ASSERT_TRUE(semanage_is_prefix(str, ""));
@@ -239,7 +239,7 @@ void test_semanage_list(void)
 
 void test_semanage_str_count(void)
 {
-	char *test_string = "abaababbaaaba";
+	const char *test_string = "abaababbaaaba";
 
 	CU_ASSERT_EQUAL(semanage_str_count(test_string, 'z'), 0);
 	CU_ASSERT_EQUAL(semanage_str_count(test_string, 'a'), 8);
Signature · 1 line
2.11.0
#9 lore raw
In reply to Nicolas Iooss
Quoted 13 lines · Nicolas Iooss

When compiling with -Wwrite-strings, clang reports some warnings like:

module_to_cil.c:784:13: error: assigning to 'char *' from 'const char [5]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
statement = "type";
^ ~~~~~~
module_to_cil.c:787:13: error: assigning to 'char *' from 'const char [5]' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
statement = "role";
^ ~~~~~~

Quoted 4 lines

Add a const type attribute to local variables which only handle constant strings.

Applied all of these except for patch 3, which I used the patch that I posted in response.

Thanks,
Jim

Quoted 127 lines
---
 libsepol/cil/src/cil_binary.c |  4 ++--
 libsepol/cil/src/cil_policy.c | 18 +++++++++---------
 libsepol/src/module_to_cil.c  |  2 +-
 3 files changed, 12 insertions(+), 12 deletions(-)
libsepol/cil/src/cil_binary.c +2 -2
index 19cbc1b87c1e..ac371aef7b2d 100644
@@ -1923,7 +1923,7 @@ static void __cil_expr_to_string(struct cil_list *expr, enum cil_flavor flavor,
 				cil_asprintf(out, "%s %s", CIL_KEY_NOT, s1);
 				free(s1);
 			} else {
-				char *opstr = "";
+				const char *opstr = "";
 
 				__cil_expr_to_string_helper(curr->next->next, flavor, &s2);
 
@@ -4376,7 +4376,7 @@ static void __cil_print_classperm(struct cil_list *cp_list)
 
 static void __cil_print_permissionx(struct cil_permissionx *px)
 {
-	char *kind_str = "";
+	const char *kind_str = "";
 	char *expr_str;
 
 	switch (px->kind) {
libsepol/cil/src/cil_policy.c +9 -9
index bb832f250db7..2e6814a60eae 100644
@@ -183,7 +183,7 @@ static void cil_gather_statements(struct cil_tree_node *start, struct cil_list *
 	cil_tree_walk(start, __cil_gather_statements_helper, NULL, NULL, lists);
 }
 
-static void cil_simple_rules_to_policy(FILE *out, struct cil_list *rules, char *kind)
+static void cil_simple_rules_to_policy(FILE *out, struct cil_list *rules, const char *kind)
 {
 	struct cil_list_item *i1;
 
@@ -194,7 +194,7 @@ static void cil_simple_rules_to_policy(FILE *out, struct cil_list *rules, char *
 
 static void cil_cats_to_policy(FILE *out, struct cil_cats *cats)
 {
-	char *lead = "";
+	const char *lead = "";
 	struct cil_cat *first = NULL, *last = NULL, *cat;
 	struct cil_list_item *i1;
 
@@ -471,7 +471,7 @@ static char *__cil_cons_leaf_operand_to_string(struct cil_db *db, struct cil_lis
 {
 	struct cil_list_item *i1;
 	enum cil_flavor flavor = operand->flavor;
-	char *o_str;
+	const char *o_str;
 	size_t o_len;
 
 	if (flavor == CIL_CONS_OPERAND) {
@@ -559,7 +559,7 @@ static char *__cil_cons_leaf_operand_to_string(struct cil_db *db, struct cil_lis
 static char *__cil_cons_leaf_op_to_string(struct cil_list_item *op, char *new)
 {
 	enum cil_flavor flavor = (enum cil_flavor)op->data;
-	char *op_str;
+	const char *op_str;
 	size_t len;
 
 	switch (flavor) {
@@ -1009,7 +1009,7 @@ static void cil_bools_to_policy(FILE *out, struct cil_list *bools)
 {
 	struct cil_list_item *i1;
 	struct cil_bool *bool;
-	char *value;
+	const char *value;
 
 	cil_list_for_each(i1, bools) {
 		bool = i1->data;
@@ -1108,7 +1108,7 @@ static void cil_xperms_to_policy(FILE *out, struct cil_permissionx *permx)
 	ebitmap_node_t *node;
 	unsigned int i, first = 0, last = 0;
 	int need_first = CIL_TRUE, need_last = CIL_TRUE;
-	char *kind;
+	const char *kind;
 
 	if (permx->kind == CIL_PERMX_KIND_IOCTL) {
 		kind = "ioctl";
@@ -1156,7 +1156,7 @@ static void cil_xperms_to_policy(FILE *out, struct cil_permissionx *permx)
 
 static void cil_av_rulex_to_policy(FILE *out, struct cil_avrule *rule)
 {
-	char *kind;
+	const char *kind;
 	struct cil_symtab_datum *src, *tgt;
 
 	src = rule->src;
@@ -1187,7 +1187,7 @@ static void cil_av_rulex_to_policy(FILE *out, struct cil_avrule *rule)
 
 static void cil_av_rule_to_policy(FILE *out, struct cil_avrule *rule)
 {
-	char *kind;
+	const char *kind;
 	struct cil_symtab_datum *src, *tgt;
 	struct cil_list *classperms_strs;
 	struct cil_list_item *i1;
@@ -1225,7 +1225,7 @@ static void cil_av_rule_to_policy(FILE *out, struct cil_avrule *rule)
 
 static void cil_type_rule_to_policy(FILE *out, struct cil_type_rule *rule)
 {
-	char *kind;
+	const char *kind;
 	struct cil_symtab_datum *src, *tgt, *res;
 	struct cil_list *class_list;
 	struct cil_list_item *i1;
libsepol/src/module_to_cil.c +1 -1
index 2acb600dddae..8c4fff9c3d9f 100644
@@ -775,7 +775,7 @@ static int cil_print_attr_strs(int indent, struct policydb *pdb, int is_type, st
 	int rc = 0;
 	struct ebitmap_node *node;
 	unsigned int i;
-	char *statement;
+	const char *statement;
 	int has_positive = pos && (ebitmap_cardinality(pos) > 0);
 	int has_negative = neg && (ebitmap_cardinality(neg) > 0);
 	char **val_to_name;
Signature · 2 lines
James Carter <[email protected]>
National Security Agency