[PATCH] samples/bpf: Modify the incorrect format specifier

liujing posted 1 patch 1 year ago
[PATCH] samples/bpf: Modify the incorrect format specifier
Posted by liujing 1 year ago
Replace %d with %u in snprintf() because it is "unsigned int".

Signed-off-by: liujing <liujing@cmss.chinamobile.com>

diff --git a/samples/bpf/xdp_router_ipv4_user.c b/samples/bpf/xdp_router_ipv4_user.c
index 266fdd0b025d..3fc1d37fee7c 100644
--- a/samples/bpf/xdp_router_ipv4_user.c
+++ b/samples/bpf/xdp_router_ipv4_user.c
@@ -134,11 +134,11 @@ static void read_route(struct nlmsghdr *nh, int nll)
 					*((__be32 *)RTA_DATA(rt_attr)));
 				break;
 			case RTA_OIF:
-				sprintf(ifs, "%u",
+				sprintf(ifs, "%d",
 					*((int *)RTA_DATA(rt_attr)));
 				break;
 			case RTA_METRICS:
-				sprintf(metrics, "%u",
+				sprintf(metrics, "%d",
 					*((int *)RTA_DATA(rt_attr)));
 			default:
 				break;
-- 
2.27.0
Re: [PATCH] samples/bpf: Modify the incorrect format specifier
Posted by Andrii Nakryiko 1 year ago
On Thu, Dec 5, 2024 at 6:31 PM liujing <liujing@cmss.chinamobile.com> wrote:
>
> Replace %d with %u in snprintf() because it is "unsigned int".

The code change is fine, but the explanation is ambiguous and hard to
follow. Just mention that we are printing integers, so we need %d
instead of %u for snprintf. As you wrote it above, it reads as if we
are printing unsigned int, yet code contradicts that.

pw-bot: cr

>
> Signed-off-by: liujing <liujing@cmss.chinamobile.com>
>
> diff --git a/samples/bpf/xdp_router_ipv4_user.c b/samples/bpf/xdp_router_ipv4_user.c
> index 266fdd0b025d..3fc1d37fee7c 100644
> --- a/samples/bpf/xdp_router_ipv4_user.c
> +++ b/samples/bpf/xdp_router_ipv4_user.c
> @@ -134,11 +134,11 @@ static void read_route(struct nlmsghdr *nh, int nll)
>                                         *((__be32 *)RTA_DATA(rt_attr)));
>                                 break;
>                         case RTA_OIF:
> -                               sprintf(ifs, "%u",
> +                               sprintf(ifs, "%d",
>                                         *((int *)RTA_DATA(rt_attr)));
>                                 break;
>                         case RTA_METRICS:
> -                               sprintf(metrics, "%u",
> +                               sprintf(metrics, "%d",
>                                         *((int *)RTA_DATA(rt_attr)));
>                         default:
>                                 break;
> --
> 2.27.0
>
>
>