[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCH 04/10] storage: avoid memory leak on stat failure



On Thu, Jun 02, 2011 at 05:07:56PM -0600, Eric Blake wrote:
> Spotted by coverity.  Triggers on failed stat, although I'm not sure
> how easy that condition is, so I'm not sure if this is a runtime
> memory hog.  Regression introduced in commit 8077d64 (unreleased).
> 
> * src/util/storage_file.c (virStorageFileGetMetadataFromFD):
> Reduce need for malloc, avoiding a leak.
> ---
>  src/util/storage_file.c |   15 +++++++--------
>  1 files changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/src/util/storage_file.c b/src/util/storage_file.c
> index 8dbd933..6b3b756 100644
> --- a/src/util/storage_file.c
> +++ b/src/util/storage_file.c
> @@ -831,11 +831,6 @@ virStorageFileGetMetadataFromFD(const char *path,
>      int ret = -1;
>      struct stat sb;
> 
> -    if (VIR_ALLOC_N(head, len) < 0) {
> -        virReportOOMError();
> -        return -1;
> -    }
> -
>      memset(meta, 0, sizeof (*meta));
> 
>      if (fstat(fd, &sb) < 0) {
> @@ -847,13 +842,17 @@ virStorageFileGetMetadataFromFD(const char *path,
> 
>      /* No header to probe for directories */
>      if (S_ISDIR(sb.st_mode)) {
> -        ret = 0;
> -        goto cleanup;
> +        return 0;
>      }
> 
>      if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
>          virReportSystemError(errno, _("cannot seek to start of '%s'"), path);
> -        goto cleanup;
> +        return -1;
> +    }
> +
> +    if (VIR_ALLOC_N(head, len) < 0) {
> +        virReportOOMError();
> +        return -1;
>      }
> 
>      if ((len = read(fd, head, len)) < 0) {

ACK,

Daniel

-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
daniel veillard com  | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library  http://libvirt.org/


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]