Web lists-archives.com

Re: [PATCH v3 1/2] use HOST_NAME_MAX to size buffers for gethostname(2)




Am 20.04.2017 um 20:37 schrieb Torsten Bögershausen:
On 2017-04-19 22:02, René Scharfe wrote:
Am 19.04.2017 um 21:09 schrieb Torsten Bögershausen:
On 2017-04-19 19:28, René Scharfe wrote:
[]
One or two minor comments inline
diff --git a/builtin/gc.c b/builtin/gc.c
index 2daede7820..4c1c01e87d 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -228,21 +228,99 @@ static int need_to_gc(void)
       return 1;
   }
   +struct pidfile {
+    struct strbuf buf;
+    char *hostname;
+};
+
+#define PIDFILE_INIT { STRBUF_INIT }
+
+static void pidfile_release(struct pidfile *pf)
+{
+    pf->hostname = NULL;
+    strbuf_release(&pf->buf);
+}
+
+static int pidfile_read(struct pidfile *pf, const char *path,
+            unsigned int max_age_seconds)
+{
+    int fd;
+    struct stat st;
+    ssize_t len;
+    char *space;
+    int rc = -1;
+
+    fd = open(path, O_RDONLY);
+    if (fd < 0)
+        return rc;
+
+    if (fstat(fd, &st))
+        goto out;
+    if (time(NULL) - st.st_mtime > max_age_seconds)
+        goto out;
+    if (st.st_size > (size_t)st.st_size)

Minor: we need xsize_t here ?
if (st.st_size > xsize_t(st.st_size))

No, xsize_t() would do the same check and die on overflow, and pidfile_read() is
supposed to handle big pids gracefully.
This about the file size, isn't it ?
And here xsize_t should be save to use and good practise.

I think I meant to write "big pidfiles" there.

With xsize_t() gc would die when seeing a pidfile whose size doesn't fit into size_t. The version I sent just ignores such files. However, it would choke on slightly smaller files that happen to not fit into memory. And no reasonable pidfile can be big enough to trigger any of that, so dying on conversion error wouldn't really be a problem. Is that what you meant? It makes sense, in any case.

Thanks,
René