Const in APIs (2)
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
@@ -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
@@ -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
@@ -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
@@ -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
Quoted 5 lines · Ivan Gyurdiev
Remove malloc nonsense due to non-const hashtab keys.
Note: I don't understand how
symtab_initcan get away with initializing a comparator/hasher with non-const keys. I specifically marked those const inhashtab_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.