45 lines
1.6 KiB
Diff
45 lines
1.6 KiB
Diff
From 2a85ab00b30f1d3a3212fecfad1138cc2d54af3b Mon Sep 17 00:00:00 2001
|
|
From: Zhao Wei Liew <zhaoweiliew@gmail.com>
|
|
Date: Wed, 31 Oct 2018 14:05:57 +0000
|
|
Subject: [PATCH] liblight: Fix potential fd leak
|
|
|
|
Currently, when the value to be written to an LED file does not
|
|
fit into the buffer, the opened file descriptor is not closed,
|
|
leaking the file descriptor.
|
|
|
|
Fix it by closing the file descriptor before returning.
|
|
|
|
Change-Id: I1c46fcc35f560358b1f60e0fac45f4c5a42cd484
|
|
---
|
|
liblight/lights.c | 10 ++++++++--
|
|
1 file changed, 8 insertions(+), 2 deletions(-)
|
|
|
|
diff --git a/liblight/lights.c b/liblight/lights.c
|
|
index d5717488..9f605270 100644
|
|
--- a/liblight/lights.c
|
|
+++ b/liblight/lights.c
|
|
@@ -123,7 +123,10 @@ write_int(char const* path, int value)
|
|
if (fd >= 0) {
|
|
char buffer[20];
|
|
size_t bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
|
|
- if(bytes >= sizeof(buffer)) return -EINVAL;
|
|
+ if (bytes >= sizeof(buffer)) {
|
|
+ close(fd);
|
|
+ return -EINVAL;
|
|
+ }
|
|
ssize_t amt = write(fd, buffer, bytes);
|
|
close(fd);
|
|
return amt == -1 ? -errno : 0;
|
|
@@ -146,7 +149,10 @@ write_double_int(char const* path, int value1, int value2)
|
|
if (fd >= 0) {
|
|
char buffer[20];
|
|
size_t bytes = snprintf(buffer, sizeof(buffer), "%d %d\n", value1, value2);
|
|
- if(bytes >= sizeof(buffer)) return -EINVAL;
|
|
+ if (bytes >= sizeof(buffer)) {
|
|
+ close(fd);
|
|
+ return -EINVAL;
|
|
+ }
|
|
ssize_t amt = write(fd, buffer, bytes);
|
|
close(fd);
|
|
return amt == -1 ? -errno : 0;
|