[libvirt] [PATCH 01/20] Remove PATH_MAX sized stack allocations related to virFileBuildPath

Daniel P. Berrange berrange at redhat.com
Mon Apr 4 10:22:13 UTC 2011


On Sun, Apr 03, 2011 at 11:21:14AM +0200, Matthias Bolte wrote:
> Make virFileBuildPath operate on the heap instead of the stack. It
> allocates a buffer instead of expecting a preexisting buffer.
> ---
>  src/conf/nwfilter_conf.c |   21 +++++++--------------
>  src/conf/storage_conf.c  |   44 +++++++++++++++-----------------------------
>  src/util/util.c          |   29 +++++++++++++++--------------
>  src/util/util.h          |    8 +++-----
>  src/xen/xen_inotify.c    |   16 +++++++++-------
>  src/xen/xm_internal.c    |   33 +++++++++++++++++----------------
>  6 files changed, 66 insertions(+), 85 deletions(-)


> -        if (!(pool->autostartLink = strdup(path))) {
> +        if (!(pool->autostartLink = virFileBuildPath(driver->autostartDir,
> +                                                     def->name, ".xml"))) {
>              virReportOOMError();
>              VIR_FREE(pool->configFile);
>              return -1;
> diff --git a/src/util/util.c b/src/util/util.c
> index 43794b1..31feecc 100644
> --- a/src/util/util.c
> +++ b/src/util/util.c
> @@ -1841,23 +1841,24 @@ cleanup:
>      return err;
>  }
>  
> -/* Build up a fully qualfiied path for a config file to be
> +/* Build up a fully qualified path for a config file to be
>   * associated with a persistent guest or network */
> -int virFileBuildPath(const char *dir,
> -                     const char *name,
> -                     const char *ext,
> -                     char *buf,
> -                     unsigned int buflen)
> +char *
> +virFileBuildPath(const char *dir, const char *name, const char *ext)
>  {
> -    if ((strlen(dir) + 1 + strlen(name) + (ext ? strlen(ext) : 0) + 1) >= (buflen-1))
> -        return -1;
> +    char *path;
>  
> -    strcpy(buf, dir);
> -    strcat(buf, "/");
> -    strcat(buf, name);
> -    if (ext)
> -        strcat(buf, ext);
> -    return 0;
> +    if (ext == NULL) {
> +        if (virAsprintf(&path, "%s/%s", dir, name) < 0) {
> +            return NULL;
> +        }
> +    } else {
> +        if (virAsprintf(&path, "%s/%s%s", dir, name, ext) < 0) {
> +            return NULL;
> +        }
> +    }
> +
> +    return path;
>  }


ACK to the patch, but we might like to make virFileBuildPath invoke
virReportOOMError() itself as a followup, so callers don't have to
worry about it

Regards,
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