Thread (6 messages) 6 messages, 3 authors, 2021-08-26

[PATCH 2/3] btrfs-progs: slightly enhance btrfs_format_csum()

STALE1863d

From: Qu Wenruo <hidden>
Date: 2021-08-26 06:40:45
Subsystem: the rest · Maintainer: Linus Torvalds

- Change it void
  The old one always return csum_size.

- Use snprintf()

Signed-off-by: Qu Wenruo <redacted>
---
 common/utils.c | 14 ++++++++------
 common/utils.h |  2 +-
 2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/common/utils.c b/common/utils.c
index e944c43ac40e..7e213146520a 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -385,18 +385,20 @@ enum btrfs_csum_type parse_csum_type(const char *s)
 	return 0;
 }
 
-int btrfs_format_csum(u16 csum_type, const u8 *data, char *output)
+void btrfs_format_csum(u16 csum_type, const u8 *data, char *output)
 {
 	int i;
+	int cur = 0;
 	const int csum_size = btrfs_csum_type_size(csum_type);
 
-	sprintf(output, "0x");
+	output[0] = '\0';
+	snprintf(output, BTRFS_CSUM_STRING_LEN, "0x");
+	cur += strlen("0x");
 	for (i = 0; i < csum_size; i++) {
-		output += 2;
-		sprintf(output, "%02x", data[i]);
+		snprintf(output + cur, BTRFS_CSUM_STRING_LEN - cur, "%02x",
+			 data[i]);
+		cur += 2;
 	}
-
-	return csum_size;
 }
 
 int get_device_info(int fd, u64 devid,
diff --git a/common/utils.h b/common/utils.h
index 8d5e78071f16..a96bbce9d234 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -46,7 +46,7 @@ enum btrfs_csum_type parse_csum_type(const char *s);
 
 /* 2 for "0x", 2 for each byte, plus nul */
 #define BTRFS_CSUM_STRING_LEN	(2 + 2 * BTRFS_CSUM_SIZE + 1)
-int btrfs_format_csum(u16 csum_type, const u8 *data, char *output);
+void btrfs_format_csum(u16 csum_type, const u8 *data, char *output);
 u64 parse_size_from_string(const char *s);
 u64 parse_qgroupid(const char *p);
 u64 arg_strtou64(const char *str);
-- 
2.32.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help