[libvirt] [PATCH 16/20] xen: Remove PATH_MAX sized stack allocation from block stats code

Daniel P. Berrange berrange at redhat.com
Mon Apr 4 10:40:23 UTC 2011


On Sun, Apr 03, 2011 at 11:21:29AM +0200, Matthias Bolte wrote:
> ---
>  src/xen/block_stats.c |   52 +++++++++++++++++++++++++-----------------------
>  1 files changed, 27 insertions(+), 25 deletions(-)
> 
> diff --git a/src/xen/block_stats.c b/src/xen/block_stats.c
> index e7c80c9..6b4171d 100644
> --- a/src/xen/block_stats.c
> +++ b/src/xen/block_stats.c
> @@ -113,34 +113,36 @@ read_stat (const char *path)
>  }
>  
>  static int64_t
> -read_bd_stat (int device, int domid, const char *str)
> +read_bd_stat(int device, int domid, const char *str)
>  {
> -    char path[PATH_MAX];
> +    static const char *paths[] = {
> +        "/sys/bus/xen-backend/devices/vbd-%d-%d/statistics/%s",
> +        "/sys/bus/xen-backend/devices/tap-%d-%d/statistics/%s",
> +        "/sys/devices/xen-backend/vbd-%d-%d/statistics/%s",
> +        "/sys/devices/xen-backend/tap-%d-%d/statistics/%s",
> +        NULL
> +    };

If you leave out the trailing NULL

> +
> +    int i;
> +    char *path;
>      int64_t r;
>  
> -    snprintf (path, sizeof path,
> -              "/sys/bus/xen-backend/devices/vbd-%d-%d/statistics/%s",
> -              domid, device, str);
> -    r = read_stat (path);
> -    if (r >= 0) return r;
> -
> -    snprintf (path, sizeof path,
> -              "/sys/bus/xen-backend/devices/tap-%d-%d/statistics/%s",
> -              domid, device, str);
> -    r = read_stat (path);
> -    if (r >= 0) return r;
> -
> -    snprintf (path, sizeof path,
> -              "/sys/devices/xen-backend/vbd-%d-%d/statistics/%s",
> -              domid, device, str);
> -    r = read_stat (path);
> -    if (r >= 0) return r;
> -
> -    snprintf (path, sizeof path,
> -              "/sys/devices/xen-backend/tap-%d-%d/statistics/%s",
> -              domid, device, str);
> -    r = read_stat (path);
> -    return r;
> +    for (i = 0; paths[i] != NULL; ++i) {

you can just use  for (i = 0 ; i < ARRAY_CARDINALITY(paths) ; i++)
which I find slightly clearer.

> +        if (virAsprintf(&path, paths[i], domid, device, str) < 0) {
> +            virReportOOMError();
> +            return -1;
> +        }
> +
> +        r = read_stat(path);
> +
> +        VIR_FREE(path);
> +
> +        if (r >= 0) {
> +            return r;
> +        }
> +    }
> +
> +    return -1;
>  }


Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list