From: Christian Marangi Date: Mon, 29 May 2023 15:07:54 +0000 (+0200) Subject: netif_utils: correctly close fd on read error X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=project%2Fustp.git netif_utils: correctly close fd on read error Fix Coverity Scan CID 1521069 reporting leaking fd on read error. Refactor the read error handling and correctly close fd on read error. Signed-off-by: Christian Marangi --- diff --git a/netif_utils.c b/netif_utils.c index a842bf7..2bf59c4 100644 --- a/netif_utils.c +++ b/netif_utils.c @@ -143,7 +143,10 @@ static int get_port_file(const char *if_name, const char *file) long res = -1; TSTM((fd = open(path, O_RDONLY)) >= 0, -1, "%m"); int l; - TSTM((l = read(fd, buf, sizeof(buf) - 1)) >= 0, -1, "%m"); + if((l = read(fd, buf, sizeof(buf) - 1)) < 0) { + ERROR("Failed to read file %s: error %m", file); + return -1; + } if(0 == l) { ERROR("Empty %s file", file);