Skip to contentSkip to conversation

SUNRPC: Fix -Wformat-truncation warning

PATCH Superseded [email protected] 3 participants 7 messages Aug 14, 2024 – Aug 16, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SUNRPC: Fix -Wformat-truncation warning

From: Kunwu Chan <[email protected]>

Increase size of the servername array to avoid truncated output warning.

net/sunrpc/clnt.c:582:75: error:‘%s’ directive output may be truncated writing up to 107 bytes into a region of size 48 [-Werror=format-truncation=]
582 | snprintf(servername, sizeof(servername), "%s",
| ^~

net/sunrpc/clnt.c:582:33: note:‘snprintf’ output between 1 and 108 bytes into a destination of size 48

  582 |                     snprintf(servername, sizeof(servername), "%s",
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  583 |                                          sun->sun_path);
---
 net/sunrpc/clnt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sunrpc/clnt.c +1 -1
index 09f29a95f2bc..874085f3ed50 100644
@@ -546,7 +546,7 @@ struct rpc_clnt *rpc_create(struct rpc_create_args *args)
 		.connect_timeout = args->connect_timeout,
 		.reconnect_timeout = args->reconnect_timeout,
 	};
-	char servername[48];
+	char servername[108];
 	struct rpc_clnt *clnt;
 	int i;
 
Signature · 1 line
2.40.1
#1 lore raw
Quoted 26 lines · On Wed, 14 Aug 2024, [email protected] wrote:

From: Kunwu Chan <[email protected]>

Increase size of the servername array to avoid truncated output warning.

net/sunrpc/clnt.c:582:75: error:‘%s’ directive output may be truncated writing up to 107 bytes into a region of size 48 [-Werror=format-truncation=]
582 | snprintf(servername, sizeof(servername), "%s",
| ^~

net/sunrpc/clnt.c:582:33: note:‘snprintf’ output between 1 and 108 bytes into a destination of size 48

  582 |                     snprintf(servername, sizeof(servername), "%s",
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  583 |                                          sun->sun_path);
---
 net/sunrpc/clnt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sunrpc/clnt.c +0 -0
index 09f29a95f2bc..874085f3ed50 100644

net/sunrpc/clnt.c struct rpc_clnt *rpc_create(struct rpc_create_args *args) · line 549

Quoted 5 lines
net/sunrpc/clnt.c +1 -1
@@ -546,7 +546,7 @@ struct rpc_clnt *rpc_create(struct rpc_create_args *args)
 		.connect_timeout = args->connect_timeout,
 		.reconnect_timeout = args->reconnect_timeout,
 	};
-	char servername[48];
+	char servername[108];

If we choose this approach to removing the warning, then we should use UNIX_PATH_MAX rather than 108.

However the longest server name copied in here will in practice be
/var/run/rpcbind.sock

so the extra 60 bytes on the stack is wasted ... maybe that doesn't matter.

The string is only used by xprt_create_transport() which requires it to be less than RPC_MAXNETNAMELEN - which is 256.
So maybe that would be a better value to use for the array size .... if we assume that stack space isn't a problem.

What ever number we use, I'd rather it was a defined constant, and not an apparently arbitrary number.

Thanks,
NeilBrown

Quoted 7 lines
net/sunrpc/clnt.c +0 -0
 	struct rpc_clnt *clnt;
 	int i;
 
Signature · 1 line
2.40.1
author
#2 lore raw

Thanks for your reply.

Quoted 22 lines · NeilBrown
Quoted 21 lines · On Wed, 14 Aug 2024, [email protected] wrote:

From: Kunwu Chan <[email protected]>

Increase size of the servername array to avoid truncated output warning.

net/sunrpc/clnt.c:582:75: error:‘%s’ directive output may be truncated writing up to 107 bytes into a region of size 48 [-Werror=format-truncation=]
582 | snprintf(servername, sizeof(servername), "%s",
| ^~

net/sunrpc/clnt.c:582:33: note:‘snprintf’ output between 1 and 108 bytes into a destination of size 48

   582 |                     snprintf(servername, sizeof(servername), "%s",
       |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   583 |                                          sun->sun_path);
---
  net/sunrpc/clnt.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

net/sunrpc/clnt.c struct rpc_clnt *rpc_create(struct rpc_create_args *args) · line 549

Quoted 10 lines
net/sunrpc/clnt.c +1 -1
index 09f29a95f2bc..874085f3ed50 100644
@@ -546,7 +546,7 @@ struct rpc_clnt *rpc_create(struct rpc_create_args *args)
  		.connect_timeout = args->connect_timeout,
  		.reconnect_timeout = args->reconnect_timeout,
  	};
-	char servername[48];
+	char servername[108];

If we choose this approach to removing the warning, then we should use UNIX_PATH_MAX rather than 108.

My negligence.

Quoted 6 lines

However the longest server name copied in here will in practice be
/var/run/rpcbind.sock

so the extra 60 bytes on the stack is wasted ... maybe that doesn't matter.

I'm thinking  about use a dynamic space alloc method like kasprintf to avoid space waste.

Quoted 4 lines

The string is only used by xprt_create_transport() which requires it to be less than RPC_MAXNETNAMELEN - which is 256.
So maybe that would be a better value to use for the array size .... if we assume that stack space isn't a problem.

Thank you for the detailed explanation. I read the xprt_create_transport,  the RPC_MAXNETNAMELEN

is only use to xprt_create_transport .

Quoted 2 lines

What ever number we use, I'd rather it was a defined constant, and not an apparently arbitrary number.

Whether we could check the sun->sun_path length before using snprintf?  The array size should smaller

than  the minimum of sun->sun_path and RPC_MAXNETNAMELEN.

Or use the dynamic space allocate method to save space.

Quoted 12 lines

Thanks,
NeilBrown

Quoted 7 lines
net/sunrpc/clnt.c +0 -0
  	struct rpc_clnt *clnt;
  	int i;
 
Signature · 1 line
2.40.1
Signature · 2 lines
Thanks,
  Kunwu.Chan
#3 lore raw
Quoted 59 lines · Kunwu Chan

Thanks for your reply.

Quoted 22 lines · NeilBrown
Quoted 21 lines · On Wed, 14 Aug 2024, [email protected] wrote:

From: Kunwu Chan <[email protected]>

Increase size of the servername array to avoid truncated output warning.

net/sunrpc/clnt.c:582:75: error:‘%s’ directive output may be truncated writing up to 107 bytes into a region of size 48 [-Werror=format-truncation=]
582 | snprintf(servername, sizeof(servername), "%s",
| ^~

net/sunrpc/clnt.c:582:33: note:‘snprintf’ output between 1 and 108 bytes into a destination of size 48

   582 |                     snprintf(servername, sizeof(servername), "%s",
       |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   583 |                                          sun->sun_path);
---
  net/sunrpc/clnt.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

net/sunrpc/clnt.c struct rpc_clnt *rpc_create(struct rpc_create_args *args) · line 549

Quoted 10 lines
net/sunrpc/clnt.c +1 -1
index 09f29a95f2bc..874085f3ed50 100644
@@ -546,7 +546,7 @@ struct rpc_clnt *rpc_create(struct rpc_create_args *args)
  		.connect_timeout = args->connect_timeout,
  		.reconnect_timeout = args->reconnect_timeout,
  	};
-	char servername[48];
+	char servername[108];

If we choose this approach to removing the warning, then we should use UNIX_PATH_MAX rather than 108.

My negligence.

Quoted 6 lines

However the longest server name copied in here will in practice be
/var/run/rpcbind.sock

so the extra 60 bytes on the stack is wasted ... maybe that doesn't matter.

I'm thinking  about use a dynamic space alloc method like kasprintf to avoid space waste.

Quoted 4 lines

The string is only used by xprt_create_transport() which requires it to be less than RPC_MAXNETNAMELEN - which is 256.
So maybe that would be a better value to use for the array size .... if we assume that stack space isn't a problem.

Thank you for the detailed explanation. I read the xprt_create_transport,  the RPC_MAXNETNAMELEN

is only use to xprt_create_transport .

Quoted 2 lines

What ever number we use, I'd rather it was a defined constant, and not an apparently arbitrary number.

Quoted 6 lines

Whether we could check the sun->sun_path length before using snprintf? The array size should smaller

than  the minimum of sun->sun_path and RPC_MAXNETNAMELEN.

Or use the dynamic space allocate method to save space.

I think that dynamically allocating space is not a good idea. It means you have to handle failure which is just a waste of code.

I'd suggest simply changing the array to RPC_MAXNETNAMELEN.

NeilBrown

Quoted 18 lines
Quoted 13 lines

Thanks,
NeilBrown

Quoted 7 lines
net/sunrpc/clnt.c +0 -0
  	struct rpc_clnt *clnt;
  	int i;
 
Signature · 1 line
2.40.1
Signature · 2 lines
Thanks,
   Kunwu.Chan
author
#4 lore raw

Thanks for your reply.

Quoted 60 lines · NeilBrown
Quoted 59 lines · Kunwu Chan

Thanks for your reply.

Quoted 22 lines · NeilBrown
Quoted 21 lines · On Wed, 14 Aug 2024, [email protected] wrote:

From: Kunwu Chan <[email protected]>

Increase size of the servername array to avoid truncated output warning.

net/sunrpc/clnt.c:582:75: error:‘%s’ directive output may be truncated writing up to 107 bytes into a region of size 48 [-Werror=format-truncation=]
582 | snprintf(servername, sizeof(servername), "%s",
| ^~

net/sunrpc/clnt.c:582:33: note:‘snprintf’ output between 1 and 108 bytes into a destination of size 48

    582 |                     snprintf(servername, sizeof(servername), "%s",
        |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    583 |                                          sun->sun_path);
---
   net/sunrpc/clnt.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

net/sunrpc/clnt.c struct rpc_clnt *rpc_create(struct rpc_create_args *args) · line 549

Quoted 10 lines
net/sunrpc/clnt.c +1 -1
index 09f29a95f2bc..874085f3ed50 100644
@@ -546,7 +546,7 @@ struct rpc_clnt *rpc_create(struct rpc_create_args *args)
   		.connect_timeout = args->connect_timeout,
   		.reconnect_timeout = args->reconnect_timeout,
   	};
-	char servername[48];
+	char servername[108];

If we choose this approach to removing the warning, then we should use UNIX_PATH_MAX rather than 108.

My negligence.

Quoted 5 lines

However the longest server name copied in here will in practice be
/var/run/rpcbind.sock

so the extra 60 bytes on the stack is wasted ... maybe that doesn't matter.

I'm thinking  about use a dynamic space alloc method like kasprintf to avoid space waste.

Quoted 4 lines

The string is only used by xprt_create_transport() which requires it to be less than RPC_MAXNETNAMELEN - which is 256.
So maybe that would be a better value to use for the array size .... if we assume that stack space isn't a problem.

Thank you for the detailed explanation. I read the xprt_create_transport,  the RPC_MAXNETNAMELEN

is only use to xprt_create_transport .

Quoted 2 lines

What ever number we use, I'd rather it was a defined constant, and not an apparently arbitrary number.

Whether we could check the sun->sun_path length before using snprintf? The array size should smaller

Quoted 7 lines
Quoted 3 lines

than  the minimum of sun->sun_path and RPC_MAXNETNAMELEN.

Or use the dynamic space allocate method to save space.

I think that dynamically allocating space is not a good idea. It means you have to handle failure which is just a waste of code.

I'd suggest simply changing the array to RPC_MAXNETNAMELEN.

I'll follow your suggestion and change it in v2.

Quoted 21 lines

NeilBrown

Quoted 16 lines
Quoted 11 lines

Thanks,
NeilBrown

Quoted 7 lines
net/sunrpc/clnt.c +0 -0
   	struct rpc_clnt *clnt;
   	int i;
 
Signature · 1 line
2.40.1
Signature · 2 lines
Thanks,
    Kunwu.Chan
Signature · 2 lines
Thanks,
  Kunwu.Chan
#5 lore raw
In reply to [email protected]
Quoted 10 lines · On Wed, Aug 14, 2024 at 05:38:53PM +0800, [email protected] wrote:

From: Kunwu Chan <[email protected]>

Increase size of the servername array to avoid truncated output warning.

net/sunrpc/clnt.c:582:75: error:‘%s’ directive output may be truncated writing up to 107 bytes into a region of size 48 [-Werror=format-truncation=]
582 | snprintf(servername, sizeof(servername), "%s",
| ^~

Quoted 5 lines

net/sunrpc/clnt.c:582:33: note:‘snprintf’ output between 1 and 108 bytes into a destination of size 48

  582 |                     snprintf(servername, sizeof(servername), "%s",
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  583 |                                          sun->sun_path);

I think this has come up before, but i could be mis-remembering. Please could you do a search for the discussion. The fact it is not solved suggests to me it is not so simple to fix. Maybe there is some protocol implications here.

Andrew

#6 lore raw
Quoted 17 lines · Andrew Lunn
Quoted 10 lines · On Wed, Aug 14, 2024 at 05:38:53PM +0800, [email protected] wrote:

From: Kunwu Chan <[email protected]>

Increase size of the servername array to avoid truncated output warning.

net/sunrpc/clnt.c:582:75: error:‘%s’ directive output may be truncated writing up to 107 bytes into a region of size 48 [-Werror=format-truncation=]
582 | snprintf(servername, sizeof(servername), "%s",
| ^~

Quoted 5 lines

net/sunrpc/clnt.c:582:33: note:‘snprintf’ output between 1 and 108 bytes into a destination of size 48

  582 |                     snprintf(servername, sizeof(servername), "%s",
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  583 |                                          sun->sun_path);
Quoted 7 lines

I think this has come up before, but i could be mis-remembering. Please could you do a search for the discussion. The fact it is not solved suggests to me it is not so simple to fix. Maybe there is some protocol implications here.

Andrew

All I could find was

https://lore.kernel.org/all/[email protected]/

which essentially followed the same path as this conversation. The patch was resubmitted using UNIX_PATH_MAX but never responded to by the relevant maintainers.

There are no protocol implications. This string is only used for informational messages.

NeilBrown