Re: [PATCH 3/6] packfile: inline `open_packed_git_1()`
On Tue, Nov 24, 2026 at 02:04:02PM -0800, Junio C Hamano wrote:
Show 31 quoted lines
> Patrick Steinhardt <ps@pks.im> writes:
> > @@ -510,55 +509,81 @@ static int open_packed_git_1(struct packed_git *p)
> > ; /* nothing */
> >
> > p->pack_fd = git_open(p->pack_name);
> > - if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
> > - return -1;
> > + if (p->pack_fd < 0 || fstat(p->pack_fd, &st)) {
> > + ret = -1;
> > + goto out;
> > + }
> > pack_open_fds++;
>
> There may be a mismatch in pack_open_fds accounting. If git_open()
> succeeds (so p->pack_fd >= 0) and fstat() fails, we jump to 'out:'
> before incrementing pack_open_fds.
>
> > +out:
> > + if (ret < 0)
> > + packfile_close_fd(p);
> > + return ret;
> > }
>
> close_pack_fd() (now called packfile_close_fd()) closes p->pack_fd
> whenever p->pack_fd >= 0 and decrements pack_open_fds, which may
> underflow to UINT_MAX if the ftat() failed because of this?
>
> May not be a new bug (we used to leave the p->pack_fd open without
> nobody closing instead, so we may not have underflown but leaked file
> descriptor instead), but since you are touching around the area, we may
> want to fix it as well.Ah, true indeed, good catch. I'll add a patch on top to fix this.
Patrick