🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Dmitry Antipov Tue, 17 Nov 2026 16:51
[PATCH] net: bridge: avoid redundant strlen() in show_backup_port()
#17998
Author: Dmitry Antipov
Date: Tue, 17 Nov 2026 16:51
31 lines
9008 bytes
In store_backup_port(), prefer strchrnul() over strchr() to
avoid redundant call to strlen().

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 net/bridge/br_sysfs_if.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/net/bridge/br_sysfs_if.c b/net/bridge/br_sysfs_if.c
index 621d62a063d9..03205b3f465f 100644
--- a/net/bridge/br_sysfs_if.c
+++ b/net/bridge/br_sysfs_if.c
@@ -231,12 +231,12 @@ static ssize_t show_backup_port(struct net_bridge_port *p, char *buf)
 static int store_backup_port(struct net_bridge_port *p, char *buf)
 {
 	struct net_device *backup_dev = NULL;
-	char *nl = strchr(buf, '\n');
+	char *nl = strchrnul(buf, '\n');
 
-	if (nl)
+	if (*nl != '\0')
 		*nl = '\0';
 
-	if (strlen(buf) > 0) {
+	if (nl - buf > 0) {
 		backup_dev = __dev_get_by_name(dev_net(p->dev), buf);
 		if (!backup_dev)
 			return -ENOENT;
-- 
2.56.1
Re: [PATCH] net: bridge: avoid redundant strlen() in show_backup_port()
#18029
Author: Joe Damato
Date: Tue, 17 Nov 2026 17:49
15 lines
11672 bytes
On Tue, Nov 17, 2026 at 07:51:06PM +0300, Dmitry Antipov wrote:
> In store_backup_port(), prefer strchrnul() over strchr() to
> avoid redundant call to strlen().

a couple things:

- i dont't think the change is worth it because it feels like unnecessary
  churn in a path that isn't a hot path. how many bytes are we really avoiding
  counting here?

- commit message mentions store_backup_port but subject line says
  show_backup_port, so commit would need to be respun

but imho i think this should be dropped
Re: [PATCH] net: bridge: avoid redundant strlen() in show_backup_port()
#18440
Author: David Laight
Date: Wed, 18 Nov 2026 10:19
39 lines
11781 bytes
On Tue, 17 Nov 2026 19:51:06 +0300
Dmitry Antipov <dmantipov@yandex.ru> wrote:

> In store_backup_port(), prefer strchrnul() over strchr() to
> avoid redundant call to strlen().

For most architectures I think the compiler optimises that strlen()
to a check of buf[0].
So you don't get the function call at all.

David

> 
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> ---
>  net/bridge/br_sysfs_if.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/net/bridge/br_sysfs_if.c b/net/bridge/br_sysfs_if.c
> index 621d62a063d9..03205b3f465f 100644
> --- a/net/bridge/br_sysfs_if.c
> +++ b/net/bridge/br_sysfs_if.c
> @@ -231,12 +231,12 @@ static ssize_t show_backup_port(struct net_bridge_port *p, char *buf)
>  static int store_backup_port(struct net_bridge_port *p, char *buf)
>  {
>  	struct net_device *backup_dev = NULL;
> -	char *nl = strchr(buf, '\n');
> +	char *nl = strchrnul(buf, '\n');
>  
> -	if (nl)
> +	if (*nl != '\0')
>  		*nl = '\0';
>  
> -	if (strlen(buf) > 0) {
> +	if (nl - buf > 0) {
>  		backup_dev = __dev_get_by_name(dev_net(p->dev), buf);
>  		if (!backup_dev)
>  			return -ENOENT;
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads