[3/7] test/zdtm: Move assignment after return value check

Submitted by Rikard Falkeborn on April 28, 2019, 6:22 p.m.

Details

Message ID 20190428182255.5641-4-rikard.falkeborn@gmail.com
State Accepted
Series "Misc fixes found by cppcheck"
Commit 07e7c53723188dcb77bf1f6a3e10ca5a28990d98
Headers show

Commit Message

Rikard Falkeborn April 28, 2019, 6:22 p.m.
If read() fails we can not use the return value as index. Move the use
of it to after the error check to avoid this.

Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com>
---
 test/zdtm/lib/ns.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/test/zdtm/lib/ns.c b/test/zdtm/lib/ns.c
index 6b4a7539..3099f749 100644
--- a/test/zdtm/lib/ns.c
+++ b/test/zdtm/lib/ns.c
@@ -325,11 +325,11 @@  int ns_init(int argc, char **argv)
 		exit(1);
 	}
 	ret = read(fd, buf, sizeof(buf) - 1);
-	buf[ret] = '\0';
 	if (ret == -1) {
 		fprintf(stderr, "read() failed: %m\n");
 		exit(1);
 	}
+	buf[ret] = '\0';
 
 	pid = atoi(buf);
 	fprintf(stderr, "kill(%d, SIGTERM)\n", pid);

Comments

Dmitry Safonov April 29, 2019, 2:50 p.m.
On Sun, 28 Apr 2019 at 19:45, Rikard Falkeborn
<rikard.falkeborn@gmail.com> wrote:
>
> If read() fails we can not use the return value as index. Move the use
> of it to after the error check to avoid this.
>
> Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com>

Reviewed-by: Dmitry Safonov <0x7f454c46@gmail.com>

> ---
>  test/zdtm/lib/ns.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/test/zdtm/lib/ns.c b/test/zdtm/lib/ns.c
> index 6b4a7539..3099f749 100644
> --- a/test/zdtm/lib/ns.c
> +++ b/test/zdtm/lib/ns.c
> @@ -325,11 +325,11 @@ int ns_init(int argc, char **argv)
>                 exit(1);
>         }
>         ret = read(fd, buf, sizeof(buf) - 1);
> -       buf[ret] = '\0';
>         if (ret == -1) {
>                 fprintf(stderr, "read() failed: %m\n");
>                 exit(1);
>         }
> +       buf[ret] = '\0';
>
>         pid = atoi(buf);
>         fprintf(stderr, "kill(%d, SIGTERM)\n", pid);
> --
> 2.21.0
>
> _______________________________________________
> CRIU mailing list
> CRIU@openvz.org
> https://lists.openvz.org/mailman/listinfo/criu