use g_path_get_basename instead of basename
PATCH 1/1 use g_path_get_basename instead of basename
basename(3) and dirname(3) modify their argument and may return pointers to statically allocated memory which may be overwritten by subsequent calls.g_path_get_basename and g_path_get_dirname have no such issues, and therefore more preferable.
--- fsdev/virtfs-proxy-helper.c | 6 +++++- hw/s390x/s390-ccw.c | 17 +++++++++++------ hw/vfio/ccw.c | 7 +++++-- hw/vfio/pci.c | 6 ++++-- hw/vfio/platform.c | 6 ++++-- qemu-io.c | 8 +++++++- qemu-nbd.c | 5 ++++- qga/commands-posix.c | 4 ++-- 8 files changed, 42 insertions(+), 17 deletions(-)
fsdev/virtfs-proxy-helper.c +5 -1
@@ -787,6 +787,8 @@ error:static void usage (char *prog) { +char *base_filename =g_path_get_basename (prog); +fprintf (stderr,"usage: %s\n" " -p|--path <path> 9p path to export\n" " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
@@ -795,7 +797,9 @@ static void usage(char *prog)" access to this socket\n" " \tNote: -s & -f can not be used together\n" " [-n|--nodaemon] Run as a normal program\n" , - basename(prog)); + base_filename); + +g_free (base_filename); } static int process_reply(int sock, int type,
hw/s390x/s390-ccw.c +11 -6
@@ -34,7 +34,7 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, Error **errp) {unsigned int cssid, ssid, devid; -char dev_path[PATH_MAX ] = {0 }, *tmp; +char dev_path[PATH_MAX ] = {0 }, *dir_name, *dir_path;if (!sysfsdev) {error_setg (errp,"No host device provided" );
@@ -48,18 +48,23 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev,return ; } - cdev->mdevid =g_strdup (basename (dev_path)); + cdev->mdevid =g_path_get_basename (dev_path); - tmp =basename (dirname (dev_path)); -if (sscanf (tmp,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) { -error_setg_errno (errp, errno,"Failed to read %s" , tmp); -return ; + dir_path =g_path_get_dirname (dev_path); + dir_name =g_path_get_basename (dir_path); +if (sscanf (dir_name,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) { +error_setg_errno (errp, errno,"Failed to read %s" , dir_name); + goto out; } cdev->hostid.cssid = cssid; cdev->hostid.ssid = ssid; cdev->hostid.devid = devid; cdev->hostid.valid = true; + +out: +g_free (dir_path); +g_free (dir_name); }static void s390_ccw_realize (S390CCWDevice *cdev,char *sysfsdev,Error **errp)
hw/vfio/ccw.c +5 -2
@@ -300,7 +300,7 @@ static void vfio_put_device(VFIOCCWDevice *vcdev)static VFIOGroup *vfio_ccw_get_group (S390CCWDevice *cdev,Error **errp) { -char *tmp, group_path[PATH_MAX ]; +char *tmp, *group_name, group_path[PATH_MAX ];ssize_t len;int groupid;
@@ -317,10 +317,13 @@ static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp) group_path[len] =0 ; -if (sscanf (basename (group_path),"%d" , &groupid) !=1 ) { + group_name =g_path_get_basename (group_path); +if (sscanf (group_name,"%d" , &groupid) !=1 ) {error_setg (errp,"vfio: failed to read %s" , group_path); +g_free (group_name);return NULL ; } +g_free (group_name);return vfio_get_group (groupid, &address_space_memory, errp); }
hw/vfio/pci.c +4 -2
@@ -2807,7 +2807,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)return ; } - vdev->vbasedev.name =g_strdup (basename (vdev->vbasedev.sysfsdev)); + vdev->vbasedev.name =g_path_get_basename (vdev->vbasedev.sysfsdev); vdev->vbasedev.ops = &vfio_pci_ops; vdev->vbasedev.type =VFIO_DEVICE_TYPE_PCI ; vdev->vbasedev.dev = &vdev->pdev.qdev;
@@ -2824,11 +2824,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) group_path[len] =0 ; - group_name =basename (group_path); + group_name =g_path_get_basename (group_path);if (sscanf (group_name,"%d" , &groupid) !=1 ) {error_setg_errno (errp, errno,"failed to read %s" , group_path); +g_free (group_name); goto error; } +g_free (group_name);trace_vfio_realize (vdev->vbasedev.name, groupid);
hw/vfio/platform.c +4 -2
@@ -561,7 +561,7 @@ static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)/* @sysfsdev takes precedence over @host */ if (vbasedev->sysfsdev) {g_free (vbasedev->name); - vbasedev->name =g_strdup (basename (vbasedev->sysfsdev)); + vbasedev->name =g_path_get_basename (vbasedev->sysfsdev); }else {if (!vbasedev->name ||strchr (vbasedev->name,'/' )) {error_setg (errp,"wrong host device name" );
@@ -590,11 +590,13 @@ static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp) group_path[len] =0 ; - group_name =basename (group_path); + group_name =g_path_get_basename (group_path);if (sscanf (group_name,"%d" , &groupid) !=1 ) {error_setg_errno (errp, errno,"failed to read %s" , group_path); +g_free (group_name);return -errno; } +g_free (group_name);trace_vfio_platform_base_device_init (vbasedev->name, groupid);
qemu-io.c +7 -1
@@ -308,6 +308,11 @@ static char *get_prompt(void)return prompt; } +static void free_progname (void ) +{ +g_free (progname); +} + static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, const char *fmt, ...) {
@@ -504,7 +509,8 @@ int main(int argc, char **argv)#endif module_call_init (MODULE_INIT_TRACE ); - progname =basename (argv[0 ]); + progname =g_path_get_basename (argv[0 ]); +atexit (free_progname);qemu_init_exec_dir (argv[0 ]);qcrypto_init (&error_fatal);
qemu-nbd.c +4 -1
@@ -893,8 +893,11 @@ int main(int argc, char **argv) }if (device !=NULL && sockpath ==NULL ) { +char *base_filename =g_path_get_basename (device); + sockpath =g_malloc (128 ); -snprintf (sockpath,128 ,SOCKET_PATH ,basename (device)); +snprintf (sockpath,128 ,SOCKET_PATH , base_filename); +g_free (base_filename); } server =qio_net_listener_new ();
qga/commands-posix.c +2 -2
@@ -808,7 +808,7 @@ static char *get_pci_driver(char const *syspath, int pathlen, Error **errp) len =readlink (dpath, buf,sizeof (buf) -1 );if (len !=-1 ) { buf[len] =0 ; - driver =g_strdup (basename (buf)); + driver =g_path_get_basename (buf); }g_free (dpath);g_free (path);
@@ -1053,7 +1053,7 @@ static void build_guest_fsinfo_for_device(char const *devpath, }if (!fs->name) { - fs->name =g_strdup (basename (syspath)); + fs->name =g_path_get_basename (syspath); }g_debug (" parse sysfs path '%s'" , syspath);
Signature · 1 line
2.1.4
Hi
Quoted 7 lines · Julia Suvorova via Qemu-devel
basename(3) and dirname(3) modify their argument and may return pointers to statically allocated memory which may be overwritten by subsequent calls.
g_path_get_basenameandg_path_get_dirnamehave no such issues, and therefore more preferable.Signed-off-by Julia Suvorova <[email protected]>
What about adding a warning for basename()/dirname() usage in scripts/checkpatch.pl ?
Quoted 230 lines
--- fsdev/virtfs-proxy-helper.c | 6 +++++- hw/s390x/s390-ccw.c | 17 +++++++++++------ hw/vfio/ccw.c | 7 +++++-- hw/vfio/pci.c | 6 ++++-- hw/vfio/platform.c | 6 ++++-- qemu-io.c | 8 +++++++- qemu-nbd.c | 5 ++++- qga/commands-posix.c | 4 ++-- 8 files changed, 42 insertions(+), 17 deletions(-)fsdev/virtfs-proxy-helper.c +5
-1@@ -787,6 +787,8 @@ error:static void usage (char *prog) { +char *base_filename =g_path_get_basename (prog); +fprintf (stderr,"usage: %s\n" " -p|--path <path> 9p path to export\n" " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"@@ -795,7 +797,9 @@ static void usage(char *prog)" access to this socket\n" " \tNote: -s & -f can not be used together\n" " [-n|--nodaemon] Run as a normal program\n" , - basename(prog)); + base_filename); + +g_free (base_filename); } static int process_reply(int sock, int type,hw/s390x/s390-ccw.c +11
-6@@ -34,7 +34,7 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, Error **errp) {unsigned int cssid, ssid, devid; -char dev_path[PATH_MAX ] = {0 }, *tmp; +char dev_path[PATH_MAX ] = {0 }, *dir_name, *dir_path;if (!sysfsdev) {error_setg (errp,"No host device provided" );@@ -48,18 +48,23 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev,return ; } - cdev->mdevid =g_strdup (basename (dev_path)); + cdev->mdevid =g_path_get_basename (dev_path); - tmp =basename (dirname (dev_path)); -if (sscanf (tmp,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) { -error_setg_errno (errp, errno,"Failed to read %s" , tmp); -return ; + dir_path =g_path_get_dirname (dev_path); + dir_name =g_path_get_basename (dir_path); +if (sscanf (dir_name,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) { +error_setg_errno (errp, errno,"Failed to read %s" , dir_name); + goto out; } cdev->hostid.cssid = cssid; cdev->hostid.ssid = ssid; cdev->hostid.devid = devid; cdev->hostid.valid = true; + +out: +g_free (dir_path); +g_free (dir_name); }static void s390_ccw_realize (S390CCWDevice *cdev,char *sysfsdev,Error **errp)hw/vfio/ccw.c +5
-2@@ -300,7 +300,7 @@ static void vfio_put_device(VFIOCCWDevice *vcdev)static VFIOGroup *vfio_ccw_get_group (S390CCWDevice *cdev,Error **errp) { -char *tmp, group_path[PATH_MAX ]; +char *tmp, *group_name, group_path[PATH_MAX ];ssize_t len;int groupid;@@ -317,10 +317,13 @@ static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp) group_path[len] =0 ; -if (sscanf (basename (group_path),"%d" , &groupid) !=1 ) { + group_name =g_path_get_basename (group_path); +if (sscanf (group_name,"%d" , &groupid) !=1 ) {error_setg (errp,"vfio: failed to read %s" , group_path); +g_free (group_name);return NULL ; } +g_free (group_name);return vfio_get_group (groupid, &address_space_memory, errp); }hw/vfio/pci.c +4
-2@@ -2807,7 +2807,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)return ; } - vdev->vbasedev.name =g_strdup (basename (vdev->vbasedev.sysfsdev)); + vdev->vbasedev.name =g_path_get_basename (vdev->vbasedev.sysfsdev); vdev->vbasedev.ops = &vfio_pci_ops; vdev->vbasedev.type =VFIO_DEVICE_TYPE_PCI ; vdev->vbasedev.dev = &vdev->pdev.qdev;@@ -2824,11 +2824,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) group_path[len] =0 ; - group_name =basename (group_path); + group_name =g_path_get_basename (group_path);if (sscanf (group_name,"%d" , &groupid) !=1 ) {error_setg_errno (errp, errno,"failed to read %s" , group_path); +g_free (group_name); goto error; } +g_free (group_name);trace_vfio_realize (vdev->vbasedev.name, groupid);hw/vfio/platform.c +4
-2@@ -561,7 +561,7 @@ static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)/* @sysfsdev takes precedence over @host */ if (vbasedev->sysfsdev) {g_free (vbasedev->name); - vbasedev->name =g_strdup (basename (vbasedev->sysfsdev)); + vbasedev->name =g_path_get_basename (vbasedev->sysfsdev); }else {if (!vbasedev->name ||strchr (vbasedev->name,'/' )) {error_setg (errp,"wrong host device name" );@@ -590,11 +590,13 @@ static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp) group_path[len] =0 ; - group_name =basename (group_path); + group_name =g_path_get_basename (group_path);if (sscanf (group_name,"%d" , &groupid) !=1 ) {error_setg_errno (errp, errno,"failed to read %s" , group_path); +g_free (group_name);return -errno; } +g_free (group_name);trace_vfio_platform_base_device_init (vbasedev->name, groupid);qemu-io.c +7
-1@@ -308,6 +308,11 @@ static char *get_prompt(void)return prompt; } +static void free_progname (void ) +{ +g_free (progname); +} + static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, const char *fmt, ...) {@@ -504,7 +509,8 @@ int main(int argc, char **argv)#endif module_call_init (MODULE_INIT_TRACE ); - progname =basename (argv[0 ]); + progname =g_path_get_basename (argv[0 ]); +atexit (free_progname);qemu_init_exec_dir (argv[0 ]);qcrypto_init (&error_fatal);qemu-nbd.c +4
-1@@ -893,8 +893,11 @@ int main(int argc, char **argv) }if (device !=NULL && sockpath ==NULL ) { +char *base_filename =g_path_get_basename (device); + sockpath =g_malloc (128 ); -snprintf (sockpath,128 ,SOCKET_PATH ,basename (device)); +snprintf (sockpath,128 ,SOCKET_PATH , base_filename); +g_free (base_filename); } server =qio_net_listener_new ();qga/commands-posix.c +2
-3@@ -808,7 +808,7 @@ static char *get_pci_driver(char const *syspath, int pathlen, Error **errp) len =readlink (dpath, buf,sizeof (buf) -1 );if (len !=-1 ) { buf[len] =0 ; - driver =g_strdup (basename (buf)); + driver =g_path_get_basename (buf); }g_free (dpath);g_free (path);@@ -1053,7 +1053,7 @@ static void build_guest_fsinfo_for_device(char const *devpath, }if (!fs->name) { - fs->name =g_strdup (basename (syspath)); + fs->name =g_path_get_basename (syspath); }g_debug (" parse sysfs path '%s'" , syspath); --2.1.4
Signature · 1 line
Marc-André Lureau
Cornelia Huck
Reviewed-byQuoted 10 lines · On Thu, 1 Mar 2018 10:47:42 +0100 Marc-André Lureau…
Hi
Quoted 6 lines · Julia Suvorova via Qemu-devel
basename(3) and dirname(3) modify their argument and may return pointers to statically allocated memory which may be overwritten by subsequent calls.
g_path_get_basenameandg_path_get_dirnamehave no such issues, and therefore more preferable.
Quoted 4 lines
Quoted 1 line
Signed-off-by Julia Suvorova <[email protected]>What about adding a warning for basename()/dirname() usage in
scripts/checkpatch.pl?
+1 to that.
Quoted 2 lines
Reviewed-by Marc-André Lureau <[email protected]>
Quoted 6 lines · Cornelia Huck
Quoted 4 lines
Quoted 2 lines
Signed-off-by Julia Suvorova <[email protected]>What about adding a warning for basename()/dirname() usage in
scripts/checkpatch.pl?+1 to that.
Good idea indeed. Julia, would you like to send a second patch that adds the warning?
There are already some examples (__FUNCTION__, strto*, signal, etc.) that you can take inspiration from.
Paolo
Quoted 11 lines · Paolo Bonzini
Quoted 6 lines · Cornelia Huck
Quoted 4 lines
Quoted 2 lines
Signed-off-by Julia Suvorova <[email protected]>What about adding a warning for basename()/dirname() usage in
scripts/checkpatch.pl?+1 to that.
Good idea indeed. Julia, would you like to send a second patch that adds the warning?
Ok, sure.
Quoted 3 lines
There are already some examples (
__FUNCTION__, strto*, signal, etc.) that you can take inspiration from.
Thanks for the tips.
Quoted 13 lines · Cornelia Huck
Quoted 10 lines · On Thu, 1 Mar 2018 10:47:42 +0100 Marc-André Lureau…
Hi
Quoted 6 lines · Julia Suvorova via Qemu-devel
basename(3) and dirname(3) modify their argument and may return pointers to statically allocated memory which may be overwritten by subsequent calls.
g_path_get_basenameandg_path_get_dirnamehave no such issues, and therefore more preferable.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Julia Suvorova <[email protected]>What about adding a warning for basename()/dirname() usage in
scripts/checkpatch.pl?+1 to that.
Ok. I'll prepare a separate patch for that.
Thanks for the review.
Quoted 5 lines
Quoted 2 lines
Reviewed-by Marc-André Lureau <[email protected]>Reviewed-by Cornelia Huck <[email protected]>
Quoted 9 lines · Julia Suvorova
qemu-io.c +5
-0+static void free_progname (void ) +{ +g_free (progname); +} + static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, const char *fmt, ...) {
qemu-io.c int main(int argc, char **argv) · line 513
Quoted 6 lines
qemu-io.c +2
-1@@ -504,7 +509,8 @@ int main(int argc, char **argv)#endif module_call_init (MODULE_INIT_TRACE ); - progname =basename (argv[0 ]); + progname =g_path_get_basename (argv[0 ]); +atexit (free_progname);
This atexit is not needed; memory is always freed on exit. But, it's good that you thought about it!
I can queue this patch for merging once I get an ack from Alex Williamson (VFIO).
Thanks,
Paolo
Quoted 5 lines · On Thu, 1 Mar 2018 10:08:06 +0300 Julia Suvorova via Qemu-devel…
basename(3) and dirname(3) modify their argument and may return pointers to statically allocated memory which may be overwritten by subsequent calls.
g_path_get_basenameandg_path_get_dirnamehave no such issues, and therefore more preferable.
I think it's quite a bit arguable whether it's preferable, afaict there are no bugs fixed here. The basic functions are being used correctly and are more conservative of memory usage than the glib variants. In several cases below the basic functions seem far more efficient and we don't need to worry about freeing unnecessarily allocated memory, the diffstat seems to attest to this. Is the inefficiency and possibility of leaking unintentionally allocated memory a statistical improvement over the claimed perils of these absolutely standard and well known functions?
I know mine is a losing battle against glib, but the changelog might as well just say "Succumb to glib" because I fail to see that there's actually an improvement here. If Paolo wants to take this,
Thanks,
Alex
Quoted 227 lines
Signed-off-by Julia Suvorova <[email protected]>--- fsdev/virtfs-proxy-helper.c | 6 +++++- hw/s390x/s390-ccw.c | 17 +++++++++++------ hw/vfio/ccw.c | 7 +++++-- hw/vfio/pci.c | 6 ++++-- hw/vfio/platform.c | 6 ++++-- qemu-io.c | 8 +++++++- qemu-nbd.c | 5 ++++- qga/commands-posix.c | 4 ++-- 8 files changed, 42 insertions(+), 17 deletions(-)fsdev/virtfs-proxy-helper.c +5
-1@@ -787,6 +787,8 @@ error:static void usage (char *prog) { +char *base_filename =g_path_get_basename (prog); +fprintf (stderr,"usage: %s\n" " -p|--path <path> 9p path to export\n" " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"@@ -795,7 +797,9 @@ static void usage(char *prog)" access to this socket\n" " \tNote: -s & -f can not be used together\n" " [-n|--nodaemon] Run as a normal program\n" , - basename(prog)); + base_filename); + +g_free (base_filename); } static int process_reply(int sock, int type,hw/s390x/s390-ccw.c +11
-6@@ -34,7 +34,7 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev, Error **errp) {unsigned int cssid, ssid, devid; -char dev_path[PATH_MAX ] = {0 }, *tmp; +char dev_path[PATH_MAX ] = {0 }, *dir_name, *dir_path;if (!sysfsdev) {error_setg (errp,"No host device provided" );@@ -48,18 +48,23 @@ static void s390_ccw_get_dev_info(S390CCWDevice *cdev,return ; } - cdev->mdevid =g_strdup (basename (dev_path)); + cdev->mdevid =g_path_get_basename (dev_path); - tmp =basename (dirname (dev_path)); -if (sscanf (tmp,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) { -error_setg_errno (errp, errno,"Failed to read %s" , tmp); -return ; + dir_path =g_path_get_dirname (dev_path); + dir_name =g_path_get_basename (dir_path); +if (sscanf (dir_name,"%2x.%1x.%4x" , &cssid, &ssid, &devid) !=3 ) { +error_setg_errno (errp, errno,"Failed to read %s" , dir_name); + goto out; } cdev->hostid.cssid = cssid; cdev->hostid.ssid = ssid; cdev->hostid.devid = devid; cdev->hostid.valid = true; + +out: +g_free (dir_path); +g_free (dir_name); }static void s390_ccw_realize (S390CCWDevice *cdev,char *sysfsdev,Error **errp)hw/vfio/ccw.c +5
-2@@ -300,7 +300,7 @@ static void vfio_put_device(VFIOCCWDevice *vcdev)static VFIOGroup *vfio_ccw_get_group (S390CCWDevice *cdev,Error **errp) { -char *tmp, group_path[PATH_MAX ]; +char *tmp, *group_name, group_path[PATH_MAX ];ssize_t len;int groupid;@@ -317,10 +317,13 @@ static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp) group_path[len] =0 ; -if (sscanf (basename (group_path),"%d" , &groupid) !=1 ) { + group_name =g_path_get_basename (group_path); +if (sscanf (group_name,"%d" , &groupid) !=1 ) {error_setg (errp,"vfio: failed to read %s" , group_path); +g_free (group_name);return NULL ; } +g_free (group_name);return vfio_get_group (groupid, &address_space_memory, errp); }hw/vfio/pci.c +4
-2@@ -2807,7 +2807,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)return ; } - vdev->vbasedev.name =g_strdup (basename (vdev->vbasedev.sysfsdev)); + vdev->vbasedev.name =g_path_get_basename (vdev->vbasedev.sysfsdev); vdev->vbasedev.ops = &vfio_pci_ops; vdev->vbasedev.type =VFIO_DEVICE_TYPE_PCI ; vdev->vbasedev.dev = &vdev->pdev.qdev;@@ -2824,11 +2824,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) group_path[len] =0 ; - group_name =basename (group_path); + group_name =g_path_get_basename (group_path);if (sscanf (group_name,"%d" , &groupid) !=1 ) {error_setg_errno (errp, errno,"failed to read %s" , group_path); +g_free (group_name); goto error; } +g_free (group_name);trace_vfio_realize (vdev->vbasedev.name, groupid);hw/vfio/platform.c +4
-2@@ -561,7 +561,7 @@ static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)/* @sysfsdev takes precedence over @host */ if (vbasedev->sysfsdev) {g_free (vbasedev->name); - vbasedev->name =g_strdup (basename (vbasedev->sysfsdev)); + vbasedev->name =g_path_get_basename (vbasedev->sysfsdev); }else {if (!vbasedev->name ||strchr (vbasedev->name,'/' )) {error_setg (errp,"wrong host device name" );@@ -590,11 +590,13 @@ static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp) group_path[len] =0 ; - group_name =basename (group_path); + group_name =g_path_get_basename (group_path);if (sscanf (group_name,"%d" , &groupid) !=1 ) {error_setg_errno (errp, errno,"failed to read %s" , group_path); +g_free (group_name);return -errno; } +g_free (group_name);trace_vfio_platform_base_device_init (vbasedev->name, groupid);qemu-io.c +7
-1@@ -308,6 +308,11 @@ static char *get_prompt(void)return prompt; } +static void free_progname (void ) +{ +g_free (progname); +} + static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, const char *fmt, ...) {@@ -504,7 +509,8 @@ int main(int argc, char **argv)#endif module_call_init (MODULE_INIT_TRACE ); - progname =basename (argv[0 ]); + progname =g_path_get_basename (argv[0 ]); +atexit (free_progname);qemu_init_exec_dir (argv[0 ]);qcrypto_init (&error_fatal);qemu-nbd.c +4
-1@@ -893,8 +893,11 @@ int main(int argc, char **argv) }if (device !=NULL && sockpath ==NULL ) { +char *base_filename =g_path_get_basename (device); + sockpath =g_malloc (128 ); -snprintf (sockpath,128 ,SOCKET_PATH ,basename (device)); +snprintf (sockpath,128 ,SOCKET_PATH , base_filename); +g_free (base_filename); } server =qio_net_listener_new ();qga/commands-posix.c +2
-2@@ -808,7 +808,7 @@ static char *get_pci_driver(char const *syspath, int pathlen, Error **errp) len =readlink (dpath, buf,sizeof (buf) -1 );if (len !=-1 ) { buf[len] =0 ; - driver =g_strdup (basename (buf)); + driver =g_path_get_basename (buf); }g_free (dpath);g_free (path);@@ -1053,7 +1053,7 @@ static void build_guest_fsinfo_for_device(char const *devpath, }if (!fs->name) { - fs->name =g_strdup (basename (syspath)); + fs->name =g_path_get_basename (syspath); }g_debug (" parse sysfs path '%s'" , syspath);
Quoted 16 lines · Alex Williamson
Quoted 5 lines
basename(3) and dirname(3) modify their argument and may return pointers to statically allocated memory which may be overwritten by subsequent calls.
g_path_get_basenameandg_path_get_dirnamehave no such issues, and therefore more preferable.I think it's quite a bit arguable whether it's preferable, afaict there are no bugs fixed here. The basic functions are being used correctly and are more conservative of memory usage than the glib variants. In several cases below the basic functions seem far more efficient and we don't need to worry about freeing unnecessarily allocated memory, the diffstat seems to attest to this. Is the inefficiency and possibility of leaking unintentionally allocated memory a statistical improvement over the claimed perils of these absolutely standard and well known functions?
Quoted 5 lines
I know mine is a losing battle against glib, but the changelog might as well just say "Succumb to glib" because I fail to see that there's actually an improvement here. If Paolo wants to take this,
Acked-by Alex Williamson <[email protected]>
Fair enough, I'll apply only the obvious cases like g_strdup(basename()).
Paolo
Quoted 9 lines · Alex Williamson
Quoted 5 lines · On Thu, 1 Mar 2018 10:08:06 +0300 Julia Suvorova via Qemu-devel…
basename(3) and dirname(3) modify their argument and may return pointers to statically allocated memory which may be overwritten by subsequent calls.
g_path_get_basenameandg_path_get_dirnamehave no such issues, and therefore more preferable.
Quoted 9 lines
I think it's quite a bit arguable whether it's preferable, afaict there are no bugs fixed here. The basic functions are being used correctly and are more conservative of memory usage than the glib variants. In several cases below the basic functions seem far more efficient and we don't need to worry about freeing unnecessarily allocated memory, the diffstat seems to attest to this. Is the inefficiency and possibility of leaking unintentionally allocated memory a statistical improvement over the claimed perils of these absolutely standard and well known functions?
The standard functions may be well-known, but as currently standardized are not very reliably useful in a multi-threaded program. However, the standard WILL be improved in the future to require a saner standardized implementation (the proposed changes to the standard remove the ability for basename()/dirname() to return NULL, remove the allowance for non-thread-safe returns, and document more fully that the returned pointer will be back to the input string in all but a few corner cases which return const char *):
http://austingroupbugs.net/view.php?id=1064
Quoted 4 lines
I know mine is a losing battle against glib, but the changelog might as well just say "Succumb to glib" because I fail to see that there's actually an improvement here. If Paolo wants to take this,
One thing that the glib functions can do that the standardized version cannot do is handle drive letters on DOS-like systems (doesn't affect our builds on Linux or BSD, but may matter to mingw builds). But you are right that the glib functions are more malloc-heavy than the (fixed) standardized functions, so it becomes a trade-off of how likely the libc functions are to comply with the tighter future standard version (that is actually somewhat useful) vs. the current standard version (that is a piece of trash because it has too many loopholes).
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org