From 2994d1ca1da9e6f25f082c0dd1a49b5f958bd2ca Mon Sep 17 00:00:00 2001
From: Nicolas Iooss <nicolas.iooss@m4x.org>
Date: Sat, 1 Nov 2014 14:23:28 +0100
Subject: [PATCH] Cast the 2nd parameter of hashtab_search to hashtab_key_t

"gcc -Wwrite-strings" reported warnings when using hashtab_search (from
libsepol) with string literals as its second parameter is a non-constant
string.

Indeed /usr/include/sepol/policydb/hashtab.h contains:

    typedef char *hashtab_key_t;
    /* ... */
    extern hashtab_datum_t hashtab_search(hashtab_t h, const hashtab_key_t k);

This means the second parameter is "char *const k", not "const char *k".
As a consequence:

* Casting to "const hashtab_key_t" leads to misunderstanding the code.
* "const char*" variables need to be explicitly casted to "char*" or
  "hashtab_key_t" before calling hashtab_search.
* When using "gcc -Wwrite-strings", literal strings need to be casted to
  "char*" or "hashtab_key_t" before calling hashtab_search.
* "gcc -Wcast-qual" reports an awful amount of warnings due to
  const-to-nonconst pointer casts.

Add missing casts to hashtab_key_t to help finding real bugs in
setools/libqpol with gcc flags.
---
 libqpol/bool_query.c       |  2 +-
 libqpol/class_perm_query.c |  4 ++--
 libqpol/mls_query.c        |  4 ++--
 libqpol/module_compiler.c  |  6 +++---
 libqpol/policy.c           | 12 ++++++------
 libqpol/policy_define.c    | 12 ++++++------
 libqpol/policy_extend.c    |  2 +-
 libqpol/role_query.c       |  2 +-
 libqpol/type_query.c       |  2 +-
 libqpol/user_query.c       |  2 +-
 10 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/libqpol/bool_query.c b/libqpol/bool_query.c
index 8814a59..78d544e 100644
--- a/libqpol/bool_query.c
+++ b/libqpol/bool_query.c
@@ -48,7 +48,7 @@ int qpol_policy_get_bool_by_name(const qpol_policy_t * policy, const char *name,
 	}
 
 	db = &policy->p->p;
-	internal_datum = hashtab_search(db->p_bools.table, (const hashtab_key_t)name);
+	internal_datum = hashtab_search(db->p_bools.table, (hashtab_key_t)name);
 	if (internal_datum == NULL) {
 		ERR(policy, "could not find datum for bool %s", name);
 		*datum = NULL;
diff --git a/libqpol/class_perm_query.c b/libqpol/class_perm_query.c
index 70750ed..6776bd2 100644
--- a/libqpol/class_perm_query.c
+++ b/libqpol/class_perm_query.c
@@ -376,7 +376,7 @@ int qpol_policy_get_class_by_name(const qpol_policy_t * policy, const char *name
 	}
 
 	db = &policy->p->p;
-	internal_datum = hashtab_search(db->p_classes.table, (const hashtab_key_t)name);
+	internal_datum = hashtab_search(db->p_classes.table, (hashtab_key_t)name);
 	if (internal_datum == NULL) {
 		*obj_class = NULL;
 		ERR(policy, "could not find class %s", name);
@@ -541,7 +541,7 @@ int qpol_policy_get_common_by_name(const qpol_policy_t * policy, const char *nam
 	}
 
 	db = &policy->p->p;
-	internal_datum = hashtab_search(db->p_commons.table, (const hashtab_key_t)name);
+	internal_datum = hashtab_search(db->p_commons.table, (hashtab_key_t)name);
 	if (internal_datum == NULL) {
 		*common = NULL;
 		ERR(policy, "could not find common %s", name);
diff --git a/libqpol/mls_query.c b/libqpol/mls_query.c
index 8c46acd..c0a242e 100644
--- a/libqpol/mls_query.c
+++ b/libqpol/mls_query.c
@@ -47,7 +47,7 @@ int qpol_policy_get_level_by_name(const qpol_policy_t * policy, const char *name
 		return STATUS_ERR;
 	}
 	db = &policy->p->p;
-	internal_datum = hashtab_search(db->p_levels.table, (const hashtab_key_t)name);
+	internal_datum = hashtab_search(db->p_levels.table, (hashtab_key_t)name);
 	if (internal_datum == NULL) {
 		ERR(policy, "could not find datum for level %s", name);
 		errno = ENOENT;
@@ -335,7 +335,7 @@ int qpol_policy_get_cat_by_name(const qpol_policy_t * policy, const char *name,
 	}
 
 	db = &policy->p->p;
-	internal_datum = hashtab_search(db->p_cats.table, (const hashtab_key_t)name);
+	internal_datum = hashtab_search(db->p_cats.table, (hashtab_key_t)name);
 	if (internal_datum == NULL) {
 		*datum = NULL;
 		ERR(policy, "could not find datum for cat %s", name);
diff --git a/libqpol/module_compiler.c b/libqpol/module_compiler.c
index 17bef67..af28dac 100644
--- a/libqpol/module_compiler.c
+++ b/libqpol/module_compiler.c
@@ -1257,7 +1257,7 @@ int is_id_in_scope(uint32_t symbol_type, hashtab_key_t id)
 {
 	scope_datum_t *scope =
 	    (scope_datum_t *) hashtab_search(policydbp->scope[symbol_type].
-					     table, id);
+					     table, (hashtab_key_t)id);
 	if (scope == NULL) {
 		return 1;	/* id is not known, so return success */
 	}
@@ -1304,13 +1304,13 @@ int is_perm_in_scope(hashtab_key_t perm_id, hashtab_key_t class_id)
 {
 	class_datum_t *cladatum =
 	    (class_datum_t *) hashtab_search(policydbp->p_classes.table,
-					     class_id);
+					     (hashtab_key_t)class_id);
 	perm_datum_t *perdatum;
 	if (cladatum == NULL) {
 		return 1;
 	}
 	perdatum = (perm_datum_t *) hashtab_search(cladatum->permissions.table,
-						   perm_id);
+						   (hashtab_key_t)perm_id);
 	if (perdatum == NULL) {
 		return 1;
 	}
diff --git a/libqpol/policy.c b/libqpol/policy.c
index 7495054..ee76c0e 100644
--- a/libqpol/policy.c
+++ b/libqpol/policy.c
@@ -503,7 +503,7 @@ static int infer_policy_version(qpol_policy_t * policy)
 	}
 
 	/* 18 : the netlink_audit_socket class added */
-	else if (hashtab_search(db->p_classes.table, (const hashtab_key_t)"netlink_audit_socket")) {
+	else if (hashtab_search(db->p_classes.table, (hashtab_key_t)"netlink_audit_socket")) {
 		db->policyvers = 18;
 	}
 
@@ -664,7 +664,7 @@ static int union_multiply_declared_symbols(qpol_policy_t * policy) {
 			avrule_decl_t *decl = blk->enabled;
 			if (!decl)
 				continue; /* disabled */
-			type_datum_t *internal_datum = hashtab_search(decl->symtab[SYM_TYPES].table, (const hashtab_key_t)name);
+			type_datum_t *internal_datum = hashtab_search(decl->symtab[SYM_TYPES].table, (hashtab_key_t)name);
 			if (internal_datum == NULL) {
 				continue; /* not declared here */
 			}
@@ -695,7 +695,7 @@ static int union_multiply_declared_symbols(qpol_policy_t * policy) {
 			goto err;
 		}
 		policydb_t *db = &policy->p->p;
-		scope_datum_t* scope_datum = hashtab_search(db->scope[SYM_ROLES].table, (const hashtab_key_t)name);
+		scope_datum_t* scope_datum = hashtab_search(db->scope[SYM_ROLES].table, (hashtab_key_t)name);
 		if (scope_datum == NULL) {
 			ERR(policy, "could not find scope datum for role %s", name);
 			error = ENOENT;
@@ -705,7 +705,7 @@ static int union_multiply_declared_symbols(qpol_policy_t * policy) {
 		{
 			if (db->decl_val_to_struct[scope_datum->decl_ids[i] - 1]->enabled == 0)
 				continue; /* block is disabled */
-			role_datum_t *internal_datum = hashtab_search(db->decl_val_to_struct[scope_datum->decl_ids[i] - 1]->symtab[SYM_ROLES].table, (const hashtab_key_t)name);
+			role_datum_t *internal_datum = hashtab_search(db->decl_val_to_struct[scope_datum->decl_ids[i] - 1]->symtab[SYM_ROLES].table, (hashtab_key_t)name);
 			if (internal_datum == NULL) {
 				continue; /* not declared here */
 			}
@@ -736,7 +736,7 @@ static int union_multiply_declared_symbols(qpol_policy_t * policy) {
 			goto err;
 		}
 		policydb_t *db = &policy->p->p;
-		scope_datum_t* scope_datum = hashtab_search(db->scope[SYM_USERS].table, (const hashtab_key_t)name);
+		scope_datum_t* scope_datum = hashtab_search(db->scope[SYM_USERS].table, (hashtab_key_t)name);
 		if (scope_datum == NULL) {
 			ERR(policy, "could not find scope datum for user %s", name);
 			error = ENOENT;
@@ -746,7 +746,7 @@ static int union_multiply_declared_symbols(qpol_policy_t * policy) {
 		{
 			if (db->decl_val_to_struct[scope_datum->decl_ids[i] - 1]->enabled == 0)
 				continue; /* block is disabled */
-			user_datum_t *internal_datum = hashtab_search(db->decl_val_to_struct[scope_datum->decl_ids[i] -1 ]->symtab[SYM_USERS].table, (const hashtab_key_t)name);
+			user_datum_t *internal_datum = hashtab_search(db->decl_val_to_struct[scope_datum->decl_ids[i] -1 ]->symtab[SYM_USERS].table, (hashtab_key_t)name);
 			if (internal_datum == NULL) {
 				continue; /* not declared here */
 			}
diff --git a/libqpol/policy_define.c b/libqpol/policy_define.c
index 7042b4c..1c75d9c 100644
--- a/libqpol/policy_define.c
+++ b/libqpol/policy_define.c
@@ -1389,7 +1389,7 @@ static int define_typebounds_helper(char *bounds_id, char *type_id)
 		return -1;
 	}
 
-	bounds = hashtab_search(policydbp->p_types.table, bounds_id);
+	bounds = hashtab_search(policydbp->p_types.table, (hashtab_key_t)bounds_id);
 	if (!bounds || bounds->flavor == TYPE_ATTRIB) {
 		yyerror2("hoge unknown type %s", bounds_id);
 		return -1;
@@ -1400,7 +1400,7 @@ static int define_typebounds_helper(char *bounds_id, char *type_id)
 		return -1;
 	}
 
-	type = hashtab_search(policydbp->p_types.table, type_id);
+	type = hashtab_search(policydbp->p_types.table, (hashtab_key_t)type_id);
 	if (!type || type->flavor == TYPE_ATTRIB) {
 		yyerror2("type %s is not declared", type_id);
 		return -1;
@@ -1508,7 +1508,7 @@ int define_type(int alias)
 			free(id);
 			return -1;
 		}
-		attr = hashtab_search(policydbp->p_types.table, id);
+		attr = hashtab_search(policydbp->p_types.table, (hashtab_key_t)id);
 		if (!attr) {
 			/* treat it as a fatal error */
 			yyerror2("attribute %s is not declared", id);
@@ -1581,7 +1581,7 @@ static int set_types(type_set_t * set, char *id, int *add, char starallowed)
 		free(id);
 		return -1;
 	}
-	t = hashtab_search(policydbp->p_types.table, id);
+	t = hashtab_search(policydbp->p_types.table, (hashtab_key_t)id);
 	if (!t) {
 		yyerror2("unknown type %s", id);
 		free(id);
@@ -2494,7 +2494,7 @@ int define_role_trans(int class_specified)
 			return -1;
 	} else {
 		cladatum = hashtab_search(policydbp->p_classes.table,
-					  "process");
+					  (hashtab_key_t)"process");
 		if (!cladatum) {
 			yyerror2("could not find process class for "
 				 "legacy role_transition statement");
@@ -4889,7 +4889,7 @@ int define_range_trans(int class_specified)
 			goto out;
 	} else {
 		cladatum = hashtab_search(policydbp->p_classes.table,
-		                          "process");
+		                          (hashtab_key_t)"process");
 		if (!cladatum) {
 			yyerror2("could not find process class for "
 			         "legacy range_transition statement");
diff --git a/libqpol/policy_extend.c b/libqpol/policy_extend.c
index 945f4e9..84c03fe 100644
--- a/libqpol/policy_extend.c
+++ b/libqpol/policy_extend.c
@@ -471,7 +471,7 @@ static int qpol_policy_add_object_r(qpol_policy_t * policy)
 
 	db = &policy->p->p;
 
-	hashtab_datum_t datum = hashtab_search(db->p_roles.table, (const hashtab_key_t)OBJECT_R);
+	hashtab_datum_t datum = hashtab_search(db->p_roles.table, (hashtab_key_t)OBJECT_R);
 	if (datum == NULL) {
 		ERR(policy, "%s", OBJECT_R " not found in policy!");
 		errno = EIO;
diff --git a/libqpol/role_query.c b/libqpol/role_query.c
index c7d7eec..e057009 100644
--- a/libqpol/role_query.c
+++ b/libqpol/role_query.c
@@ -49,7 +49,7 @@ int qpol_policy_get_role_by_name(const qpol_policy_t * policy, const char *name,
 	}
 
 	db = &policy->p->p;
-	internal_datum = hashtab_search(db->p_roles.table, (const hashtab_key_t)name);
+	internal_datum = hashtab_search(db->p_roles.table, (hashtab_key_t)name);
 	if (internal_datum == NULL) {
 		*datum = NULL;
 		ERR(policy, "could not find datum for role %s", name);
diff --git a/libqpol/type_query.c b/libqpol/type_query.c
index a746180..321c20c 100644
--- a/libqpol/type_query.c
+++ b/libqpol/type_query.c
@@ -49,7 +49,7 @@ int qpol_policy_get_type_by_name(const qpol_policy_t * policy, const char *name,
 	}
 
 	db = &policy->p->p;
-	internal_datum = hashtab_search(db->p_types.table, (const hashtab_key_t)name);
+	internal_datum = hashtab_search(db->p_types.table, (hashtab_key_t)name);
 	if (internal_datum == NULL) {
 		*datum = NULL;
 		ERR(policy, "could not find datum for type %s", name);
diff --git a/libqpol/user_query.c b/libqpol/user_query.c
index 37237d5..8a72336 100644
--- a/libqpol/user_query.c
+++ b/libqpol/user_query.c
@@ -53,7 +53,7 @@ int qpol_policy_get_user_by_name(const qpol_policy_t * policy, const char *name,
 	}
 
 	db = &policy->p->p;
-	internal_datum = hashtab_search(db->p_users.table, (const hashtab_key_t)name);
+	internal_datum = hashtab_search(db->p_users.table, (hashtab_key_t)name);
 	if (internal_datum == NULL) {
 		*datum = NULL;
 		ERR(policy, "could not find datum for user %s", name);