2020-05-09 08:33:39

by chenzhou

[permalink] [raw]
Subject: [PATCH -next] arch/arm: use scnprintf() in l2x0_pmu_event_show()

snprintf() returns the number of bytes that would be written,
which may be greater than the the actual length to be written.

show() methods should return the number of bytes printed into the
buffer. This is the return value of scnprintf().

Signed-off-by: Chen Zhou <[email protected]>
---
arch/arm/mm/cache-l2x0-pmu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm/mm/cache-l2x0-pmu.c b/arch/arm/mm/cache-l2x0-pmu.c
index 993fefdc167a..d20626451a2e 100644
--- a/arch/arm/mm/cache-l2x0-pmu.c
+++ b/arch/arm/mm/cache-l2x0-pmu.c
@@ -343,7 +343,7 @@ static ssize_t l2x0_pmu_event_show(struct device *dev,
struct l2x0_event_attribute *lattr;

lattr = container_of(attr, typeof(*lattr), attr);
- return snprintf(buf, PAGE_SIZE, "config=0x%x\n", lattr->config);
+ return scnprintf(buf, PAGE_SIZE, "config=0x%x\n", lattr->config);
}

static umode_t l2x0_pmu_event_attr_is_visible(struct kobject *kobj,
--
2.20.1


2020-05-10 06:16:28

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH -next] arch/arm: use scnprintf() in l2x0_pmu_event_show()

On Sat, May 09, 2020 at 04:35:39PM +0800, Chen Zhou wrote:
> snprintf() returns the number of bytes that would be written,
> which may be greater than the the actual length to be written.
>
> show() methods should return the number of bytes printed into the
> buffer. This is the return value of scnprintf().
>
> Signed-off-by: Chen Zhou <[email protected]>
> ---
> arch/arm/mm/cache-l2x0-pmu.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/arm/mm/cache-l2x0-pmu.c b/arch/arm/mm/cache-l2x0-pmu.c
> index 993fefdc167a..d20626451a2e 100644
> --- a/arch/arm/mm/cache-l2x0-pmu.c
> +++ b/arch/arm/mm/cache-l2x0-pmu.c
> @@ -343,7 +343,7 @@ static ssize_t l2x0_pmu_event_show(struct device *dev,
> struct l2x0_event_attribute *lattr;
>
> lattr = container_of(attr, typeof(*lattr), attr);
> - return snprintf(buf, PAGE_SIZE, "config=0x%x\n", lattr->config);
> + return scnprintf(buf, PAGE_SIZE, "config=0x%x\n", lattr->config);

This should just be replaced with sprintf() as there's no way such a
string can go beyond PAGE_SIZE.

And the "config=" part could be dropped as well, but now userspace
probably requires it, that is sad.

thanks,

greg k-h