Richard W.M. Jones
2017-Apr-19 16:47 UTC
[Libguestfs] [PATCH] daemon: Remove use of fixed-size stack buffers.
GCC 7 complains that the fixed size buffers are not large enough
(at least in theory) when using ‘-O3 -mtune=broadwell’.
---
daemon/devsparts.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/daemon/devsparts.c b/daemon/devsparts.c
index 584e7d8b8..eac79197e 100644
--- a/daemon/devsparts.c
+++ b/daemon/devsparts.c
@@ -125,13 +125,16 @@ foreach_block_device (block_dev_func_t func, bool
return_md)
static int
add_device (const char *device, struct stringsbuf *r)
{
- char dev_path[256];
- snprintf (dev_path, sizeof dev_path, "/dev/%s", device);
+ CLEANUP_FREE char *dev_path;
- if (add_string (r, dev_path) == -1) {
+ if (asprintf (&dev_path, "/dev/%s", device) == -1) {
+ reply_with_perror ("asprintf");
return -1;
}
+ if (add_string (r, dev_path) == -1)
+ return -1;
+
return 0;
}
@@ -153,10 +156,13 @@ do_list_devices (void)
static int
add_partitions (const char *device, struct stringsbuf *r)
{
- char devdir[256];
+ CLEANUP_FREE char *devdir;
/* Open the device's directory under /sys/block */
- snprintf (devdir, sizeof devdir, "/sys/block/%s", device);
+ if (asprintf (&devdir, "/sys/block/%s", device) == -1) {
+ reply_with_perror ("asprintf");
+ return -1;
+ }
DIR *dir = opendir (devdir);
if (!dir) {
--
2.12.0
Pino Toscano
2017-Apr-19 16:53 UTC
Re: [Libguestfs] [PATCH] daemon: Remove use of fixed-size stack buffers.
On Wednesday, 19 April 2017 18:47:11 CEST Richard W.M. Jones wrote:> GCC 7 complains that the fixed size buffers are not large enough > (at least in theory) when using ‘-O3 -mtune=broadwell’. > --- > daemon/devsparts.c | 16 +++++++++++----- > 1 file changed, 11 insertions(+), 5 deletions(-) > > diff --git a/daemon/devsparts.c b/daemon/devsparts.c > index 584e7d8b8..eac79197e 100644 > --- a/daemon/devsparts.c > +++ b/daemon/devsparts.c > @@ -125,13 +125,16 @@ foreach_block_device (block_dev_func_t func, bool return_md) > static int > add_device (const char *device, struct stringsbuf *r) > { > - char dev_path[256]; > - snprintf (dev_path, sizeof dev_path, "/dev/%s", device); > + CLEANUP_FREE char *dev_path;Please init it to NULL, so it is not a problem in case new code is added between this and what follows. OTOH ...> > - if (add_string (r, dev_path) == -1) { > + if (asprintf (&dev_path, "/dev/%s", device) == -1) { > + reply_with_perror ("asprintf"); > return -1; > } > > + if (add_string (r, dev_path) == -1) > + return -1;... this could be add_string_nodup, handing over the dev_path string to the stringbuf, and thus removing one memory allocation.> + > return 0; > } > > @@ -153,10 +156,13 @@ do_list_devices (void) > static int > add_partitions (const char *device, struct stringsbuf *r) > { > - char devdir[256]; > + CLEANUP_FREE char *devdir;Same note here about the initialization to NULL. Thanks, -- Pino Toscano
Reasonably Related Threads
- [PATCH v2] daemon: Remove use of fixed-size stack buffers.
- [PATCH v2] daemon: Remove use of fixed-size stack buffers.
- [PATCH 1/2] daemon: Fix part-to-dev when the partition name includes p<N>.
- [PATCH] Recognise cd-rom devices in devsparts.c
- [PATCH 05/27] daemon: Reimplement several devsparts APIs in OCaml.