git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[cgit PATCH] Close file descriptor on error in readfile()

From
Rys Sommefeldt <rys@pixeltards.com>
Date
Nov 7, 2009, 02:01 UTC
Message-ID
<4AF4D4EC.1040806@pixeltards.com>
Hi Lars,

My colleagues and I use cgit at work, and we've found that the scanning process can consume all available fds pretty quickly on our cgit hosts, because it doesn't close them properly on error. We have a few thousand active repositories for cgit to scan, and we noticed it dying after a certain amount.

I've attached a patch which should apply against current master, although I developed it a while back on an older 0.8 version (sorry it took so long to subscribe and send the patch in).

Cheers,

Rys Sommefeldt ---

 From 6446cf839d2104cd40848e439bf97cd7fd6ccfee Mon Sep 17 00:00:00 2001
From: Rys Sommefeldt <rsommefeldt@plus.net>
Date: Fri, 6 Nov 2009 17:14:56 +0000
Subject: [PATCH] Close fd when done
---
 shared.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/shared.c b/shared.c
index d7b2d5a..d5e54e6 100644
--- a/shared.c
+++ b/shared.c
@@ -404,14 +404,19 @@ int readfile(const char *path, char **buf, size_t 
*size)
     struct stat st;
 
     fd = open(path, O_RDONLY);
-    if (fd == -1)
+    if (fd == -1) {
+        close(fd);
         return errno;
-    if (fstat(fd, &st))
+    }
+    if (fstat(fd, &st)) {
+        close(fd);
         return errno;
+    }
     if (!S_ISREG(st.st_mode))
         return EISDIR;
     *buf = xmalloc(st.st_size + 1);
     *size = read_in_full(fd, *buf, st.st_size);
     (*buf)[*size] = '\0';
+    close(fd);
     return (*size == st.st_size ? 0 : errno);
 }
-- 
1.6.5.2
Next: Steven Noonan
Message 1 of 7 in “Close file descriptor on error in readfile()”
  1. Close file descriptor on error in readfile()Rys Sommefeldt, Nov 7, 2009
  2. Steven NoonanNov 7, 2009
  3. Rys SommefeldtNov 7, 2009
  4. Rys SommefeldtNov 7, 2009
  5. Lars HjemliNov 7, 2009
  6. Andreas SchwabNov 7, 2009
  7. Lars HjemliNov 7, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.