[PATCH v5 05/32] lib/string_helpers: string_get_size() now returns characters wrote

Matthew Wilcox (Oracle) posted 32 patches 3 years, 8 months ago
[PATCH v5 05/32] lib/string_helpers: string_get_size() now returns characters wrote
Posted by Matthew Wilcox (Oracle) 3 years, 8 months ago
From: Kent Overstreet <kent.overstreet@gmail.com>

printbuf now needs to know the number of characters that would have been
written if the buffer was too small, like snprintf(); this changes
string_get_size() to return the the return value of snprintf().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Cc: Andy Shevchenko <andy@kernel.org>
---
 include/linux/string_helpers.h | 4 ++--
 lib/string_helpers.c           | 7 +++----
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 67de398944e8..52e0f1d283b9 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -19,8 +19,8 @@ enum string_size_units {
 	STRING_UNITS_2,		/* use binary powers of 2^10 */
 };
 
-void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
-		     char *buf, int len);
+int string_get_size(u64 size, u64 blk_size, enum string_size_units units,
+		    char *buf, int len);
 
 #define UNESCAPE_SPACE		BIT(0)
 #define UNESCAPE_OCTAL		BIT(1)
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 7be20bcc6137..d247bf945f16 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -33,8 +33,8 @@
  * at least 9 bytes and will always be zero terminated.
  *
  */
-void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
-		     char *buf, int len)
+int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
+		    char *buf, int len)
 {
 	static const char *const units_10[] = {
 		"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
@@ -127,8 +127,7 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
 	else
 		unit = units_str[units][i];
 
-	snprintf(buf, len, "%u%s %s", (u32)size,
-		 tmp, unit);
+	return snprintf(buf, len, "%u%s %s", (u32)size, tmp, unit);
 }
 EXPORT_SYMBOL(string_get_size);
 
-- 
2.35.1
Re: [PATCH v5 05/32] lib/string_helpers: string_get_size() now returns characters wrote
Posted by Andy Shevchenko 3 years, 8 months ago
On Mon, Aug 8, 2022 at 4:41 AM Matthew Wilcox (Oracle)
<willy@infradead.org> wrote:
>
> From: Kent Overstreet <kent.overstreet@gmail.com>
>
> printbuf now needs to know the number of characters that would have been
> written if the buffer was too small, like snprintf(); this changes
> string_get_size() to return the the return value of snprintf().

the the --> the

Acked-by: Andy Shevchenko <andy@kernel.org>

> Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
> Cc: Andy Shevchenko <andy@kernel.org>
> ---
>  include/linux/string_helpers.h | 4 ++--
>  lib/string_helpers.c           | 7 +++----
>  2 files changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
> index 67de398944e8..52e0f1d283b9 100644
> --- a/include/linux/string_helpers.h
> +++ b/include/linux/string_helpers.h
> @@ -19,8 +19,8 @@ enum string_size_units {
>         STRING_UNITS_2,         /* use binary powers of 2^10 */
>  };
>
> -void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
> -                    char *buf, int len);
> +int string_get_size(u64 size, u64 blk_size, enum string_size_units units,
> +                   char *buf, int len);
>
>  #define UNESCAPE_SPACE         BIT(0)
>  #define UNESCAPE_OCTAL         BIT(1)
> diff --git a/lib/string_helpers.c b/lib/string_helpers.c
> index 7be20bcc6137..d247bf945f16 100644
> --- a/lib/string_helpers.c
> +++ b/lib/string_helpers.c
> @@ -33,8 +33,8 @@
>   * at least 9 bytes and will always be zero terminated.
>   *
>   */
> -void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
> -                    char *buf, int len)
> +int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
> +                   char *buf, int len)
>  {
>         static const char *const units_10[] = {
>                 "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
> @@ -127,8 +127,7 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
>         else
>                 unit = units_str[units][i];
>
> -       snprintf(buf, len, "%u%s %s", (u32)size,
> -                tmp, unit);
> +       return snprintf(buf, len, "%u%s %s", (u32)size, tmp, unit);
>  }
>  EXPORT_SYMBOL(string_get_size);
>
> --
> 2.35.1
>


-- 
With Best Regards,
Andy Shevchenko