From e37aedde2e7f2afa69ac63bdfc80cc103b19469d Mon Sep 17 00:00:00 2001 From: "Christopher N. Hesse" Date: Wed, 5 Apr 2017 18:55:28 +0200 Subject: [PATCH] lights: Put back fd checks before closing While close(NULL) is indeed a harmless noop, fd can end up being < 0 for us. Change-Id: I56dcd7fb61c72d3ce750b13329ff42e11ab63c84 --- liblights/lights_helper.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/liblights/lights_helper.c b/liblights/lights_helper.c index 6ac18bf..be59399 100644 --- a/liblights/lights_helper.c +++ b/liblights/lights_helper.c @@ -31,7 +31,7 @@ * Reads an Integer from a file. * * @param path The absolute path string. - * @return The Integer with decimal base, -1 or errno on error. + * @return The Integer with decimal base, -1 or -errno on error. */ static int read_int(char const *path) { @@ -66,7 +66,8 @@ static int read_int(char const *path) } exit: - close(fd); + if (fd >= 0) + close(fd); return ret; } @@ -75,7 +76,7 @@ exit: * * @param path The absolute path string. * @param value The Integer value to be written. - * @return 0 on success, errno on error. + * @return 0 on success, -errno on error. */ static int write_int(char const *path, const int value) { @@ -100,7 +101,8 @@ static int write_int(char const *path, const int value) } exit: - close(fd); + if (fd >= 0) + close(fd); return ret; } -- 2.20.1