From: Phil Carmody Date: Thu, 11 Jan 2018 13:20:09 +0000 (+0200) Subject: global - migrate from strncmp to str_begins X-Git-Tag: 2.3.2.rc1~40 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d026e2d2e010ae804a6984e0c28824e5824f9945;p=thirdparty%2Fdovecot%2Fcore.git global - migrate from strncmp to str_begins Simplify a bunch of verbose strncmp(,,strlen()) calls. --- 8< --- strncmp.cocci --- @@ expression e1, e2; @@ - strncmp(e1, e2, strlen(e2)) == 0 + str_begins(e1, e2) @@ expression e1, e2; @@ - strncmp(e1, e2, strlen(e2)) != 0 + !str_begins(e1, e2) @@ expression e1, e2; @@ - strncmp(e1, e2, strlen(e1)) == 0 + str_begins(e2, e1) @@ expression e1, e2; @@ - strncmp(e1, e2, strlen(e1)) != 0 + !str_begins(e2, e1) --- 8< --------------------- Signed-off-by: Phil Carmody --- diff --git a/src/doveadm/doveadm-dict.c b/src/doveadm/doveadm-dict.c index 9a1af6e44e..5f781fc36e 100644 --- a/src/doveadm/doveadm-dict.c +++ b/src/doveadm/doveadm-dict.c @@ -38,15 +38,15 @@ cmd_dict_init_full(struct doveadm_cmd_context *cctx, i_debug("key = %s", key); - if (strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) != 0 && - strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) != 0) { + if (!str_begins(key, DICT_PATH_PRIVATE) && + !str_begins(key, DICT_PATH_SHARED)) { i_error("Key must begin with '"DICT_PATH_PRIVATE "' or '"DICT_PATH_SHARED"': %s", key); doveadm_exit_code = EX_USAGE; return -1; } if (username[0] == '\0' && - strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) == 0) { + str_begins(key, DICT_PATH_PRIVATE)) { i_error("-u must be specified for "DICT_PATH_PRIVATE" keys"); doveadm_exit_code = EX_USAGE; return -1; diff --git a/src/imap-hibernate/imap-client.c b/src/imap-hibernate/imap-client.c index f336bdb476..aba09fd813 100644 --- a/src/imap-hibernate/imap-client.c +++ b/src/imap-hibernate/imap-client.c @@ -490,7 +490,7 @@ imap_client_var_expand_func_userdb(const char *data, void *context, const char *value = NULL; for(;*fields != NULL; fields++) { - if (strncmp(*fields, field_name, strlen(field_name)) == 0) { + if (str_begins(*fields, field_name)) { value = *fields+strlen(field_name); break; } diff --git a/src/imap-login/imap-proxy.c b/src/imap-login/imap-proxy.c index 768426644d..879ee68103 100644 --- a/src/imap-login/imap-proxy.c +++ b/src/imap-login/imap-proxy.c @@ -359,8 +359,7 @@ int imap_proxy_parse_line(struct client *client, const char *line) client_proxy_log_failure(client, log_line); } #define STR_NO_IMAP_RESP_CODE_AUTHFAILED "NO ["IMAP_RESP_CODE_AUTHFAILED"]" - if (strncmp(line, STR_NO_IMAP_RESP_CODE_AUTHFAILED, - strlen(STR_NO_IMAP_RESP_CODE_AUTHFAILED)) == 0) { + if (str_begins(line, STR_NO_IMAP_RESP_CODE_AUTHFAILED)) { /* the remote sent a generic "authentication failed" error. replace it with our one, so that in case the remote is sending a different error message diff --git a/src/lib-dict-backend/dict-cdb.c b/src/lib-dict-backend/dict-cdb.c index def2617200..a7f4123daf 100644 --- a/src/lib-dict-backend/dict-cdb.c +++ b/src/lib-dict-backend/dict-cdb.c @@ -197,9 +197,9 @@ static bool cdb_dict_iterate(struct dict_iterate_context *_ctx, if (((ctx->flags & DICT_ITERATE_FLAG_EXACT_KEY) != 0 && strcmp(key, *ptr) == 0) || ((ctx->flags & DICT_ITERATE_FLAG_RECURSE) != 0 && - strncmp(key, *ptr, strlen(*ptr)) == 0) || + str_begins(key, *ptr)) || ((ctx->flags & DICT_ITERATE_FLAG_RECURSE) == 0 && - strncmp(key, *ptr, strlen(*ptr)) == 0 && + str_begins(key, *ptr) && strchr(key + strlen(*ptr), '/') == NULL)) { match = TRUE; break; diff --git a/src/lib-dict-extra/dict-fs.c b/src/lib-dict-extra/dict-fs.c index 0f00b2e19a..7987ffd540 100644 --- a/src/lib-dict-extra/dict-fs.c +++ b/src/lib-dict-extra/dict-fs.c @@ -69,9 +69,9 @@ static void fs_dict_deinit(struct dict *_dict) static const char *fs_dict_get_full_key(struct fs_dict *dict, const char *key) { - if (strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) == 0) + if (str_begins(key, DICT_PATH_SHARED)) return key + strlen(DICT_PATH_SHARED); - else if (strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) == 0) { + else if (str_begins(key, DICT_PATH_PRIVATE)) { return t_strdup_printf("%s/%s", dict->username, key + strlen(DICT_PATH_PRIVATE)); } else { diff --git a/src/lib-dict/dict-memcached-ascii.c b/src/lib-dict/dict-memcached-ascii.c index 30e4f531da..4936fa868f 100644 --- a/src/lib-dict/dict-memcached-ascii.c +++ b/src/lib-dict/dict-memcached-ascii.c @@ -501,9 +501,9 @@ static const char * memcached_ascii_dict_get_full_key(struct memcached_ascii_dict *dict, const char *key) { - if (strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) == 0) + if (str_begins(key, DICT_PATH_SHARED)) key += strlen(DICT_PATH_SHARED); - else if (strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) == 0) { + else if (str_begins(key, DICT_PATH_PRIVATE)) { key = t_strdup_printf("%s%c%s", dict->username, DICT_USERNAME_SEPARATOR, key + strlen(DICT_PATH_PRIVATE)); diff --git a/src/lib-dict/dict-memcached.c b/src/lib-dict/dict-memcached.c index cea4a33b69..1d4cc65a26 100644 --- a/src/lib-dict/dict-memcached.c +++ b/src/lib-dict/dict-memcached.c @@ -280,7 +280,7 @@ memcached_dict_lookup(struct dict *_dict, pool_t pool, const char *key, struct timeout *to; size_t key_len; - if (strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) == 0) + if (str_begins(key, DICT_PATH_SHARED)) key += strlen(DICT_PATH_SHARED); else { *error_r = t_strdup_printf("memcached: Only shared keys supported currently"); diff --git a/src/lib-dict/dict-redis.c b/src/lib-dict/dict-redis.c index 97dc40e65a..f554ee5e1e 100644 --- a/src/lib-dict/dict-redis.c +++ b/src/lib-dict/dict-redis.c @@ -487,9 +487,9 @@ static void redis_dict_lookup_timeout(struct redis_dict *dict) static const char * redis_dict_get_full_key(struct redis_dict *dict, const char *key) { - if (strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) == 0) + if (str_begins(key, DICT_PATH_SHARED)) key += strlen(DICT_PATH_SHARED); - else if (strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) == 0) { + else if (str_begins(key, DICT_PATH_PRIVATE)) { key = t_strdup_printf("%s%c%s", dict->username, DICT_USERNAME_SEPARATOR, key + strlen(DICT_PATH_PRIVATE)); diff --git a/src/lib-dict/dict.c b/src/lib-dict/dict.c index ed2ee1dcd5..40f55aaa55 100644 --- a/src/lib-dict/dict.c +++ b/src/lib-dict/dict.c @@ -117,8 +117,8 @@ bool dict_switch_ioloop(struct dict *dict) static bool dict_key_prefix_is_valid(const char *key) { - return strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) == 0 || - strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) == 0; + return str_begins(key, DICT_PATH_SHARED) || + str_begins(key, DICT_PATH_PRIVATE); } int dict_lookup(struct dict *dict, pool_t pool, const char *key, diff --git a/src/lib-fs/fs-metawrap.c b/src/lib-fs/fs-metawrap.c index 8a667a20a1..73b69dadd0 100644 --- a/src/lib-fs/fs-metawrap.c +++ b/src/lib-fs/fs-metawrap.c @@ -277,8 +277,7 @@ fs_metawrap_append_metadata(struct metawrap_fs_file *file, string_t *str) const struct fs_metadata *metadata; array_foreach(&file->file.metadata, metadata) { - if (strncmp(metadata->key, FS_METADATA_INTERNAL_PREFIX, - strlen(FS_METADATA_INTERNAL_PREFIX)) == 0) + if (str_begins(metadata->key, FS_METADATA_INTERNAL_PREFIX)) continue; str_append_tabescaped(str, metadata->key); diff --git a/src/lib-fs/fs-posix.c b/src/lib-fs/fs-posix.c index 34488f9afa..81b4a875ae 100644 --- a/src/lib-fs/fs-posix.c +++ b/src/lib-fs/fs-posix.c @@ -222,7 +222,7 @@ static int fs_posix_rmdir_parents(struct posix_fs *fs, const char *path) while ((p = strrchr(path, '/')) != NULL) { path = t_strdup_until(path, p); if ((fs->root_path != NULL && strcmp(path, fs->root_path) == 0) || - (fs->path_prefix != NULL && strncmp(path, fs->path_prefix, strlen(path)) == 0)) + (fs->path_prefix != NULL && str_begins(fs->path_prefix, path))) break; if (rmdir(path) == 0) { /* success, continue to parent */ diff --git a/src/lib-imap-storage/imap-metadata.c b/src/lib-imap-storage/imap-metadata.c index e74045cc13..34025aa491 100644 --- a/src/lib-imap-storage/imap-metadata.c +++ b/src/lib-imap-storage/imap-metadata.c @@ -84,13 +84,11 @@ imap_metadata_entry2key(struct imap_metadata_transaction *imtrans, /* names are case-insensitive so we'll always lowercase them */ entry = t_str_lcase(entry); - if (strncmp(entry, IMAP_METADATA_PRIVATE_PREFIX, - strlen(IMAP_METADATA_PRIVATE_PREFIX)) == 0) { + if (str_begins(entry, IMAP_METADATA_PRIVATE_PREFIX)) { *key_r = entry + strlen(IMAP_METADATA_PRIVATE_PREFIX); *type_r = MAIL_ATTRIBUTE_TYPE_PRIVATE; } else { - i_assert(strncmp(entry, IMAP_METADATA_SHARED_PREFIX, - strlen(IMAP_METADATA_SHARED_PREFIX)) == 0); + i_assert(str_begins(entry, IMAP_METADATA_SHARED_PREFIX)); *key_r = entry + strlen(IMAP_METADATA_SHARED_PREFIX); *type_r = MAIL_ATTRIBUTE_TYPE_SHARED; } @@ -100,8 +98,7 @@ imap_metadata_entry2key(struct imap_metadata_transaction *imtrans, i_assert((*key_r)[0] == '/'); *key_r += 1; } - if (strncmp(*key_r, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT, - strlen(MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT)) == 0) { + if (str_begins(*key_r, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT)) { /* Dovecot's internal attribute (mailbox or server). don't allow accessing this. */ return FALSE; diff --git a/src/lib-storage/index/dbox-multi/mdbox-purge.c b/src/lib-storage/index/dbox-multi/mdbox-purge.c index 56e69e100b..696c63e5f0 100644 --- a/src/lib-storage/index/dbox-multi/mdbox-purge.c +++ b/src/lib-storage/index/dbox-multi/mdbox-purge.c @@ -526,8 +526,7 @@ static int mdbox_purge_get_primary_files(struct mdbox_purge_context *ctx) dir_len = str_len(path); for (errno = 0; (d = readdir(dir)) != NULL; errno = 0) { - if (strncmp(d->d_name, MDBOX_MAIL_FILE_PREFIX, - strlen(MDBOX_MAIL_FILE_PREFIX)) != 0) + if (!str_begins(d->d_name, MDBOX_MAIL_FILE_PREFIX)) continue; if (str_to_uint32(d->d_name + strlen(MDBOX_MAIL_FILE_PREFIX), &file_id) < 0) diff --git a/src/lib-storage/index/dbox-single/sdbox-sync-rebuild.c b/src/lib-storage/index/dbox-single/sdbox-sync-rebuild.c index 22944901fe..aa4832f15f 100644 --- a/src/lib-storage/index/dbox-single/sdbox-sync-rebuild.c +++ b/src/lib-storage/index/dbox-single/sdbox-sync-rebuild.c @@ -73,8 +73,7 @@ sdbox_sync_add_file(struct index_rebuild_context *ctx, uint32_t uid; int ret; - if (strncmp(fname, SDBOX_MAIL_FILE_PREFIX, - strlen(SDBOX_MAIL_FILE_PREFIX)) != 0) + if (!str_begins(fname, SDBOX_MAIL_FILE_PREFIX)) return 0; fname += strlen(SDBOX_MAIL_FILE_PREFIX); diff --git a/src/lib-storage/index/index-search-mime.c b/src/lib-storage/index/index-search-mime.c index 847a4ab7a7..d30b47749d 100644 --- a/src/lib-storage/index/index-search-mime.c +++ b/src/lib-storage/index/index-search-mime.c @@ -282,7 +282,7 @@ seach_arg_mime_filename_match(struct search_mimepart_context *mpctx, case SEARCH_MIME_FILENAME_CONTAINS: return (strstr(value, key) != NULL ? 1 : 0); case SEARCH_MIME_FILENAME_BEGINS: - return (strncmp(value, key, strlen(key)) == 0 ? 1 : 0); + return (str_begins(value, key) ? 1 : 0); case SEARCH_MIME_FILENAME_ENDS: vlen = strlen(value); alen = strlen(key); diff --git a/src/lib-storage/index/index-storage.c b/src/lib-storage/index/index-storage.c index 71103cb40e..9889edfafa 100644 --- a/src/lib-storage/index/index-storage.c +++ b/src/lib-storage/index/index-storage.c @@ -715,8 +715,7 @@ mailbox_delete_all_attributes(struct mailbox_transaction_context *t, iter = mailbox_attribute_iter_init(t->box, type, ""); while ((key = mailbox_attribute_iter_next(iter)) != NULL) { if (inbox && - strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER, - strlen(MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER)) == 0) + str_begins(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER)) continue; if (mailbox_attribute_unset(t, type, key) < 0) { @@ -1079,7 +1078,7 @@ int index_storage_set_subscribed(struct mailbox *box, bool set) subscription name */ subs_name = t_strconcat(list->ns->prefix, box->name, NULL); /* drop the common prefix (typically there isn't one) */ - i_assert(strncmp(ns->prefix, subs_name, strlen(ns->prefix)) == 0); + i_assert(str_begins(subs_name, ns->prefix)); subs_name += strlen(ns->prefix); list = ns->list; diff --git a/src/lib-storage/index/mbox/mbox-storage.c b/src/lib-storage/index/mbox/mbox-storage.c index 7f60907fa2..8cd8058416 100644 --- a/src/lib-storage/index/mbox/mbox-storage.c +++ b/src/lib-storage/index/mbox/mbox-storage.c @@ -437,7 +437,7 @@ static int mbox_mailbox_open_existing(struct mbox_mailbox *mbox) /var/mail and we want to allow privileged dotlocking */ rootdir = mailbox_list_get_root_forced(box->list, MAILBOX_LIST_PATH_TYPE_DIR); - if (strncmp(box_path, rootdir, strlen(rootdir)) != 0) + if (!str_begins(box_path, rootdir)) mbox->mbox_privileged_locking = TRUE; } if ((box->flags & MAILBOX_FLAG_KEEP_LOCKED) != 0) { diff --git a/src/lib-storage/list/mailbox-list-delete.c b/src/lib-storage/list/mailbox-list-delete.c index 05e973559b..6cfbd8b4ed 100644 --- a/src/lib-storage/list/mailbox-list-delete.c +++ b/src/lib-storage/list/mailbox-list-delete.c @@ -287,7 +287,7 @@ void mailbox_list_delete_until_root(struct mailbox_list *list, const char *path, } root_dir = mailbox_list_get_root_forced(list, type); - if (strncmp(path, root_dir, strlen(root_dir)) != 0) { + if (!str_begins(path, root_dir)) { /* mbox workaround: name=child/box, root_dir=mail/.imap/, path=mail/child/.imap/box. we'll want to try to delete the .imap/ part, but no further. */ diff --git a/src/lib-storage/list/mailbox-list-fs.c b/src/lib-storage/list/mailbox-list-fs.c index 0d34261521..c1aabb9ce1 100644 --- a/src/lib-storage/list/mailbox-list-fs.c +++ b/src/lib-storage/list/mailbox-list-fs.c @@ -335,7 +335,7 @@ static int fs_list_delete_dir(struct mailbox_list *list, const char *name) if (mailbox_list_get_path(list, child_name, MAILBOX_LIST_PATH_TYPE_INDEX, &child_path) > 0 && - strncmp(path, child_path, strlen(path)) == 0) { + str_begins(child_path, path)) { /* drop the "/child" part out. */ p = strrchr(child_path, '/'); if (rmdir(t_strdup_until(child_path, p)) == 0) { diff --git a/src/lib-storage/mail-storage-service.c b/src/lib-storage/mail-storage-service.c index 38eab63b60..ea00ede77e 100644 --- a/src/lib-storage/mail-storage-service.c +++ b/src/lib-storage/mail-storage-service.c @@ -218,7 +218,7 @@ static bool validate_chroot(const struct mail_user_settings *user_set, chroot_dirs = t_strsplit(user_set->valid_chroot_dirs, ":"); while (*chroot_dirs != NULL) { if (**chroot_dirs != '\0' && - strncmp(dir, *chroot_dirs, strlen(*chroot_dirs)) == 0) + str_begins(dir, *chroot_dirs)) return TRUE; chroot_dirs++; } diff --git a/src/lib-storage/mailbox-attribute.c b/src/lib-storage/mailbox-attribute.c index e0250569e8..43467b9816 100644 --- a/src/lib-storage/mailbox-attribute.c +++ b/src/lib-storage/mailbox-attribute.c @@ -110,7 +110,7 @@ mailbox_internal_attribute_get(enum mail_attribute_type type, return NULL; } iattr = array_idx(&mailbox_internal_attributes, insert_idx-1); - if (strncmp(iattr->key, key, strlen(iattr->key)) != 0) { + if (!str_begins(key, iattr->key)) { /* iattr isn't a prefix of key */ return NULL; } else if ((iattr->flags & MAIL_ATTRIBUTE_INTERNAL_FLAG_CHILDREN) != 0) { @@ -187,8 +187,7 @@ mailbox_attribute_set_common(struct mailbox_transaction_context *t, /* allow internal server attribute only for inbox */ if (iattr != NULL && !t->box->inbox_any && - strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER, - strlen(MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER)) == 0) + str_begins(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER)) iattr = NULL; /* handle internal attribute */ @@ -280,8 +279,7 @@ mailbox_attribute_get_common(struct mailbox *box, /* allow internal server attributes only for the inbox */ if (iattr != NULL && !box->inbox_user && - strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER, - strlen(MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER)) == 0) + str_begins(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER)) iattr = NULL; /* internal attribute */ diff --git a/src/lib-storage/mailbox-attribute.h b/src/lib-storage/mailbox-attribute.h index 2e042e0b63..8c7bc96c06 100644 --- a/src/lib-storage/mailbox-attribute.h +++ b/src/lib-storage/mailbox-attribute.h @@ -173,10 +173,8 @@ struct mailbox_transaction_context; /* User can get/set all non-pvt/ attributes and also pvt/server/ (but not pvt/server/pvt/) attributes. */ #define MAILBOX_ATTRIBUTE_KEY_IS_USER_ACCESSIBLE(key) \ - (strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT, \ - strlen(MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT)) != 0 || \ - (strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER, \ - strlen(MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER)) == 0 && \ + (!str_begins(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT) || \ + (str_begins(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER) && \ strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT, \ strlen(MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT_SERVER MAILBOX_ATTRIBUTE_PREFIX_DOVECOT_PVT)) != 0)) diff --git a/src/lib-storage/mailbox-list.c b/src/lib-storage/mailbox-list.c index e395f90778..39cb8852f1 100644 --- a/src/lib-storage/mailbox-list.c +++ b/src/lib-storage/mailbox-list.c @@ -1240,7 +1240,7 @@ int mailbox_list_try_mkdir_root(struct mailbox_list *list, const char *path, if (!mailbox_list_get_root_path(list, type, &root_dir)) i_unreached(); - i_assert(strncmp(root_dir, path, strlen(root_dir)) == 0); + i_assert(str_begins(path, root_dir)); if (strcmp(root_dir, path) != 0 && stat(root_dir, &st) == 0) { /* creating a subdirectory under an already existing root dir. use the root's permissions */ diff --git a/src/lib/mempool-alloconly.c b/src/lib/mempool-alloconly.c index 2b938d2d43..6d144ce7f3 100644 --- a/src/lib/mempool-alloconly.c +++ b/src/lib/mempool-alloconly.c @@ -141,7 +141,7 @@ pool_t pool_alloconly_create(const char *name ATTR_UNUSED, size_t size) new_apool = p_new(&apool.pool, struct alloconly_pool, 1); *new_apool = apool; #ifdef DEBUG - if (strncmp(name, MEMPOOL_GROWING, strlen(MEMPOOL_GROWING)) == 0 || + if (str_begins(name, MEMPOOL_GROWING) || getenv("DEBUG_SILENT") != NULL) { name += strlen(MEMPOOL_GROWING); new_apool->disable_warning = TRUE; diff --git a/src/lib/test-strfuncs.c b/src/lib/test-strfuncs.c index c8f2b2c68e..bd3309e520 100644 --- a/src/lib/test-strfuncs.c +++ b/src/lib/test-strfuncs.c @@ -400,7 +400,7 @@ test_str_match(void) /* This is just 2 ways of wording the same test, but that also sanity tests the match values above. */ test_assert_idx(str_begins(tests[i].s1, tests[i].s2) == - (strncmp(tests[i].s1, tests[i].s2, strlen(tests[i].s2)) == 0), i); + (str_begins(tests[i].s1, tests[i].s2)), i); test_assert_idx(str_begins(tests[i].s1, tests[i].s2) == (strlen(tests[i].s2) == tests[i].match), i); } diff --git a/src/master/main.c b/src/master/main.c index 4c04c7efac..19f06e1c33 100644 --- a/src/master/main.c +++ b/src/master/main.c @@ -81,7 +81,7 @@ void process_exec(const char *cmd) /* prefix with dovecot/ */ argv[0] = t_strdup_printf("%s/%s", services->set->instance_name, argv[0]); - if (strncmp(argv[0], PACKAGE, strlen(PACKAGE)) != 0) + if (!str_begins(argv[0], PACKAGE)) argv[0] = t_strconcat(PACKAGE"-", argv[0], NULL); execv_const(executable, argv); } diff --git a/src/plugins/acl/acl-api.c b/src/plugins/acl/acl-api.c index bee359606a..1e62adf7e5 100644 --- a/src/plugins/acl/acl-api.c +++ b/src/plugins/acl/acl-api.c @@ -518,18 +518,15 @@ bool acl_rights_has_nonowner_lookup_changes(const struct acl_rights *rights) int acl_identifier_parse(const char *line, struct acl_rights *rights) { - if (strncmp(line, ACL_ID_NAME_USER_PREFIX, - strlen(ACL_ID_NAME_USER_PREFIX)) == 0) { + if (str_begins(line, ACL_ID_NAME_USER_PREFIX)) { rights->id_type = ACL_ID_USER; rights->identifier = line + 5; } else if (strcmp(line, ACL_ID_NAME_OWNER) == 0) { rights->id_type = ACL_ID_OWNER; - } else if (strncmp(line, ACL_ID_NAME_GROUP_PREFIX, - strlen(ACL_ID_NAME_GROUP_PREFIX)) == 0) { + } else if (str_begins(line, ACL_ID_NAME_GROUP_PREFIX)) { rights->id_type = ACL_ID_GROUP; rights->identifier = line + 6; - } else if (strncmp(line, ACL_ID_NAME_GROUP_OVERRIDE_PREFIX, - strlen(ACL_ID_NAME_GROUP_OVERRIDE_PREFIX)) == 0) { + } else if (str_begins(line, ACL_ID_NAME_GROUP_OVERRIDE_PREFIX)) { rights->id_type = ACL_ID_GROUP_OVERRIDE; rights->identifier = line + 15; } else if (strcmp(line, ACL_ID_NAME_AUTHENTICATED) == 0) { diff --git a/src/plugins/acl/acl-attributes.c b/src/plugins/acl/acl-attributes.c index 8d2458e72a..2499a30f9c 100644 --- a/src/plugins/acl/acl-attributes.c +++ b/src/plugins/acl/acl-attributes.c @@ -142,8 +142,7 @@ int acl_attribute_set(struct mailbox_transaction_context *t, if (acl_have_attribute_rights(t->box) < 0) return -1; - if (strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_ACL, - strlen(MAILBOX_ATTRIBUTE_PREFIX_ACL)) == 0) + if (str_begins(key, MAILBOX_ATTRIBUTE_PREFIX_ACL)) return acl_attribute_update_acl(t, key, value); return abox->module_ctx.super.attribute_set(t, type, key, value); } @@ -156,8 +155,7 @@ int acl_attribute_get(struct mailbox *box, if (acl_have_attribute_rights(box) < 0) return -1; - if (strncmp(key, MAILBOX_ATTRIBUTE_PREFIX_ACL, - strlen(MAILBOX_ATTRIBUTE_PREFIX_ACL)) == 0) + if (str_begins(key, MAILBOX_ATTRIBUTE_PREFIX_ACL)) return acl_attribute_get_acl(box, key, value_r); return abox->module_ctx.super.attribute_get(box, type, key, value_r); } @@ -178,8 +176,7 @@ acl_attribute_iter_init(struct mailbox *box, enum mail_attribute_type type, attribute_iter_init(box, type, prefix); if (box->storage->user->dsyncing && type == MAIL_ATTRIBUTE_TYPE_SHARED && - strncmp(prefix, MAILBOX_ATTRIBUTE_PREFIX_ACL, - strlen(prefix)) == 0) { + str_begins(MAILBOX_ATTRIBUTE_PREFIX_ACL, prefix)) { aiter->acl_iter = acl_object_list_init(abox->aclobj); aiter->acl_name = str_new(default_pool, 128); str_append(aiter->acl_name, MAILBOX_ATTRIBUTE_PREFIX_ACL); diff --git a/src/plugins/imap-acl/imap-acl-plugin.c b/src/plugins/imap-acl/imap-acl-plugin.c index b0a8e0d99e..c4c3c0070a 100644 --- a/src/plugins/imap-acl/imap-acl-plugin.c +++ b/src/plugins/imap-acl/imap-acl-plugin.c @@ -443,8 +443,7 @@ imap_acl_identifier_parse(struct client_command_context *cmd, { struct mail_user *user = cmd->client->user; - if (strncmp(id, IMAP_ACL_GLOBAL_PREFIX, - strlen(IMAP_ACL_GLOBAL_PREFIX)) == 0) { + if (str_begins(id, IMAP_ACL_GLOBAL_PREFIX)) { *error_r = t_strdup_printf("Global ACLs can't be modified: %s", id); return -1; @@ -464,12 +463,10 @@ imap_acl_identifier_parse(struct client_command_context *cmd, rights->id_type = ACL_ID_AUTHENTICATED; } else if (strcmp(id, IMAP_ACL_OWNER) == 0) rights->id_type = ACL_ID_OWNER; - else if (strncmp(id, IMAP_ACL_GROUP_PREFIX, - strlen(IMAP_ACL_GROUP_PREFIX)) == 0) { + else if (str_begins(id, IMAP_ACL_GROUP_PREFIX)) { rights->id_type = ACL_ID_GROUP; rights->identifier = id + strlen(IMAP_ACL_GROUP_PREFIX); - } else if (strncmp(id, IMAP_ACL_GROUP_OVERRIDE_PREFIX, - strlen(IMAP_ACL_GROUP_OVERRIDE_PREFIX)) == 0) { + } else if (str_begins(id, IMAP_ACL_GROUP_OVERRIDE_PREFIX)) { rights->id_type = ACL_ID_GROUP_OVERRIDE; rights->identifier = id + strlen(IMAP_ACL_GROUP_OVERRIDE_PREFIX);