parport: Use kasprintf() instead of fixed buffer formatting
PATCH 1/3 parport: Use kasprintf() instead of fixed buffer formatting
Improve readability and maintainability by replacing a hardcoded string allocation and formatting by the use of the kasprintf() helper.
--- drivers/parport/procfs.c | 53 +++++++--------------------------------- drivers/parport/share.c | 15 +++++------- include/linux/parport.h | 2 -- 3 files changed, 15 insertions(+), 55 deletions(-)
drivers/parport/procfs.c +9 -44
@@ -32,13 +32,6 @@#define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ)#define PARPORT_MIN_SPINTIME_VALUE 1#define PARPORT_MAX_SPINTIME_VALUE 1000 -/* -* PARPORT_BASE_* is the size of the known parts of the sysctl path -* in dev/partport/%s/devices/%s. "dev/parport/"(12), "/devices/"(9 -* and null char(1). -*/ -#define PARPORT_BASE_PATH_SIZE 13 -#define PARPORT_BASE_DEVICES_PATH_SIZE 22static int do_active_device (struct ctl_table *table,int write,void *result,size_t *lenp,loff_t *ppos)
@@ -431,8 +424,7 @@ int parport_proc_register(struct parport *port) {struct parport_sysctl_table *t;char *tmp_dir_path; -size_t tmp_path_len, port_name_len; -int bytes_written, i, err =0 ; +int i, err =0 ; t =kmemdup (&parport_sysctl_template,sizeof (*t),GFP_KERNEL );if (t ==NULL )
@@ -446,35 +438,23 @@ int parport_proc_register(struct parport *port) t->vars[5 + i].extra2 = &port->probe_info[i]; } - port_name_len =strnlen (port->name,PARPORT_NAME_MAX_LEN ); -/* -* Allocate a buffer for two paths: dev/parport/PORT and dev/parport/PORT/devices. -* We calculate for the second as that will give us enough for the first. -*/ - tmp_path_len =PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len; - tmp_dir_path =kzalloc (tmp_path_len,GFP_KERNEL ); + tmp_dir_path =kasprintf (GFP_KERNEL ,"dev/parport/%s/devices" , port->name);if (!tmp_dir_path) { err = -ENOMEM ; goto exit_free_t; } - bytes_written =snprintf (tmp_dir_path, tmp_path_len, -"dev/parport/%s/devices" , port->name); -if (tmp_path_len <= bytes_written) { - err = -ENOENT ; - goto exit_free_tmp_dir_path; - } t->devices_header =register_sysctl (tmp_dir_path, t->device_dir);if (t->devices_header ==NULL ) { err = -ENOENT ; goto exit_free_tmp_dir_path; } - tmp_path_len =PARPORT_BASE_PATH_SIZE + port_name_len; - bytes_written =snprintf (tmp_dir_path, tmp_path_len, -"dev/parport/%s" , port->name); -if (tmp_path_len <= bytes_written) { - err = -ENOENT ; +kfree (tmp_dir_path); + + tmp_dir_path =kasprintf (GFP_KERNEL ,"dev/parport/%s" , port->name); +if (!tmp_dir_path) { + err = -ENOMEM ; goto unregister_devices_h; }
@@ -514,34 +494,22 @@ int parport_proc_unregister(struct parport *port)int parport_device_proc_register (struct pardevice *device) { -int bytes_written, err =0 ;struct parport_device_sysctl_table *t;struct parport * port = device->port; -size_t port_name_len, device_name_len, tmp_dir_path_len;char *tmp_dir_path; +int err =0 ; t =kmemdup (&parport_device_sysctl_template,sizeof (*t),GFP_KERNEL );if (t ==NULL )return -ENOMEM ; - port_name_len =strnlen (port->name,PARPORT_NAME_MAX_LEN ); - device_name_len =strnlen (device->name,PATH_MAX ); -/* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */ - tmp_dir_path_len =PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len + device_name_len; - tmp_dir_path =kzalloc (tmp_dir_path_len,GFP_KERNEL ); + tmp_dir_path =kasprintf (GFP_KERNEL ,"dev/parport/%s/devices/%s" , port->name, device->name);if (!tmp_dir_path) { err = -ENOMEM ; goto exit_free_t; } - bytes_written =snprintf (tmp_dir_path, tmp_dir_path_len,"dev/parport/%s/devices/%s" , - port->name, device->name); -if (tmp_dir_path_len <= bytes_written) { - err = -ENOENT ; - goto exit_free_path; - } - t->vars[0 ].data = &device->timeslice; t->sysctl_header =register_sysctl (tmp_dir_path, t->vars);
@@ -554,9 +522,6 @@ int parport_device_proc_register(struct pardevice *device)kfree (tmp_dir_path);return 0 ; -exit_free_path: -kfree (tmp_dir_path); - exit_free_t:kfree (t);
drivers/parport/share.c +6 -9
@@ -438,7 +438,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,struct parport *tmp;int num;int device; -char *name;int ret; tmp =kzalloc (sizeof (struct parport ),GFP_KERNEL );
@@ -467,11 +466,6 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,atomic_set (&tmp->ref_count,1 );INIT_LIST_HEAD (&tmp->full_list); - name =kmalloc (PARPORT_NAME_MAX_LEN ,GFP_KERNEL ); -if (!name) { -kfree (tmp); -return NULL ; - }/* Search for the lowest free parport number. */ spin_lock (&full_list_lock);
@@ -487,11 +481,14 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,/* * Now that the portnum is known finish doing the Init. */ -sprintf (name,"parport%d" , tmp->portnum = tmp->number); - tmp->name = name; + tmp->name =kasprintf (GFP_KERNEL ,"parport%d" , tmp->portnum); +if (!tmp->name) { +kfree (tmp); +return NULL ; + } +dev_set_name (&tmp->bus_dev, tmp->name); tmp->bus_dev.bus = &parport_bus_type; tmp->bus_dev.release = free_port; -dev_set_name (&tmp->bus_dev, name); tmp->bus_dev.type = &parport_device_type;for (device =0 ; device <5 ; device++)
include/linux/parport.h +0 -2
@@ -180,8 +180,6 @@ struct ieee1284_info {struct semaphore irq; }; -#define PARPORT_NAME_MAX_LEN 15 -/* A parallel port */ struct parport {unsigned long base;/* base address */
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH 2/3 parport: Use list_for_each() helper
Convert hard to read custom code to list_for_each(). No functional changes intended.
Note, we may not use list_for_each_entry() as at the end of the list the iterator will point to an invalid entry and may not be dereferenced.
--- drivers/parport/share.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/parport/share.c +4 -2
@@ -469,9 +469,11 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma,/* Search for the lowest free parport number. */ spin_lock (&full_list_lock); -for (l = all_ports.next, num =0 ; l != &all_ports; l = l->next, num++) { + num =0 ; +list_for_each (l, &all_ports) { struct parport *p = list_entry(l, struct parport, full_list); -if (p->number != num) + +if (p->number != num++)break ; } tmp->portnum = tmp->number = num;
Signature · 1 line
2.40.0.1.gaa8946217a0b
PATCH 3/3 parport: Drop unneeded NULL or 0 assignments
In reply to Andy Shevchenkokzalloc() gives us a zeroed memory, no need to explicitly assing 0 or NULL or similar to the members of the data structure that has been allocated with the above mentioned API.
--- drivers/parport/share.c | 4 ---- 1 file changed, 4 deletions(-)
drivers/parport/share.c +0 -4
@@ -449,13 +449,9 @@ struct parport *parport_register_port(unsigned long base, int irq, int dma, tmp->irq = irq; tmp->dma = dma; tmp->muxport = tmp->daisy = tmp->muxsel =-1 ; - tmp->modes =0 ;INIT_LIST_HEAD (&tmp->list); - tmp->devices = tmp->cad =NULL ; - tmp->flags =0 ; tmp->ops = ops; tmp->physport = tmp; -memset (tmp->probe_info,0 ,5 *sizeof (struct parport_device_info ));rwlock_init (&tmp->cad_lock);spin_lock_init (&tmp->waitlist_lock);spin_lock_init (&tmp->pardevice_lock);
Signature · 1 line
2.40.0.1.gaa8946217a0b
drivers/parport/procfs.c int parport_proc_register(struct parport *port)
Quoted 39 lines · Andy Shevchenko
Improve readability and maintainability by replacing a hardcoded string allocation and formatting by the use of the
kasprintf()helper.Signed-off-by Andy Shevchenko <[email protected]>--- drivers/parport/procfs.c | 53 +++++++--------------------------------- drivers/parport/share.c | 15 +++++------- include/linux/parport.h | 2 -- 3 files changed, 15 insertions(+), 55 deletions(-)drivers/parport/procfs.c +1
-9@@ -32,13 +32,6 @@#define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ)#define PARPORT_MIN_SPINTIME_VALUE 1#define PARPORT_MAX_SPINTIME_VALUE 1000 -/* -* PARPORT_BASE_* is the size of the known parts of the sysctl path -* in dev/partport/%s/devices/%s. "dev/parport/"(12), "/devices/"(9 -* and null char(1). -*/ -#define PARPORT_BASE_PATH_SIZE 13 -#define PARPORT_BASE_DEVICES_PATH_SIZE 22static int do_active_device (struct ctl_table *table,int write,void *result,size_t *lenp,loff_t *ppos)@@ -431,8 +424,7 @@ int parport_proc_register(struct parport *port) {struct parport_sysctl_table *t;char *tmp_dir_path; -size_t tmp_path_len, port_name_len; -int bytes_written, i, err =0 ; +int i, err =0 ; t =kmemdup (&parport_sysctl_template,sizeof (*t),GFP_KERNEL );if (t ==NULL )@@ -446,35 +438,23 @@ int parport_proc_register(struct parport *port)
For this function I would even go a step further and start with the two kasprintf calls so we can then free them in the reverse order. And then leave the rest as it is. I attached an untested diff that applies on top of your changes to show you what I mean.
Quoted 50 lines
drivers/parport/procfs.c +6
-20t->vars[5 + i].extra2 = &port->probe_info[i]; } - port_name_len =strnlen (port->name,PARPORT_NAME_MAX_LEN ); -/* -* Allocate a buffer for two paths: dev/parport/PORT and dev/parport/PORT/devices. -* We calculate for the second as that will give us enough for the first. -*/ - tmp_path_len =PARPORT_BASE_DEVICES_PATH_SIZE + port_name_len; - tmp_dir_path =kzalloc (tmp_path_len,GFP_KERNEL ); + tmp_dir_path =kasprintf (GFP_KERNEL ,"dev/parport/%s/devices" , port->name);if (!tmp_dir_path) { err = -ENOMEM ; goto exit_free_t; } - bytes_written =snprintf (tmp_dir_path, tmp_path_len, -"dev/parport/%s/devices" , port->name); -if (tmp_path_len <= bytes_written) { - err = -ENOENT ; - goto exit_free_tmp_dir_path; - } t->devices_header =register_sysctl (tmp_dir_path, t->device_dir);if (t->devices_header ==NULL ) { err = -ENOENT ; goto exit_free_tmp_dir_path; } - tmp_path_len =PARPORT_BASE_PATH_SIZE + port_name_len; - bytes_written =snprintf (tmp_dir_path, tmp_path_len, -"dev/parport/%s" , port->name); -if (tmp_path_len <= bytes_written) { - err = -ENOENT ; +kfree (tmp_dir_path); + + tmp_dir_path =kasprintf (GFP_KERNEL ,"dev/parport/%s" , port->name); +if (!tmp_dir_path) { + err = -ENOMEM ; goto unregister_devices_h; }@@ -514,34 +494,22 @@ int parport_proc_unregister(struct parport *port)int parport_device_proc_register (struct pardevice *device) { -int bytes_written, err =0 ;struct parport_device_sysctl_table *t;struct parport * port = device->port; -size_t port_name_len, device_name_len, tmp_dir_path_len;char *tmp_dir_path;
...
include/linux/parport.h struct ieee1284_info { · line 183
Quoted 9 lines
include/linux/parport.h +0
-1@@ -180,8 +180,6 @@ struct ieee1284_info {struct semaphore irq; }; -#define PARPORT_NAME_MAX_LEN 15
This variable protected against port->name not ending in '\0'. Anyone worried that kasprintf could be unbounded?
Quoted 7 lines
include/linux/parport.h +0
-1-/* A parallel port */ struct parport {unsigned long base;/* base address */ Signature · 1 line
2.40.0.1.gaa8946217a0b
Signature · 1 line
Joel Granados
drivers/parport/procfs.c +25 -25
@@ -423,12 +423,24 @@ parport_default_sysctl_table = {int parport_proc_register (struct parport *port) {struct parport_sysctl_table *t; -char *tmp_dir_path; +char *parport_name_devices, *parport_name;int i, err =0 ; + parport_name_devices =kasprintf (GFP_KERNEL ,"dev/parport/%s/devices" , port->name); +if (!parport_name_devices) +return -ENOMEM ; + + parport_name =kasprintf (GFP_KERNEL ,"dev/parport/%s" , port->name); +if (!parport_name) { + err = -ENOMEM ; + goto exit_partport_name_devices; + } + t =kmemdup (&parport_sysctl_template,sizeof (*t),GFP_KERNEL ); -if (t ==NULL ) -return -ENOMEM ; +if (t ==NULL ) { + err = -ENOMEM ; + goto exit_parport_name; + } t->device_dir[0 ].extra1 = port;
@@ -438,27 +450,13 @@ int parport_proc_register(struct parport *port) t->vars[5 + i].extra2 = &port->probe_info[i]; } - tmp_dir_path =kasprintf (GFP_KERNEL ,"dev/parport/%s/devices" , port->name); -if (!tmp_dir_path) { - err = -ENOMEM ; - goto exit_free_t; - } - - t->devices_header =register_sysctl (tmp_dir_path, t->device_dir); + t->devices_header =register_sysctl (parport_name_devices, t->device_dir);if (t->devices_header ==NULL ) { err = -ENOENT ; - goto exit_free_tmp_dir_path; + goto exit_free_t; } -kfree (tmp_dir_path); - - tmp_dir_path =kasprintf (GFP_KERNEL ,"dev/parport/%s" , port->name); -if (!tmp_dir_path) { - err = -ENOMEM ; - goto unregister_devices_h; - } - - t->port_header =register_sysctl (tmp_dir_path, t->vars); + t->port_header =register_sysctl (parport_name, t->vars);if (t->port_header ==NULL ) { err = -ENOENT ; goto unregister_devices_h;
@@ -466,17 +464,19 @@ int parport_proc_register(struct parport *port) port->sysctl_table = t; -kfree (tmp_dir_path); -return 0 ; + goto exit_parport_name; unregister_devices_h:unregister_sysctl_table (t->devices_header); -exit_free_tmp_dir_path: -kfree (tmp_dir_path); - exit_free_t:kfree (t); + +exit_parport_name: +kfree (parport_name); + +exit_partport_name_devices: +kfree (parport_name_devices);return err; }
Quoted 1 line · Joel Granados
On Fri, Sep 01, 2023 at 04:42:48PM +0300, Andy Shevchenko wrote:
Thank you for the review, my answers below.
...
Quoted 8 lines
Quoted 8 lines
(quoted diff) +1
-2@@ -431,8 +424,7 @@ int parport_proc_register(struct parport *port) { struct parport_sysctl_table *t; char *tmp_dir_path; - size_t tmp_path_len, port_name_len; - int bytes_written, i, err = 0; + int i, err = 0;
Quoted 6 lines
Review int parport_proc_register(struct parport *port)
Quoted 3 lines
(quoted diff) +0
-0t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL); if (t == NULL)@@ -446,35 +438,23 @@ int parport_proc_register(struct parport *port)For this function I would even go a step further and start with the two kasprintf calls so we can then free them in the reverse order. And then leave the rest as it is.
I'm not sure I see the big picture here. Can you draft what you are proposing (showing only the lines that are important)?
Quoted 2 lines
I attached an untested diff that applies on
top of your changes to show you what I mean.
Ah, I see now, it's below. And how is it better?
(LoCs statistics seems to be the same, so...)
What is a downside in my opinion with your code is this line
return 0; --> goto blablabla.
this makes code less maintainable.
OTOH we may do what you want, but it will take a bit more LoCs and honestly I don't see the benefit of doing that as in both cases the variable used is temporary. What may be the good solution here is to split the repetitive code excerpt to the parameterized helper function. But this will be another patch which you can build on top of this series, right?
...
Quoted 11 lines
include/linux/parport.h struct ieee1284_info { · line 183
Quoted 9 lines
include/linux/parport.h +0
-1@@ -180,8 +180,6 @@ struct ieee1284_info {struct semaphore irq; }; -#define PARPORT_NAME_MAX_LEN 15This variable protected against
port->namenot ending in '\0'. Anyone worried that kasprintf could be unbounded?
I'm lost here. kasprintf() guarantees the NUL-termination. Any other concerns?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Improve readability and maintainability by replacing a hardcoded string allocation and formatting by the use of the
kasprintf()helper.
Sudip, any comments on this series?
Signature · 2 lines
With Best Regards, Andy Shevchenko