]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hash-object: fix descriptor leak with --literally
authorJeff King <peff@peff.net>
Thu, 19 Jan 2023 01:57:56 +0000 (20:57 -0500)
committerJunio C Hamano <gitster@pobox.com>
Thu, 19 Jan 2023 16:24:21 +0000 (08:24 -0800)
In hash_object(), we open a descriptor for each file to hash (whether we
got the filename from the command line or --stdin-paths), but never
close it. For the traditional code path, which feeds the result to
index_fd(), this is OK; it closes the descriptor for us.

But 5ba9a93b39 (hash-object: add --literally option, 2014-09-11) added a
second code path, which does not close the descriptor. There we need to
do so ourselves.

You can see the problem in a clone of git.git like this:

  $ git ls-files -s | grep ^100644 | cut -f2 |
    git hash-object --stdin-paths --literally >/dev/null
  fatal: could not open 'builtin/var.c' for reading: Too many open files

After this patch, it completes successfully. I didn't bother with a
test, as it's a pain to deal with descriptor limits portably, and the
fix is so trivial.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/hash-object.c

index fbae878c2b951599fcfb82021185c67cc942b5ca..3d6bec60e184d956b1570ed5de8446e0123f43f8 100644 (file)
@@ -27,6 +27,7 @@ static int hash_literally(struct object_id *oid, int fd, const char *type, unsig
        else
                ret = write_object_file_literally(buf.buf, buf.len, type, oid,
                                                 flags);
+       close(fd);
        strbuf_release(&buf);
        return ret;
 }