Skip to contentSkip to conversation

Const in APIs (2)

SEPOL Ivan Gyurdiev 2 participants 2 messages Jan 6, 2006 – Jan 9, 2006 SELinux security module

lore
Share & tools
lore
#0 lore raw

Remove malloc nonsense due to non-const hashtab keys.

Note: I don't understand how symtab_init can get away with initializing a comparator/hasher with non-const keys. I specifically marked those const in hashtab_init, but that doesn't seem to cause any warnings.

libsepol/include/sepol/policydb/hashtab.h +14 -8
diff -Naurp --exclude ports_local.c --exclude-from excludes old/libsepol/include/sepol/policydb/hashtab.h new/libsepol/include/sepol/policydb/hashtab.h
@@ -52,13 +52,17 @@ typedef hashtab_val_t *hashtab_t;
    Returns NULL if insufficent space is available or
    the new hash table otherwise.
  */
-extern hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
-						     hashtab_key_t key),
-			 int (*keycmp) (hashtab_t h,
-					hashtab_key_t key1,
-					hashtab_key_t key2),
-			 unsigned int size);
-
+extern hashtab_t hashtab_create(
+	unsigned int (*hash_value) (
+		hashtab_t h,
+		const hashtab_key_t key),
+
+	int (*keycmp) (
+		hashtab_t h,
+		const hashtab_key_t key1,
+		const hashtab_key_t key2),
+	
+	unsigned int size);
 /*
    Inserts the specified (key, datum) pair into the specified hash table.
 
@@ -103,7 +107,9 @@ extern int hashtab_replace(hashtab_t h, 
    Returns NULL if no entry has the specified key or
    the datum of the entry otherwise.
  */
-extern hashtab_datum_t hashtab_search(hashtab_t h, hashtab_key_t k);
+extern hashtab_datum_t hashtab_search(
+	hashtab_t h, 
+	const hashtab_key_t k);
 
 /*
    Destroys the specified hash table.
libsepol/src/hashtab.c +15 -11
diff -Naurp --exclude ports_local.c --exclude-from excludes old/libsepol/src/hashtab.c new/libsepol/src/hashtab.c
@@ -11,13 +11,17 @@
 #include <string.h>
 #include <sepol/policydb/hashtab.h>
 
-hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
-						     hashtab_key_t key),
-			 int (*keycmp) (hashtab_t h,
-					hashtab_key_t key1,
-					hashtab_key_t key2),
-			 unsigned int size)
-{
+hashtab_t hashtab_create(
+	unsigned int (*hash_value) (
+		hashtab_t h,
+		const hashtab_key_t key),
+
+	int (*keycmp) (
+		hashtab_t h,
+		const hashtab_key_t key1,
+		const hashtab_key_t key2),
+		unsigned int size) {
+
 	hashtab_t p;
 	unsigned int i;
 
@@ -165,13 +169,13 @@ int hashtab_replace(hashtab_t h, hashtab
 }
 
 
-hashtab_datum_t
-hashtab_search(hashtab_t h, hashtab_key_t key)
-{
+hashtab_datum_t hashtab_search(
+	hashtab_t h, 
+	const hashtab_key_t key) {
+
 	int hvalue;
 	hashtab_ptr_t cur;
 
-
 	if (!h)
 		return NULL;
 
libsepol/src/roles.c +4 -7
diff -Naurp --exclude ports_local.c --exclude-from excludes old/libsepol/src/roles.c new/libsepol/src/roles.c
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <stddef.h>
 
+#include <sepol/policydb/hashtab.h>
 #include <sepol/policydb/policydb.h>
 
 #include "debug.h"
@@ -15,14 +16,10 @@ int sepol_role_exists(
 	int* response) {
 
   	policydb_t *policydb = &p->p;
-	char* role_copy = strdup(role);
-	if (!role_copy) {
-		ERR(handle, "out of memory, role check failed");
-		return STATUS_ERR;
-	}
+	*response = (hashtab_search(policydb->p_roles.table, 
+		(const hashtab_key_t) role) != NULL);
 
-	*response = (hashtab_search(policydb->p_roles.table, role_copy) != NULL);
-	free(role_copy);
+	handle = NULL;
 	return STATUS_SUCCESS;
 }
 
libsepol/src/symtab.c +1 -0
diff -Naurp --exclude ports_local.c --exclude-from excludes old/libsepol/src/symtab.c new/libsepol/src/symtab.c
@@ -8,6 +8,7 @@
  */
 
 #include <string.h>
+#include <sepol/policydb/hashtab.h>
 #include <sepol/policydb/symtab.h>
 
 static unsigned int symhash(hashtab_t h, hashtab_key_t key)
libsepol/src/users.c +26 -52

198 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Ivan Gyurdiev

Remove malloc nonsense due to non-const hashtab keys.

Note: I don't understand how symtab_init can get away with initializing a comparator/hasher with non-const keys. I specifically marked those const in hashtab_init, but that doesn't seem to cause any warnings.

Merged as of libsepol 1.11.8.

"Strong typing is for weak minds"

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.