From b1745c3eb4f84c2b68a90cbec6f6a9255ece4645 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sun, 21 Apr 2013 15:55:33 -0700 Subject: Fix fd leak when write() fails in WriteToFile() Reported by parfait 1.1 bug checking tool: File Descriptor Leak: Leaked File Descriptor fd at line 1098 of src/MultiSrc.c in function 'WriteToFile'. fd initialized at line 1096 with creat fd leaks when creat(name, 438) != -1 at line 1096. (Adapted from libXaw commit a30892ed9b6d193f6eb2bab5b37180ac8f63b0b1.) Signed-off-by: Alan Coopersmith --- src/MultiSrc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/MultiSrc.c b/src/MultiSrc.c index 7ad3cdd..a1b2035 100644 --- a/src/MultiSrc.c +++ b/src/MultiSrc.c @@ -966,15 +966,18 @@ static Boolean WriteToFile(String string, String name) { int fd; + Bool result = True; - if ( ((fd = creat(name, 0666)) == -1 ) || - (write(fd, string, sizeof(unsigned char) * strlen(string)) == -1) ) + if ((fd = creat(name, 0666)) == -1) return(FALSE); + if (write(fd, string, sizeof(unsigned char) * strlen(string)) == -1) + result = False; + if ( close(fd) == -1 ) return(FALSE); - return(TRUE); + return(result); } -- cgit v1.2.3