From 3786fdc51b18b74411a4d5cca9af2aa3ff250505 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 10 Apr 2014 10:40:32 +0200 Subject: echo_srv: Fix resource leak of fd in pidfile(). Signed-off-by: Andreas Schneider Reviewed-by: Jakub Hrozek Reviewed-by: Simo Sorce --- tests/echo_srv.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/echo_srv.c b/tests/echo_srv.c index 4c65d19..e7e6e6c 100644 --- a/tests/echo_srv.c +++ b/tests/echo_srv.c @@ -53,7 +53,9 @@ static int pidfile(const char *path) { int err; int fd; - char pid_str[32]; + char pid_str[32] = { 0 }; + ssize_t nwritten; + size_t len; fd = open(path, O_RDONLY, 0644); err = errno; @@ -70,9 +72,14 @@ static int pidfile(const char *path) return err; } - memset(pid_str, 0, sizeof(pid_str)); snprintf(pid_str, sizeof(pid_str) -1, "%u\n", (unsigned int) getpid()); - write(fd, pid_str, strlen(pid_str)); + len = strlen(pid_str); + + nwritten = write(fd, pid_str, len); + close(fd); + if (nwritten != (ssize_t)len) { + return EIO; + } return 0; } -- cgit