[PATCH] ebgpart: fix file descriptor handling

10 views
Skip to first unread message

Andreas J. Reichel

unread,
Oct 12, 2017, 7:41:13 AM10/12/17
to efibootg...@googlegroups.com, Andreas Reichel
From: Andreas Reichel <andreas.r...@siemens.com>

* Test for fd < 0 instead of fd == 0
* close fd before return to prevent handle leak

Signed-off-by: Andreas Reichel <andreas.r...@siemens.com>
---
tools/ebgpart.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/ebgpart.c b/tools/ebgpart.c
index f80bdfe..ef99ace 100644
--- a/tools/ebgpart.c
+++ b/tools/ebgpart.c
@@ -255,7 +255,7 @@ static bool check_partition_table(PedDevice *dev)

VERBOSE(stdout, "Checking %s\n", dev->path);
fd = open(dev->path, O_RDONLY);
- if (fd == 0) {
+ if (fd < 0) {
VERBOSE(stderr, "Error opening block device.\n");
return false;
}
@@ -294,6 +294,7 @@ static bool check_partition_table(PedDevice *dev)
struct EFIHeader efihdr;
if (read(fd, &efihdr, sizeof(efihdr)) !=
sizeof(efihdr)) {
+ close(fd);
VERBOSE(stderr, "Error reading EFI Header\n.");
VERBOSE(stderr, "(%s)", strerror(errno));
return false;
--
2.14.2

Jan Kiszka

unread,
Oct 12, 2017, 7:50:10 AM10/12/17
to [ext] Andreas J. Reichel, efibootg...@googlegroups.com
We should be still leaking fd when running into the cpt_out_of_mem
branch, no?

Jan

--
Siemens AG, Corporate Technology, CT RDA ITP SES-DE
Corporate Competence Center Embedded Linux

Andreas J. Reichel

unread,
Oct 12, 2017, 8:05:51 AM10/12/17
to efibootg...@googlegroups.com, Andreas Reichel
From: Andreas Reichel <andreas.r...@siemens.com>

* Test for fd < 0 instead of fd == 0
* close fd before return to prevent handle leak

Signed-off-by: Andreas Reichel <andreas.r...@siemens.com>
---
tools/ebgpart.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/ebgpart.c b/tools/ebgpart.c
index f80bdfe..ed8a0bb 100644
--- a/tools/ebgpart.c
+++ b/tools/ebgpart.c
@@ -255,7 +255,7 @@ static bool check_partition_table(PedDevice *dev)

VERBOSE(stdout, "Checking %s\n", dev->path);
fd = open(dev->path, O_RDONLY);
- if (fd == 0) {
+ if (fd < 0) {
VERBOSE(stderr, "Error opening block device.\n");
return false;
}
@@ -294,6 +294,7 @@ static bool check_partition_table(PedDevice *dev)
struct EFIHeader efihdr;
if (read(fd, &efihdr, sizeof(efihdr)) !=
sizeof(efihdr)) {
+ close(fd);
VERBOSE(stderr, "Error reading EFI Header\n.");
VERBOSE(stderr, "(%s)", strerror(errno));
return false;
@@ -340,6 +341,7 @@ static bool check_partition_table(PedDevice *dev)
}
continue;
cpt_out_of_mem:
+ close(fd);
if (pfst) free(pfst);
if (tmp) free(tmp);
return false;
--
2.14.2

Jan Kiszka

unread,
Oct 13, 2017, 1:35:43 AM10/13/17
to [ext] Andreas J. Reichel, efibootg...@googlegroups.com
Thanks, applied to next.
Reply all
Reply to author
Forward
0 new messages