linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf cpumap: Use scnprintf instead of snprintf
@ 2020-03-22 17:25 Christophe JAILLET
  2020-03-23 11:03 ` Dan Carpenter
  2020-03-23 15:11 ` David Laight
  0 siblings, 2 replies; 8+ messages in thread
From: Christophe JAILLET @ 2020-03-22 17:25 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, kan.liang, zhe.he, dzickus, jstancek
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET

'scnprintf' returns the number of characters written in the output buffer
excluding the trailing '\0', instead of the number of characters which
would be generated for the given input.

Both function return a number of characters, excluding the trailing '\0'.
So comparaison to check if it overflows, should be done against max_size-1.
Comparaison against max_size can never match.

Fixes: 7780c25bae59f ("perf tools: Allow ability to map cpus to nodes easily")
Fixes: a24020e6b7cf6 ("perf tools: Change cpu_map__fprintf output")
Fixes: 92a7e1278005b ("perf cpumap: Add cpu__max_present_cpu()")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 tools/perf/util/cpumap.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
index 983b7388f22b..b87e7ef4d130 100644
--- a/tools/perf/util/cpumap.c
+++ b/tools/perf/util/cpumap.c
@@ -316,8 +316,8 @@ static void set_max_cpu_num(void)
 		goto out;
 
 	/* get the highest possible cpu number for a sparse allocation */
-	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
-	if (ret == PATH_MAX) {
+	ret = scnprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
+	if (ret == PATH_MAX-1) {
 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
 		goto out;
 	}
@@ -327,8 +327,8 @@ static void set_max_cpu_num(void)
 		goto out;
 
 	/* get the highest present cpu number for a sparse allocation */
-	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
-	if (ret == PATH_MAX) {
+	ret = scnprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
+	if (ret == PATH_MAX-1) {
 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
 		goto out;
 	}
@@ -355,8 +355,8 @@ static void set_max_node_num(void)
 		goto out;
 
 	/* get the highest possible cpu number for a sparse allocation */
-	ret = snprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
-	if (ret == PATH_MAX) {
+	ret = scnprintf(path, PATH_MAX, "%s/devices/system/node/possible", mnt);
+	if (ret == PATH_MAX-1) {
 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
 		goto out;
 	}
@@ -440,8 +440,8 @@ int cpu__setup_cpunode_map(void)
 	if (!mnt)
 		return 0;
 
-	n = snprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
-	if (n == PATH_MAX) {
+	n = scnprintf(path, PATH_MAX, "%s/devices/system/node", mnt);
+	if (n == PATH_MAX-1) {
 		pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
 		return -1;
 	}
@@ -455,8 +455,8 @@ int cpu__setup_cpunode_map(void)
 		if (dent1->d_type != DT_DIR || sscanf(dent1->d_name, "node%u", &mem) < 1)
 			continue;
 
-		n = snprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
-		if (n == PATH_MAX) {
+		n = scnprintf(buf, PATH_MAX, "%s/%s", path, dent1->d_name);
+		if (n == PATH_MAX-1) {
 			pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
 			continue;
 		}
@@ -501,21 +501,22 @@ size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
 		if (start == -1) {
 			start = i;
 			if (last) {
-				ret += snprintf(buf + ret, size - ret,
-						"%s%d", COMMA,
-						map->map[i]);
+				ret += scnprintf(buf + ret, size - ret,
+						 "%s%d", COMMA,
+						 map->map[i]);
 			}
 		} else if (((i - start) != (cpu - map->map[start])) || last) {
 			int end = i - 1;
 
 			if (start == end) {
-				ret += snprintf(buf + ret, size - ret,
-						"%s%d", COMMA,
-						map->map[start]);
+				ret += scnprintf(buf + ret, size - ret,
+						 "%s%d", COMMA,
+						 map->map[start]);
 			} else {
-				ret += snprintf(buf + ret, size - ret,
-						"%s%d-%d", COMMA,
-						map->map[start], map->map[end]);
+				ret += scnprintf(buf + ret, size - ret,
+						 "%s%d-%d", COMMA,
+						 map->map[start],
+						 map->map[end]);
 			}
 			first = false;
 			start = i;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] perf cpumap: Use scnprintf instead of snprintf
  2020-03-22 17:25 [PATCH] perf cpumap: Use scnprintf instead of snprintf Christophe JAILLET
@ 2020-03-23 11:03 ` Dan Carpenter
  2020-03-23 12:08   ` Christophe JAILLET
  2020-03-23 15:11 ` David Laight
  1 sibling, 1 reply; 8+ messages in thread
From: Dan Carpenter @ 2020-03-23 11:03 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, kan.liang, zhe.he, dzickus, jstancek, linux-kernel,
	kernel-janitors

On Sun, Mar 22, 2020 at 06:25:23PM +0100, Christophe JAILLET wrote:
> 'scnprintf' returns the number of characters written in the output buffer
> excluding the trailing '\0', instead of the number of characters which
> would be generated for the given input.
> 
> Both function return a number of characters, excluding the trailing '\0'.
> So comparaison to check if it overflows, should be done against max_size-1.
> Comparaison against max_size can never match.
> 
> Fixes: 7780c25bae59f ("perf tools: Allow ability to map cpus to nodes easily")
> Fixes: a24020e6b7cf6 ("perf tools: Change cpu_map__fprintf output")
> Fixes: 92a7e1278005b ("perf cpumap: Add cpu__max_present_cpu()")
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
>  tools/perf/util/cpumap.c | 39 ++++++++++++++++++++-------------------
>  1 file changed, 20 insertions(+), 19 deletions(-)
> 
> diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
> index 983b7388f22b..b87e7ef4d130 100644
> --- a/tools/perf/util/cpumap.c
> +++ b/tools/perf/util/cpumap.c
> @@ -316,8 +316,8 @@ static void set_max_cpu_num(void)
>  		goto out;
>  
>  	/* get the highest possible cpu number for a sparse allocation */
> -	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
> -	if (ret == PATH_MAX) {
> +	ret = scnprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
> +	if (ret == PATH_MAX-1) {

This should be a static analysis warning.

But isn't this stuff userspace?  I can't figure out how to compile it on
Debian so I'm not sure.  There is no scnprintf() in user space.

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] perf cpumap: Use scnprintf instead of snprintf
  2020-03-23 11:03 ` Dan Carpenter
@ 2020-03-23 12:08   ` Christophe JAILLET
  2020-03-23 12:08     ` Christophe JAILLET
  2020-03-23 12:16     ` Dan Carpenter
  0 siblings, 2 replies; 8+ messages in thread
From: Christophe JAILLET @ 2020-03-23 12:08 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, kan.liang, zhe.he, dzickus, jstancek, linux-kernel,
	kernel-janitors

Le 23/03/2020 à 12:03, Dan Carpenter a écrit :
> On Sun, Mar 22, 2020 at 06:25:23PM +0100, Christophe JAILLET wrote:
>> 'scnprintf' returns the number of characters written in the output buffer
>> excluding the trailing '\0', instead of the number of characters which
>> would be generated for the given input.
>>
>> Both function return a number of characters, excluding the trailing '\0'.
>> So comparaison to check if it overflows, should be done against max_size-1.
>> Comparaison against max_size can never match.
>>
>> Fixes: 7780c25bae59f ("perf tools: Allow ability to map cpus to nodes easily")
>> Fixes: a24020e6b7cf6 ("perf tools: Change cpu_map__fprintf output")
>> Fixes: 92a7e1278005b ("perf cpumap: Add cpu__max_present_cpu()")
>> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
>> ---
>>   tools/perf/util/cpumap.c | 39 ++++++++++++++++++++-------------------
>>   1 file changed, 20 insertions(+), 19 deletions(-)
>>
>> diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
>> index 983b7388f22b..b87e7ef4d130 100644
>> --- a/tools/perf/util/cpumap.c
>> +++ b/tools/perf/util/cpumap.c
>> @@ -316,8 +316,8 @@ static void set_max_cpu_num(void)
>>   		goto out;
>>   
>>   	/* get the highest possible cpu number for a sparse allocation */
>> -	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
>> -	if (ret == PATH_MAX) {
>> +	ret = scnprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
>> +	if (ret == PATH_MAX-1) {
> This should be a static analysis warning.
>
> But isn't this stuff userspace?  I can't figure out how to compile it on
> Debian so I'm not sure.  There is no scnprintf() in user space.
>
> regards,
> dan carpenter

I compiled it with:

     make tools/perf

the cpumap.o is generated and if I introduce an error, 
'scn<SPACE>printf' for example, gcc triggers a built error.

I though it was enough to validate the patch, before sending it.


Anyway, keeping 'snprintf' could be better to check for the overflow, 
but 'if (ret == PATH_MAX)' should be turned in 'if (ret >= PATH_MAX)'.
If agreed, I can send a V2.

CJ


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] perf cpumap: Use scnprintf instead of snprintf
  2020-03-23 12:08   ` Christophe JAILLET
@ 2020-03-23 12:08     ` Christophe JAILLET
  2020-03-23 12:16     ` Dan Carpenter
  1 sibling, 0 replies; 8+ messages in thread
From: Christophe JAILLET @ 2020-03-23 12:08 UTC (permalink / raw)
  To: linux-kernel; +Cc: kernel-janitors

Le 23/03/2020 à 12:03, Dan Carpenter a écrit :
> On Sun, Mar 22, 2020 at 06:25:23PM +0100, Christophe JAILLET wrote:
>> 'scnprintf' returns the number of characters written in the output buffer
>> excluding the trailing '\0', instead of the number of characters which
>> would be generated for the given input.
>>
>> Both function return a number of characters, excluding the trailing '\0'.
>> So comparaison to check if it overflows, should be done against max_size-1.
>> Comparaison against max_size can never match.
>>
>> Fixes: 7780c25bae59f ("perf tools: Allow ability to map cpus to nodes easily")
>> Fixes: a24020e6b7cf6 ("perf tools: Change cpu_map__fprintf output")
>> Fixes: 92a7e1278005b ("perf cpumap: Add cpu__max_present_cpu()")
>> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
>> ---
>>   tools/perf/util/cpumap.c | 39 ++++++++++++++++++++-------------------
>>   1 file changed, 20 insertions(+), 19 deletions(-)
>>
>> diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
>> index 983b7388f22b..b87e7ef4d130 100644
>> --- a/tools/perf/util/cpumap.c
>> +++ b/tools/perf/util/cpumap.c
>> @@ -316,8 +316,8 @@ static void set_max_cpu_num(void)
>>   		goto out;
>>   
>>   	/* get the highest possible cpu number for a sparse allocation */
>> -	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
>> -	if (ret == PATH_MAX) {
>> +	ret = scnprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
>> +	if (ret == PATH_MAX-1) {
> This should be a static analysis warning.
>
> But isn't this stuff userspace?  I can't figure out how to compile it on
> Debian so I'm not sure.  There is no scnprintf() in user space.
>
> regards,
> dan carpenter

I compiled it with:

     make tools/perf

the cpumap.o is generated and if I introduce an error, 
'scn<SPACE>printf' for example, gcc triggers a built error.

I though it was enough to validate the patch, before sending it.


Anyway, keeping 'snprintf' could be better to check for the overflow, 
but 'if (ret == PATH_MAX)' should be turned in 'if (ret >= PATH_MAX)'.
If agreed, I can send a V2.

CJ



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] perf cpumap: Use scnprintf instead of snprintf
  2020-03-23 12:08   ` Christophe JAILLET
  2020-03-23 12:08     ` Christophe JAILLET
@ 2020-03-23 12:16     ` Dan Carpenter
  1 sibling, 0 replies; 8+ messages in thread
From: Dan Carpenter @ 2020-03-23 12:16 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, kan.liang, zhe.he, dzickus, jstancek, linux-kernel,
	kernel-janitors

On Mon, Mar 23, 2020 at 01:08:47PM +0100, Christophe JAILLET wrote:
> Le 23/03/2020 à 12:03, Dan Carpenter a écrit :
> > On Sun, Mar 22, 2020 at 06:25:23PM +0100, Christophe JAILLET wrote:
> > > 'scnprintf' returns the number of characters written in the output buffer
> > > excluding the trailing '\0', instead of the number of characters which
> > > would be generated for the given input.
> > > 
> > > Both function return a number of characters, excluding the trailing '\0'.
> > > So comparaison to check if it overflows, should be done against max_size-1.
> > > Comparaison against max_size can never match.
> > > 
> > > Fixes: 7780c25bae59f ("perf tools: Allow ability to map cpus to nodes easily")
> > > Fixes: a24020e6b7cf6 ("perf tools: Change cpu_map__fprintf output")
> > > Fixes: 92a7e1278005b ("perf cpumap: Add cpu__max_present_cpu()")
> > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> > > ---
> > >   tools/perf/util/cpumap.c | 39 ++++++++++++++++++++-------------------
> > >   1 file changed, 20 insertions(+), 19 deletions(-)
> > > 
> > > diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
> > > index 983b7388f22b..b87e7ef4d130 100644
> > > --- a/tools/perf/util/cpumap.c
> > > +++ b/tools/perf/util/cpumap.c
> > > @@ -316,8 +316,8 @@ static void set_max_cpu_num(void)
> > >   		goto out;
> > >   	/* get the highest possible cpu number for a sparse allocation */
> > > -	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
> > > -	if (ret == PATH_MAX) {
> > > +	ret = scnprintf(path, PATH_MAX, "%s/devices/system/cpu/possible", mnt);
> > > +	if (ret == PATH_MAX-1) {
> > This should be a static analysis warning.
> > 
> > But isn't this stuff userspace?  I can't figure out how to compile it on
> > Debian so I'm not sure.  There is no scnprintf() in user space.
> > 
> > regards,
> > dan carpenter
> 
> I compiled it with:
> 
>     make tools/perf
> 

Ah.  You're absolutely right.  My bad.  Sorry for that.

I was doing "cd tools/perf; make" and it told me to install glibc-dev.

regards,
dan carpenter



^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH] perf cpumap: Use scnprintf instead of snprintf
  2020-03-22 17:25 [PATCH] perf cpumap: Use scnprintf instead of snprintf Christophe JAILLET
  2020-03-23 11:03 ` Dan Carpenter
@ 2020-03-23 15:11 ` David Laight
  2020-03-23 15:20   ` Christophe JAILLET
  1 sibling, 1 reply; 8+ messages in thread
From: David Laight @ 2020-03-23 15:11 UTC (permalink / raw)
  To: 'Christophe JAILLET',
	peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, kan.liang, zhe.he, dzickus, jstancek
  Cc: linux-kernel, kernel-janitors

From: Christophe JAILLET
> Sent: 22 March 2020 17:25
> 'scnprintf' returns the number of characters written in the output buffer
> excluding the trailing '\0', instead of the number of characters which
> would be generated for the given input.
> 
> Both function return a number of characters, excluding the trailing '\0'.
> So comparaison to check if it overflows, should be done against max_size-1.
> Comparaison against max_size can never match.

NACK.
Since snprintf() returns the number of characters it would have
written to an infinite buffer the comparison can 'match'.

However it should test for (ret >= PATH_MAX).

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] perf cpumap: Use scnprintf instead of snprintf
  2020-03-23 15:11 ` David Laight
@ 2020-03-23 15:20   ` Christophe JAILLET
  2020-03-23 15:20     ` Christophe JAILLET
  0 siblings, 1 reply; 8+ messages in thread
From: Christophe JAILLET @ 2020-03-23 15:20 UTC (permalink / raw)
  To: David Laight, peterz, mingo, acme, mark.rutland,
	alexander.shishkin, jolsa, namhyung, kan.liang, zhe.he, dzickus,
	jstancek
  Cc: linux-kernel, kernel-janitors

Le 23/03/2020 à 16:11, David Laight a écrit :
> From: Christophe JAILLET
>> Sent: 22 March 2020 17:25
>> 'scnprintf' returns the number of characters written in the output buffer
>> excluding the trailing '\0', instead of the number of characters which
>> would be generated for the given input.
>>
>> Both function return a number of characters, excluding the trailing '\0'.
>> So comparaison to check if it overflows, should be done against max_size-1.
>> Comparaison against max_size can never match.
> NACK.
> Since snprintf() returns the number of characters it would have
> written to an infinite buffer the comparison can 'match'.
>
> However it should test for (ret >= PATH_MAX).

Agreed. I'll send a V2.

CJ

> 	David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
>
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] perf cpumap: Use scnprintf instead of snprintf
  2020-03-23 15:20   ` Christophe JAILLET
@ 2020-03-23 15:20     ` Christophe JAILLET
  0 siblings, 0 replies; 8+ messages in thread
From: Christophe JAILLET @ 2020-03-23 15:20 UTC (permalink / raw)
  To: linux-kernel; +Cc: kernel-janitors

Le 23/03/2020 à 16:11, David Laight a écrit :
> From: Christophe JAILLET
>> Sent: 22 March 2020 17:25
>> 'scnprintf' returns the number of characters written in the output buffer
>> excluding the trailing '\0', instead of the number of characters which
>> would be generated for the given input.
>>
>> Both function return a number of characters, excluding the trailing '\0'.
>> So comparaison to check if it overflows, should be done against max_size-1.
>> Comparaison against max_size can never match.
> NACK.
> Since snprintf() returns the number of characters it would have
> written to an infinite buffer the comparison can 'match'.
>
> However it should test for (ret >= PATH_MAX).

Agreed. I'll send a V2.

CJ

> 	David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
>
>



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-03-23 15:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-22 17:25 [PATCH] perf cpumap: Use scnprintf instead of snprintf Christophe JAILLET
2020-03-23 11:03 ` Dan Carpenter
2020-03-23 12:08   ` Christophe JAILLET
2020-03-23 12:08     ` Christophe JAILLET
2020-03-23 12:16     ` Dan Carpenter
2020-03-23 15:11 ` David Laight
2020-03-23 15:20   ` Christophe JAILLET
2020-03-23 15:20     ` Christophe JAILLET

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).