Skip to contentSkip to conversation

btrfs: use helper functions to check if a string has a given prefix

PATCH In review George Hu 2 participants 4 messages Sep 26 Btrfs file system

lore
Share & tools
lore

George Hu

author
#0 lore raw

PATCH 1/1 btrfs: use helper functions to check if a string has a given prefix

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.

---
 fs/btrfs/compression.c | 15 ++++-----------
 fs/btrfs/compression.h |  2 +-
 fs/btrfs/props.c       |  2 +-
 fs/btrfs/super.c       |  5 ++---
 fs/btrfs/xattr.c       |  3 +--
 5 files changed, 9 insertions(+), 18 deletions(-)
fs/btrfs/compression.c +4 -11
index c62b5148d5ac..82d0413e6236 100644
@@ -70,19 +70,12 @@ static struct compressed_bio *alloc_compressed_bio(struct btrfs_inode *inode,
 	return to_compressed_bio(bbio);
 }
 
-bool btrfs_compress_is_valid_type(const char *str, size_t len)
+bool btrfs_compress_is_valid_type(const char *str)
 {
-	int i;
-
-	for (i = 1; i < ARRAY_SIZE(btrfs_compress_types); i++) {
-		size_t comp_len = strlen(btrfs_compress_types[i]);
-
-		if (len < comp_len)
-			continue;
-
-		if (!strncmp(btrfs_compress_types[i], str, comp_len))
+	for (int i = 1; i < ARRAY_SIZE(btrfs_compress_types); i++)
+		if (strstarts(str, btrfs_compress_types[i]))
 			return true;
-	}
+
 	return false;
 }
 
fs/btrfs/compression.h +1 -1
index 1022dc53ec51..1d8e12fcd488 100644
@@ -131,7 +131,7 @@ extern const struct btrfs_compress_levels btrfs_lzo_compress;
 extern const struct btrfs_compress_levels btrfs_zstd_compress;
 
 const char* btrfs_compress_type2str(enum btrfs_compression_type type);
-bool btrfs_compress_is_valid_type(const char *str, size_t len);
+bool btrfs_compress_is_valid_type(const char *str);
 
 int btrfs_compress_heuristic(struct btrfs_inode *inode, u64 start, u64 end);
 
fs/btrfs/props.c +1 -1
index bb77d46376d4..c33930c58802 100644
@@ -301,7 +301,7 @@ static int prop_compression_validate(const struct btrfs_inode *inode,
 	if (!value)
 		return 0;
 
-	if (btrfs_compress_is_valid_type(value, len))
+	if (btrfs_compress_is_valid_type(value))
 		return 0;
 
 	if ((len == 2 && strncmp("no", value, 2) == 0) ||
fs/btrfs/super.c +2 -3
index ddb620ac241b..287cdc45bef8 100644
@@ -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,
fs/btrfs/xattr.c +1 -2
index ab55d10bd71f..9011d8096e2d 100644
@@ -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;
 	}
 
Signature · 1 line
2.55.0
#1 lore raw
Quoted 27 lines · George Hu

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.

---
  fs/btrfs/compression.c | 15 ++++-----------
  fs/btrfs/compression.h |  2 +-
  fs/btrfs/props.c       |  2 +-
  fs/btrfs/super.c       |  5 ++---
  fs/btrfs/xattr.c       |  3 +--
  5 files changed, 9 insertions(+), 18 deletions(-)
fs/btrfs/compression.c +1 -2
index c62b5148d5ac..82d0413e6236 100644
@@ -70,19 +70,12 @@ static struct compressed_bio *alloc_compressed_bio(struct btrfs_inode *inode,
  	return to_compressed_bio(bbio);
  }
 
-bool btrfs_compress_is_valid_type(const char *str, size_t len)
+bool btrfs_compress_is_valid_type(const char *str)
  {
-	int i;

fs/btrfs/compression.c static struct compressed_bio *alloc_compressed_bio(struct btrfs_inode *inode, · line 76

Quoted 10 lines
fs/btrfs/compression.c +2 -8
-
-	for (i = 1; i < ARRAY_SIZE(btrfs_compress_types); i++) {
-		size_t comp_len = strlen(btrfs_compress_types[i]);
-
-		if (len < comp_len)
-			continue;
-
-		if (!strncmp(btrfs_compress_types[i], str, comp_len))
+	for (int i = 1; i < ARRAY_SIZE(btrfs_compress_types); i++)
+		if (strstarts(str, btrfs_compress_types[i]))

strstarts() is using strlen(btrfs_compress_types[i]) to do the string comparison, but the old code will skip the comparison completely, to avoid accessing memory after @len.

Since @str is not NUL terminated, this is very dangerous.

To be honest, I didn't see strstarts() provides any readability benefit unless the parameter is NUL terminated.

Quoted 63 lines
fs/btrfs/compression.c +1 -1
  			return true;
-	}
+
  	return false;
  }
 
fs/btrfs/compression.h +1 -1
index 1022dc53ec51..1d8e12fcd488 100644
@@ -131,7 +131,7 @@ extern const struct btrfs_compress_levels btrfs_lzo_compress;
  extern const struct btrfs_compress_levels btrfs_zstd_compress;
 
  const char* btrfs_compress_type2str(enum btrfs_compression_type type);
-bool btrfs_compress_is_valid_type(const char *str, size_t len);
+bool btrfs_compress_is_valid_type(const char *str);
 
  int btrfs_compress_heuristic(struct btrfs_inode *inode, u64 start, u64 end);
 
fs/btrfs/props.c +1 -1
index bb77d46376d4..c33930c58802 100644
@@ -301,7 +301,7 @@ static int prop_compression_validate(const struct btrfs_inode *inode,
  	if (!value)
  		return 0;
 
-	if (btrfs_compress_is_valid_type(value, len))
+	if (btrfs_compress_is_valid_type(value))
  		return 0;
 
  	if ((len == 2 && strncmp("no", value, 2) == 0) ||
fs/btrfs/super.c +2 -3
index ddb620ac241b..287cdc45bef8 100644
@@ -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,
fs/btrfs/xattr.c +1 -2
index ab55d10bd71f..9011d8096e2d 100644
@@ -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;
  	}
 
author
#2 lore raw

PATCH v2 1/1 btrfs: use strstarts() and str_has_prefix() for prefix checks

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.

---
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(-)
fs/btrfs/super.c +2 -3
index ddb620ac241b..287cdc45bef8 100644
@@ -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,
fs/btrfs/xattr.c +1 -2
index ab55d10bd71f..9011d8096e2d 100644
@@ -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;
 	}
 
Signature · 1 line
2.55.0
#3 lore raw

Re: [PATCH v2] btrfs: use strstarts() and str_has_prefix() for prefix checks

Quoted 22 lines · George Hu

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.

---
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(-)
fs/btrfs/super.c +0 -0
index ddb620ac241b..287cdc45bef8 100644

fs/btrfs/super.c static const struct fs_parameter_spec btrfs_fs_parameters[] = { · line 268

Quoted 9 lines
fs/btrfs/super.c +2 -3
@@ -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');

For this call site, both strings are NUL terminated so there is no problem about accessing beyond boundary.

But to be honest, I don't think this improves the readability.

I still have to check what str_has_prefix() really returns to understand the code, as the function is not a common helper and just by the name I don't get what if it will return.

Quoted 8 lines
fs/btrfs/super.c +0 -0
  }
 
  static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
fs/btrfs/xattr.c +0 -0
index ab55d10bd71f..9011d8096e2d 100644

fs/btrfs/xattr.c int btrfs_setxattr_trans(struct inode *inode, const char *name, · line 242

Quoted 6 lines
fs/btrfs/xattr.c +1 -2
@@ -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));

@name is not NUL-terminated, thus even the old code is not safe.

The only thing saving us here is that we should have a security XATTR passed in, thus the name must have the "security." prefix and not cause access-beyond-boundary.

Again, I do not think it's a good idea to use strstarts() with any non-NUL-terminated strings.

Quoted 3 lines
fs/btrfs/xattr.c +0 -0
  		trans = current->journal_info;
  	}