Skip to contentSkip to conversation

comedi: comedi_bond: Refactor strlcat() to seq_buf in do_dev_config()

PATCH In review Mariia Nikitash 3 participants 5 messages Sep 27 – Nov 12 Comedi drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 comedi: comedi_bond: Refactor strlcat() to seq_buf in do_dev_config()

From: Bill Wendling <[email protected]>

do_dev_config() builds a space-separated list of bonded devices in devpriv->name by formatting each "minor:subdev " pair into a temporary stack buffer with snprintf() and appending it with strlcat() in a loop.

In preparation for removing the deprecated strlcat() API[1], use struct seq_buf and seq_buf_printf() to format each entry directly into devpriv->name. This avoids repeated rescanning of devpriv->name and eliminates the temporary buffer.

---
 drivers/comedi/drivers/comedi_bond.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/comedi/drivers/comedi_bond.c +8 -10
index 8e10ecab4f0d..a10cfbd04c72 100644
@@ -39,6 +39,7 @@
 
 #include <linux/module.h>
 #include <linux/string.h>
+#include <linux/seq_buf.h>
 #include <linux/slab.h>
 #include <linux/comedi.h>
 #include <linux/comedi/comedilib.h>
@@ -170,10 +171,11 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 {
 	struct comedi_bond_private *devpriv = dev->private;
 	DECLARE_BITMAP(devs_opened, COMEDI_NUM_BOARD_MINORS);
+	struct seq_buf s;
 	int i;
 
 	memset(&devs_opened, 0, sizeof(devs_opened));
-	devpriv->name[0] = 0;
+	seq_buf_init(&s, devpriv->name, sizeof(devpriv->name));
 	/*
 	 * Loop through all comedi devices specified on the command-line,
 	 * building our device list.
@@ -250,15 +252,9 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 			}
 			devpriv->devs = devs;
 			devpriv->devs[devpriv->ndevs++] = bdev;
-			{
-				/* Append dev:subdev to devpriv->name */
-				char buf[20];
-
-				snprintf(buf, sizeof(buf), "%u:%u ",
-					 bdev->minor, bdev->subdev);
-				strlcat(devpriv->name, buf,
-					sizeof(devpriv->name));
-			}
+
+			/* Append dev:subdev to devpriv->name */
+			seq_buf_printf(&s, "%u:%u ", bdev->minor, bdev->subdev);
 		}
 	}
 
@@ -267,6 +263,8 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 		return -EINVAL;
 	}
 
+	seq_buf_str(&s);
+
 	return 0;
 }
 
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 65 lines · Mariia Nikitash

From: Bill Wendling <[email protected]>

do_dev_config() builds a space-separated list of bonded devices in devpriv->name by formatting each "minor:subdev " pair into a temporary stack buffer with snprintf() and appending it with strlcat() in a loop.

In preparation for removing the deprecated strlcat() API[1], use struct seq_buf and seq_buf_printf() to format each entry directly into devpriv->name. This avoids repeated rescanning of devpriv->name and eliminates the temporary buffer.

---
  drivers/comedi/drivers/comedi_bond.c | 18 ++++++++----------
  1 file changed, 8 insertions(+), 10 deletions(-)
drivers/comedi/drivers/comedi_bond.c +6 -10
index 8e10ecab4f0d..a10cfbd04c72 100644
@@ -39,6 +39,7 @@
 
  #include <linux/module.h>
  #include <linux/string.h>
+#include <linux/seq_buf.h>
  #include <linux/slab.h>
  #include <linux/comedi.h>
  #include <linux/comedi/comedilib.h>
@@ -170,10 +171,11 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
  {
  	struct comedi_bond_private *devpriv = dev->private;
  	DECLARE_BITMAP(devs_opened, COMEDI_NUM_BOARD_MINORS);
+	struct seq_buf s;
  	int i;
 
  	memset(&devs_opened, 0, sizeof(devs_opened));
-	devpriv->name[0] = 0;
+	seq_buf_init(&s, devpriv->name, sizeof(devpriv->name));
  	/*
  	 * Loop through all comedi devices specified on the command-line,
  	 * building our device list.
@@ -250,15 +252,9 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
  			}
  			devpriv->devs = devs;
  			devpriv->devs[devpriv->ndevs++] = bdev;
-			{
-				/* Append dev:subdev to devpriv->name */
-				char buf[20];
-
-				snprintf(buf, sizeof(buf), "%u:%u ",
-					 bdev->minor, bdev->subdev);
-				strlcat(devpriv->name, buf,
-					sizeof(devpriv->name));
-			}
+
+			/* Append dev:subdev to devpriv->name */
+			seq_buf_printf(&s, "%u:%u ", bdev->minor, bdev->subdev);
  		}
  	}
 

drivers/comedi/drivers/comedi_bond.c static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it) · line 270

Quoted 8 lines
drivers/comedi/drivers/comedi_bond.c +2 -0
@@ -267,6 +263,8 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
  		return -EINVAL;
  	}
 
+	seq_buf_str(&s);
+
  	return 0;
  }
 

Looks good, thanks!

Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-
Reviewed-by on v1 1/1
#2 lore raw
In reply to Mariia Nikitash
Quoted 65 lines · Mariia Nikitash

From: Bill Wendling <[email protected]>

do_dev_config() builds a space-separated list of bonded devices in devpriv->name by formatting each "minor:subdev " pair into a temporary stack buffer with snprintf() and appending it with strlcat() in a loop.

In preparation for removing the deprecated strlcat() API[1], use struct seq_buf and seq_buf_printf() to format each entry directly into devpriv->name. This avoids repeated rescanning of devpriv->name and eliminates the temporary buffer.

---
 drivers/comedi/drivers/comedi_bond.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/comedi/drivers/comedi_bond.c +6 -10
index 8e10ecab4f0d..a10cfbd04c72 100644
@@ -39,6 +39,7 @@
 
 #include <linux/module.h>
 #include <linux/string.h>
+#include <linux/seq_buf.h>
 #include <linux/slab.h>
 #include <linux/comedi.h>
 #include <linux/comedi/comedilib.h>
@@ -170,10 +171,11 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 {
 	struct comedi_bond_private *devpriv = dev->private;
 	DECLARE_BITMAP(devs_opened, COMEDI_NUM_BOARD_MINORS);
+	struct seq_buf s;
 	int i;
 
 	memset(&devs_opened, 0, sizeof(devs_opened));
-	devpriv->name[0] = 0;
+	seq_buf_init(&s, devpriv->name, sizeof(devpriv->name));
 	/*
 	 * Loop through all comedi devices specified on the command-line,
 	 * building our device list.
@@ -250,15 +252,9 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 			}
 			devpriv->devs = devs;
 			devpriv->devs[devpriv->ndevs++] = bdev;
-			{
-				/* Append dev:subdev to devpriv->name */
-				char buf[20];
-
-				snprintf(buf, sizeof(buf), "%u:%u ",
-					 bdev->minor, bdev->subdev);
-				strlcat(devpriv->name, buf,
-					sizeof(devpriv->name));
-			}
+
+			/* Append dev:subdev to devpriv->name */
+			seq_buf_printf(&s, "%u:%u ", bdev->minor, bdev->subdev);
 		}
 	}
 

drivers/comedi/drivers/comedi_bond.c static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it) · line 266

Quoted 4 lines
drivers/comedi/drivers/comedi_bond.c +1 -0
@@ -267,6 +263,8 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 		return -EINVAL;
 	}
 
+	seq_buf_str(&s);

If this hasn't been applied yet, a v2 with the above changed to the now-available seq_buf_terminate() would be nice to have. Either way:

Quoted 7 lines
drivers/comedi/drivers/comedi_bond.c +1 -0
+
 	return 0;
 }
 
Signature · 1 line
2.43.0
Signature · 1 line
Kees Cook
#3 lore raw

Hi!

Quoted 70 lines · Kees Cook
Quoted 68 lines · Mariia Nikitash

From: Bill Wendling <[email protected]>

do_dev_config() builds a space-separated list of bonded devices in devpriv->name by formatting each "minor:subdev " pair into a temporary stack buffer with snprintf() and appending it with strlcat() in a loop.

In preparation for removing the deprecated strlcat() API[1], use struct seq_buf and seq_buf_printf() to format each entry directly into devpriv->name. This avoids repeated rescanning of devpriv->name and eliminates the temporary buffer.

---
 drivers/comedi/drivers/comedi_bond.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/comedi/drivers/comedi_bond.c +6 -10
index 8e10ecab4f0d..a10cfbd04c72 100644
@@ -39,6 +39,7 @@
 
 #include <linux/module.h>
 #include <linux/string.h>
+#include <linux/seq_buf.h>
 #include <linux/slab.h>
 #include <linux/comedi.h>
 #include <linux/comedi/comedilib.h>
@@ -170,10 +171,11 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
 {
      struct comedi_bond_private *devpriv = dev->private;
      DECLARE_BITMAP(devs_opened, COMEDI_NUM_BOARD_MINORS);
+     struct seq_buf s;
      int i;
 
      memset(&devs_opened, 0, sizeof(devs_opened));
-     devpriv->name[0] = 0;
+     seq_buf_init(&s, devpriv->name, sizeof(devpriv->name));
      /*
       * Loop through all comedi devices specified on the command-line,
       * building our device list.
@@ -250,15 +252,9 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
                      }
                      devpriv->devs = devs;
                      devpriv->devs[devpriv->ndevs++] = bdev;
-                     {
-                             /* Append dev:subdev to devpriv->name */
-                             char buf[20];
-
-                             snprintf(buf, sizeof(buf), "%u:%u ",
-                                      bdev->minor, bdev->subdev);
-                             strlcat(devpriv->name, buf,
-                                     sizeof(devpriv->name));
-                     }
+
+                     /* Append dev:subdev to devpriv->name */
+                     seq_buf_printf(&s, "%u:%u ", bdev->minor, bdev->subdev);
              }
      }
 
@@ -267,6 +263,8 @@ static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it)
              return -EINVAL;
      }
 
Quoted 6 lines

drivers/comedi/drivers/comedi_bond.c static int do_dev_config(struct comedi_device *dev, struct comedi_devconfig *it) · line 266

Quoted 1 line
drivers/comedi/drivers/comedi_bond.c +1 -0
+     seq_buf_str(&s);

If this hasn't been applied yet, a v2 with the above changed to the now-available seq_buf_terminate() would be nice to have. Either way:

Thanks for the review! This patch has already been merged, but I'd be happy to send a follow up patch with seq_buf_terminate() if you'd like.

Quoted 6 lines
Quoted 6 lines
drivers/comedi/drivers/comedi_bond.c +1 -0
+
      return 0;
 }
 
Quoted 6 lines
Quoted 3 lines

--
2.43.0

--
Kees Cook

Best,
Mariia

#4 lore raw
Quoted 3 lines · Mariia Nikitash

Thanks for the review! This patch has already been merged, but I'd be happy to send a follow up patch with seq_buf_terminate() if you'd like.

Nah, it's fine. :) I don't see value in the churn since the binary output will be identical.

Signature · 1 line
Kees Cook