All of lore.kernel.org
 help / color / mirror / Atom feed
From: George Hu <integral@archlinux.org>
To: wqu@suse.com
Cc: dsterba@suse.com, integral@archlinux.org,
	linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	mason@kernel.org
Subject: [PATCH v2] btrfs: use strstarts() and str_has_prefix() for prefix checks
Date: Sat, 26 Sep 2026 20:22:17 +0800	[thread overview]
Message-ID: <20260926122217.22759-1-integral@archlinux.org> (raw)
In-Reply-To: <a7d68d73-3ba1-46d3-a6c9-5ee213e5b551@suse.com>

Replace strncmp() prefix checks with strstarts() when only a boolean result
is needed, and with str_has_prefix() when the prefix length is needed after
a successful match.

This makes prefix checks clearer and more concise.

Signed-off-by: George Hu <integral@archlinux.org>
---
V1 -> V2:
 - Avoid replacing strncmp() with strstarts() when the parameter is
   not NUL terminated
 - Revise subject

 fs/btrfs/super.c | 5 ++---
 fs/btrfs/xattr.c | 3 +--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index ddb620ac241b..287cdc45bef8 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -263,10 +263,9 @@ static const struct fs_parameter_spec btrfs_fs_parameters[] = {
 
 static bool btrfs_match_compress_type(const char *string, const char *type, bool may_have_level)
 {
-	const int len = strlen(type);
+	const size_t len = str_has_prefix(string, type);
 
-	return (strncmp(string, type, len) == 0) &&
-		((may_have_level && string[len] == ':') || string[len] == '\0');
+	return len && ((may_have_level && string[len] == ':') || string[len] == '\0');
 }
 
 static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
diff --git a/fs/btrfs/xattr.c b/fs/btrfs/xattr.c
index ab55d10bd71f..9011d8096e2d 100644
--- a/fs/btrfs/xattr.c
+++ b/fs/btrfs/xattr.c
@@ -239,8 +239,7 @@ int btrfs_setxattr_trans(struct inode *inode, const char *name,
 		 * block_rsv of the handle and trigger a warning for the start
 		 * case.
 		 */
-		ASSERT(strncmp(name, XATTR_SECURITY_PREFIX,
-			       XATTR_SECURITY_PREFIX_LEN) == 0);
+		ASSERT(strstarts(name, XATTR_SECURITY_PREFIX));
 		trans = current->journal_info;
 	}
 
-- 
2.55.0


  reply	other threads:[~2026-09-26 12:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-26 10:36 [PATCH] btrfs: use helper functions to check if a string has a given prefix George Hu
2026-09-26 11:03 ` Qu Wenruo
2026-09-26 12:22   ` George Hu [this message]
2026-09-26 21:22     ` [PATCH v2] btrfs: use strstarts() and str_has_prefix() for prefix checks Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260926122217.22759-1-integral@archlinux.org \
    --to=integral@archlinux.org \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=wqu@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.