config: fix bogus fd check when setting up default config
commitaabbd3f3c9dd17c414100d0e029f8a3d13673ab1
authorJeff King <peff@peff.net>
Fri, 8 Jul 2016 09:06:50 +0000 (8 05:06 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 8 Jul 2016 16:47:28 +0000 (8 09:47 -0700)
tree2e803c866f90395deac63bf644d47ecb80c0adb0
parent05219a1276341e72d8082d76b7f5ed394b7437a4
config: fix bogus fd check when setting up default config

Since 9830534 (config --global --edit: create a template
file if needed, 2014-07-25), an edit of the global config
file will try to open() it with O_EXCL, and wants to handle
three cases:

  1. We succeeded; the user has no config file, and we
     should fill in the default template.

  2. We got EEXIST; they have a file already, proceed as usual.

  3. We got another error; we should complain.

However, the check for case 1 does "if (fd)", which will
generally _always_ be true (except for the oddball case that
somehow our stdin got closed and opening really did give us
a new descriptor 0).

So in the EEXIST case, we tried to write the default config
anyway! Fortunately, this turns out to be a noop, since we
just end up writing to and closing "-1", which does nothing.

But in case 3, we would fail to notice any other errors, and
just silently continue (given that we don't actually notice
write errors for the template either, it's probably not that
big a deal; we're about to spawn the editor, so it would
notice any problems. But the code is clearly _trying_ to hit
cover this case and failing).

We can fix it easily by using "fd >= 0" for case 1.

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