]> git.99rst.org Git - git.git/commit
loose: avoid closing invalid fd on error path
authorJohannes Schindelin <redacted>
Sun, 5 Jul 2026 08:24:19 +0000 (08:24 +0000)
committerJunio C Hamano <redacted>
Sun, 5 Jul 2026 16:12:09 +0000 (09:12 -0700)
commitbd58327406c6868b92fb1b61d85b7430839042d4
tree80b0d8a69f5909bcefedd41b3d5ac556792e395f
parent8b90835161cff95e80bc39e8acb25f0f77592ecf
loose: avoid closing invalid fd on error path

`write_one_object()` opens a file at line 186 and jumps to the errout
label on failure. The errout cleanup unconditionally calls `close(fd)`,
but when `open()` itself failed, fd is -1. Calling `close(-1)` is
harmless on most platforms (returns EBADF) but is undefined behavior per
POSIX and can confuse fd tracking in sanitizer builds.

Guard the close with fd >= 0.

Pointed out by Coverity.

Assisted-by: Claude Opus 4.6
Signed-off-by: Johannes Schindelin <redacted>
Signed-off-by: Junio C Hamano <redacted>
loose.c
git clone https://git.99rst.org/PROJECT