Fixed NULL_RETURNS issues introduced by remote images code.

Submitted by Rodrigo Bruno on March 10, 2017, 9:43 a.m.

Details

Message ID 1489139019-17879-1-git-send-email-rbruno@gsd.inesc-id.pt
State New
Series "Fixed NULL_RETURNS issues introduced by remote images code."
Headers show

Commit Message

Rodrigo Bruno March 10, 2017, 9:43 a.m.
Signed-off-by: Rodrigo Bruno <rbruno@gsd.inesc-id.pt>
---
 criu/image.c | 15 ++++++++++-----
 criu/util.c  |  5 +++++
 2 files changed, 15 insertions(+), 5 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/image.c b/criu/image.c
index 57d3c7b..384ee51 100644
--- a/criu/image.c
+++ b/criu/image.c
@@ -332,9 +332,14 @@  int do_open_remote_image(int dfd, char *path, int flags)
 	/* When using namespaces, the current dir is changed so we need to
 	 * change to previous working dir and back to correctly open the image
 	 * proxy and cache sockets. */
-	int save = dirfd(opendir("."));
+	DIR  *save = opendir(".");
+	if (save == NULL) {
+		pr_error("unable to open current working directory");
+		return -1;
+	}
+
 	if (fchdir(get_service_fd(IMG_FD_OFF)) < 0) {
-		pr_debug("fchdir to dfd failed!\n");
+		pr_error("fchdir to dfd failed");
 		return -1;
 	}
 
@@ -352,11 +357,11 @@  int do_open_remote_image(int dfd, char *path, int flags)
 		ret = write_remote_image_connection(snapshot_id, path, O_WRONLY);
 	}
 
-	if (fchdir(save) < 0) {
-		pr_debug("fchdir to save failed!\n");
+	if (fchdir(dirfd(save)) < 0) {
+		pr_error("fchdir to saved dir failed");
 		return -1;
 	}
-	close(save);
+	closedir(save);
 
 	return ret;
 }
diff --git a/criu/util.c b/criu/util.c
index 9fd8ba2..d33ce0e 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -522,6 +522,11 @@  int copy_file(int fd_in, int fd_out, size_t bytes)
 	char *buffer = (char*) malloc(chunk);
 	ssize_t ret;
 
+	if (buffer == NULL) {
+		pr_err("Failed to allocate buffer to copy file.\n");
+		return -1;
+	}
+
 	while (1) {
 		if (opts.remote) {
 			ret = read(fd_in, buffer, chunk);

Comments

Andrey Vagin March 13, 2017, 8:37 p.m.
On Fri, Mar 10, 2017 at 09:43:39AM +0000, Rodrigo Bruno wrote:
> Signed-off-by: Rodrigo Bruno <rbruno@gsd.inesc-id.pt>
> ---
>  criu/image.c | 15 ++++++++++-----
>  criu/util.c  |  5 +++++
>  2 files changed, 15 insertions(+), 5 deletions(-)
> 
> diff --git a/criu/image.c b/criu/image.c
> index 57d3c7b..384ee51 100644
> --- a/criu/image.c
> +++ b/criu/image.c
> @@ -332,9 +332,14 @@ int do_open_remote_image(int dfd, char *path, int flags)
>  	/* When using namespaces, the current dir is changed so we need to
>  	 * change to previous working dir and back to correctly open the image
>  	 * proxy and cache sockets. */
> -	int save = dirfd(opendir("."));
> +	DIR  *save = opendir(".");
> +	if (save == NULL) {
> +		pr_error("unable to open current working directory");
> +		return -1;
> +	}
> +
>  	if (fchdir(get_service_fd(IMG_FD_OFF)) < 0) {
> -		pr_debug("fchdir to dfd failed!\n");
> +		pr_error("fchdir to dfd failed");

I think you mean pr_perror ?

>  		return -1;
>  	}
>  
> @@ -352,11 +357,11 @@ int do_open_remote_image(int dfd, char *path, int flags)
>  		ret = write_remote_image_connection(snapshot_id, path, O_WRONLY);
>  	}
>  
> -	if (fchdir(save) < 0) {
> -		pr_debug("fchdir to save failed!\n");
> +	if (fchdir(dirfd(save)) < 0) {
> +		pr_error("fchdir to saved dir failed");
>  		return -1;
>  	}
> -	close(save);
> +	closedir(save);
>  
>  	return ret;
>  }
> diff --git a/criu/util.c b/criu/util.c
> index 9fd8ba2..d33ce0e 100644
> --- a/criu/util.c
> +++ b/criu/util.c
> @@ -522,6 +522,11 @@ int copy_file(int fd_in, int fd_out, size_t bytes)
>  	char *buffer = (char*) malloc(chunk);
>  	ssize_t ret;
>  
> +	if (buffer == NULL) {
> +		pr_err("Failed to allocate buffer to copy file.\n");
> +		return -1;
> +	}
> +
>  	while (1) {
>  		if (opts.remote) {
>  			ret = read(fd_in, buffer, chunk);
> -- 
> 1.9.1
> 
> _______________________________________________
> CRIU mailing list
> CRIU@openvz.org
> https://lists.openvz.org/mailman/listinfo/criu
Rodrigo Bruno March 14, 2017, 11:19 p.m.
Yes, sorry.

I have just sent a second version of the patch.

2017-03-13 20:37 GMT+00:00 Andrei Vagin <avagin@virtuozzo.com>:

> On Fri, Mar 10, 2017 at 09:43:39AM +0000, Rodrigo Bruno wrote:
> > Signed-off-by: Rodrigo Bruno <rbruno@gsd.inesc-id.pt>
> > ---
> >  criu/image.c | 15 ++++++++++-----
> >  criu/util.c  |  5 +++++
> >  2 files changed, 15 insertions(+), 5 deletions(-)
> >
> > diff --git a/criu/image.c b/criu/image.c
> > index 57d3c7b..384ee51 100644
> > --- a/criu/image.c
> > +++ b/criu/image.c
> > @@ -332,9 +332,14 @@ int do_open_remote_image(int dfd, char *path, int
> flags)
> >       /* When using namespaces, the current dir is changed so we need to
> >        * change to previous working dir and back to correctly open the
> image
> >        * proxy and cache sockets. */
> > -     int save = dirfd(opendir("."));
> > +     DIR  *save = opendir(".");
> > +     if (save == NULL) {
> > +             pr_error("unable to open current working directory");
> > +             return -1;
> > +     }
> > +
> >       if (fchdir(get_service_fd(IMG_FD_OFF)) < 0) {
> > -             pr_debug("fchdir to dfd failed!\n");
> > +             pr_error("fchdir to dfd failed");
>
> I think you mean pr_perror ?
>
> >               return -1;
> >       }
> >
> > @@ -352,11 +357,11 @@ int do_open_remote_image(int dfd, char *path, int
> flags)
> >               ret = write_remote_image_connection(snapshot_id, path,
> O_WRONLY);
> >       }
> >
> > -     if (fchdir(save) < 0) {
> > -             pr_debug("fchdir to save failed!\n");
> > +     if (fchdir(dirfd(save)) < 0) {
> > +             pr_error("fchdir to saved dir failed");
> >               return -1;
> >       }
> > -     close(save);
> > +     closedir(save);
> >
> >       return ret;
> >  }
> > diff --git a/criu/util.c b/criu/util.c
> > index 9fd8ba2..d33ce0e 100644
> > --- a/criu/util.c
> > +++ b/criu/util.c
> > @@ -522,6 +522,11 @@ int copy_file(int fd_in, int fd_out, size_t bytes)
> >       char *buffer = (char*) malloc(chunk);
> >       ssize_t ret;
> >
> > +     if (buffer == NULL) {
> > +             pr_err("Failed to allocate buffer to copy file.\n");
> > +             return -1;
> > +     }
> > +
> >       while (1) {
> >               if (opts.remote) {
> >                       ret = read(fd_in, buffer, chunk);
> > --
> > 1.9.1
> >
> > _______________________________________________
> > CRIU mailing list
> > CRIU@openvz.org
> > https://lists.openvz.org/mailman/listinfo/criu
>