]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
libndr: Simplify ndr_token_retrieve_cmp_fn()
authorVolker Lendecke <vl@samba.org>
Wed, 28 Aug 2024 10:08:57 +0000 (12:08 +0200)
committerVolker Lendecke <vl@samba.org>
Thu, 29 Aug 2024 07:22:34 +0000 (07:22 +0000)
Avoid an if-statement inside by passing a pointer-comparing function

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jennifer Sutton <josutton@catalyst.net.nz>
librpc/ndr/ndr.c

index 9e538f6826732ac062f1cc8073b937590a1228a7..75b6f67f3f377f7abbe5760ca208c5ab9e08d39d 100644 (file)
@@ -1051,17 +1051,9 @@ _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list *list
 {
        struct ndr_token *tokens = list->tokens;
        unsigned i;
-       if (_cmp_fn) {
-               for (i = list->count - 1; i < list->count; i--) {
-                       if (_cmp_fn(tokens[i].key, key) == 0) {
-                               goto found;
-                       }
-               }
-       } else {
-               for (i = list->count - 1; i < list->count; i--) {
-                       if (tokens[i].key == key) {
-                               goto found;
-                       }
+       for (i = list->count - 1; i < list->count; i--) {
+               if (_cmp_fn(tokens[i].key, key) == 0) {
+                       goto found;
                }
        }
        return NDR_ERR_TOKEN;
@@ -1076,13 +1068,18 @@ found:
        return NDR_ERR_SUCCESS;
 }
 
+static int token_cmp_ptr(const void *a, const void *b)
+{
+       return (a == b) ? 0 : 1;
+}
+
 /*
   retrieve a token from a ndr context
 */
 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list *list,
                                              const void *key, uint32_t *v)
 {
-       return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
+       return ndr_token_retrieve_cmp_fn(list, key, v, token_cmp_ptr, true);
 }
 
 /*
@@ -1091,7 +1088,7 @@ _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list *list,
 _PUBLIC_ enum ndr_err_code ndr_token_peek(struct ndr_token_list *list,
                                          const void *key, uint32_t *v)
 {
-       return ndr_token_retrieve_cmp_fn(list, key, v, NULL, false);
+       return ndr_token_retrieve_cmp_fn(list, key, v, token_cmp_ptr, false);
 }
 
 /*