[PATCH] efi/cper: Fix cper_bits_to_str buffer handling and return value

Morduan Zang posted 1 patch 3 weeks, 4 days ago
drivers/firmware/efi/cper.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] efi/cper: Fix cper_bits_to_str buffer handling and return value
Posted by Morduan Zang 3 weeks, 4 days ago
The return value calculation was incorrect:
    `return len - buf_size;`
    Initially `len = buf_size`, then `len` decreases with each operation.
    This results in a negative return value on success.

Fix by returning `buf_size - len` which correctly calculates the
    actual number of bytes written.

Fixes: a976d790f494 ("efi/cper: Add a new helper function to print bitmasks")
Signed-off-by: Morduan Zang <zhangdandan@uniontech.com>
---
 drivers/firmware/efi/cper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/cper.c b/drivers/firmware/efi/cper.c
index 0232bd040f61..bd99802cb0ca 100644
--- a/drivers/firmware/efi/cper.c
+++ b/drivers/firmware/efi/cper.c
@@ -162,7 +162,7 @@ int cper_bits_to_str(char *buf, int buf_size, unsigned long bits,
 		len -= size;
 		str += size;
 	}
-	return len - buf_size;
+	return buf_size - len;
 }
 EXPORT_SYMBOL_GPL(cper_bits_to_str);
 
-- 
2.50.1
Re: [PATCH] efi/cper: Fix cper_bits_to_str buffer handling and return value
Posted by Ard Biesheuvel 3 weeks, 4 days ago
On Wed, 14 Jan 2026 at 06:32, Morduan Zang <zhangdandan@uniontech.com> wrote:
>
> The return value calculation was incorrect:
>     `return len - buf_size;`
>     Initially `len = buf_size`, then `len` decreases with each operation.
>     This results in a negative return value on success.
>
> Fix by returning `buf_size - len` which correctly calculates the
>     actual number of bytes written.
>
> Fixes: a976d790f494 ("efi/cper: Add a new helper function to print bitmasks")
> Signed-off-by: Morduan Zang <zhangdandan@uniontech.com>
> ---
>  drivers/firmware/efi/cper.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/firmware/efi/cper.c b/drivers/firmware/efi/cper.c
> index 0232bd040f61..bd99802cb0ca 100644
> --- a/drivers/firmware/efi/cper.c
> +++ b/drivers/firmware/efi/cper.c
> @@ -162,7 +162,7 @@ int cper_bits_to_str(char *buf, int buf_size, unsigned long bits,
>                 len -= size;
>                 str += size;
>         }
> -       return len - buf_size;
> +       return buf_size - len;
>  }
>  EXPORT_SYMBOL_GPL(cper_bits_to_str);
>

Thanks for the fix. Queued up in efi/urgent.