netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2] tc: u32: check return value from snprintf
@ 2024-02-11  1:04 Stephen Hemminger
  2024-02-12  7:24 ` Hangbin Liu
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Stephen Hemminger @ 2024-02-11  1:04 UTC (permalink / raw)
  To: netdev; +Cc: liuhangbin, jhs, Stephen Hemminger

Add assertion to check for case of snprintf failing (bad format?)
or buffer getting full.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 tc/f_u32.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tc/f_u32.c b/tc/f_u32.c
index 913ec1de435d..8a2413103906 100644
--- a/tc/f_u32.c
+++ b/tc/f_u32.c
@@ -7,6 +7,7 @@
  *
  */
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -87,6 +88,7 @@ static char *sprint_u32_handle(__u32 handle, char *buf)
 	if (htid) {
 		int l = snprintf(b, bsize, "%x:", htid>>20);
 
+		assert(l > 0 && l < bsize);
 		bsize -= l;
 		b += l;
 	}
@@ -94,12 +96,14 @@ static char *sprint_u32_handle(__u32 handle, char *buf)
 		if (hash) {
 			int l = snprintf(b, bsize, "%x", hash);
 
+			assert(l > 0 && l < bsize);
 			bsize -= l;
 			b += l;
 		}
 		if (nodeid) {
 			int l = snprintf(b, bsize, ":%x", nodeid);
 
+			assert(l > 0 && l < bsize);
 			bsize -= l;
 			b += l;
 		}
-- 
2.43.0


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

* Re: [PATCH iproute2] tc: u32: check return value from snprintf
  2024-02-11  1:04 [PATCH iproute2] tc: u32: check return value from snprintf Stephen Hemminger
@ 2024-02-12  7:24 ` Hangbin Liu
  2024-02-12 16:53   ` Stephen Hemminger
  2024-02-13  6:25 ` Hangbin Liu
  2024-02-15  3:10 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 6+ messages in thread
From: Hangbin Liu @ 2024-02-12  7:24 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev, jhs

On Sat, Feb 10, 2024 at 05:04:23PM -0800, Stephen Hemminger wrote:
> Add assertion to check for case of snprintf failing (bad format?)
> or buffer getting full.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

Hi Stephen,

Is there a bug report or something else that we only do the assertion
for tc/f_u32.c?

Thanks
Hangbin

> ---
>  tc/f_u32.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/tc/f_u32.c b/tc/f_u32.c
> index 913ec1de435d..8a2413103906 100644
> --- a/tc/f_u32.c
> +++ b/tc/f_u32.c
> @@ -7,6 +7,7 @@
>   *
>   */
>  
> +#include <assert.h>
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <unistd.h>
> @@ -87,6 +88,7 @@ static char *sprint_u32_handle(__u32 handle, char *buf)
>  	if (htid) {
>  		int l = snprintf(b, bsize, "%x:", htid>>20);
>  
> +		assert(l > 0 && l < bsize);
>  		bsize -= l;
>  		b += l;
>  	}
> @@ -94,12 +96,14 @@ static char *sprint_u32_handle(__u32 handle, char *buf)
>  		if (hash) {
>  			int l = snprintf(b, bsize, "%x", hash);
>  
> +			assert(l > 0 && l < bsize);
>  			bsize -= l;
>  			b += l;
>  		}
>  		if (nodeid) {
>  			int l = snprintf(b, bsize, ":%x", nodeid);
>  
> +			assert(l > 0 && l < bsize);
>  			bsize -= l;
>  			b += l;
>  		}
> -- 
> 2.43.0
> 

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

* Re: [PATCH iproute2] tc: u32: check return value from snprintf
  2024-02-12  7:24 ` Hangbin Liu
@ 2024-02-12 16:53   ` Stephen Hemminger
  2024-02-16  9:58     ` David Laight
  0 siblings, 1 reply; 6+ messages in thread
From: Stephen Hemminger @ 2024-02-12 16:53 UTC (permalink / raw)
  To: Hangbin Liu; +Cc: netdev, jhs

On Mon, 12 Feb 2024 15:24:49 +0800
Hangbin Liu <liuhangbin@gmail.com> wrote:

> On Sat, Feb 10, 2024 at 05:04:23PM -0800, Stephen Hemminger wrote:
> > Add assertion to check for case of snprintf failing (bad format?)
> > or buffer getting full.
> > 
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>  
> 
> Hi Stephen,
> 
> Is there a bug report or something else that we only do the assertion
> for tc/f_u32.c?
> 
> Thanks
> Hangbin

No bug, it is not possible to trigger with current code.
Return of < 0 only happens with improper format string,
and the overrun would only happen if buffer was not big enough
The bsize is SPRINT_BUF() which is 64 bytes.

It is more a way to avoid some code checker complaining in future.

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

* Re: [PATCH iproute2] tc: u32: check return value from snprintf
  2024-02-11  1:04 [PATCH iproute2] tc: u32: check return value from snprintf Stephen Hemminger
  2024-02-12  7:24 ` Hangbin Liu
@ 2024-02-13  6:25 ` Hangbin Liu
  2024-02-15  3:10 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 6+ messages in thread
From: Hangbin Liu @ 2024-02-13  6:25 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev, jhs

On Sat, Feb 10, 2024 at 05:04:23PM -0800, Stephen Hemminger wrote:
> Add assertion to check for case of snprintf failing (bad format?)
> or buffer getting full.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

Reviewed-by: Hangbin Liu <liuhangbin@gmail.com>

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

* Re: [PATCH iproute2] tc: u32: check return value from snprintf
  2024-02-11  1:04 [PATCH iproute2] tc: u32: check return value from snprintf Stephen Hemminger
  2024-02-12  7:24 ` Hangbin Liu
  2024-02-13  6:25 ` Hangbin Liu
@ 2024-02-15  3:10 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-02-15  3:10 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev, liuhangbin, jhs

Hello:

This patch was applied to iproute2/iproute2.git (main)
by Stephen Hemminger <stephen@networkplumber.org>:

On Sat, 10 Feb 2024 17:04:23 -0800 you wrote:
> Add assertion to check for case of snprintf failing (bad format?)
> or buffer getting full.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  tc/f_u32.c | 4 ++++
>  1 file changed, 4 insertions(+)

Here is the summary with links:
  - [iproute2] tc: u32: check return value from snprintf
    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=d06f6a3d1766

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

* RE: [PATCH iproute2] tc: u32: check return value from snprintf
  2024-02-12 16:53   ` Stephen Hemminger
@ 2024-02-16  9:58     ` David Laight
  0 siblings, 0 replies; 6+ messages in thread
From: David Laight @ 2024-02-16  9:58 UTC (permalink / raw)
  To: 'Stephen Hemminger', Hangbin Liu
  Cc: netdev@vger.kernel.org, jhs@mojatatu.com

From: Stephen Hemminger <stephen@networkplumber.org>
> Sent: 12 February 2024 16:54
> 
> On Mon, 12 Feb 2024 15:24:49 +0800
> Hangbin Liu <liuhangbin@gmail.com> wrote:
> 
> > On Sat, Feb 10, 2024 at 05:04:23PM -0800, Stephen Hemminger wrote:
> > > Add assertion to check for case of snprintf failing (bad format?)
> > > or buffer getting full.
> > >
> > > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> >
> > Hi Stephen,
> >
> > Is there a bug report or something else that we only do the assertion
> > for tc/f_u32.c?
> >
> > Thanks
> > Hangbin
> 
> No bug, it is not possible to trigger with current code.
> Return of < 0 only happens with improper format string,
> and the overrun would only happen if buffer was not big enough
> The bsize is SPRINT_BUF() which is 64 bytes.
> 
> It is more a way to avoid some code checker complaining in future.

What are you testing?
If you are testing snprintf() then maybe a check for <0 is sane.
But otherwise it is a waste of time.

FWIW do you know what (any) printf() function should return for (eg):
	int len = MAXINT;
	len = snprintf(NULL, 0, "%*s %*s", len, "abcd", len, "1234");

My brain doesn't think that a 'bad format' generates -1.
You can get -1 from fprintf() (if a write() fails) which largely
means that code looking at the result of fprintf() is broken.
(You need to do fflush() and ferror() if you want to know
if a write failed, and you can't reliably assume it is the
length.)

	David

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


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

end of thread, other threads:[~2024-02-16  9:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-11  1:04 [PATCH iproute2] tc: u32: check return value from snprintf Stephen Hemminger
2024-02-12  7:24 ` Hangbin Liu
2024-02-12 16:53   ` Stephen Hemminger
2024-02-16  9:58     ` David Laight
2024-02-13  6:25 ` Hangbin Liu
2024-02-15  3:10 ` patchwork-bot+netdevbpf

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).