<feed xmlns='http://www.w3.org/2005/Atom'>
<title>git/gpg-interface.c, branch v2.18.2</title>
<subtitle>Mirror of https://git.kernel.org/pub/scm/git/git.git/
</subtitle>
<id>https://git.shady.money/git/atom?h=v2.18.2</id>
<link rel='self' href='https://git.shady.money/git/atom?h=v2.18.2'/>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/'/>
<updated>2018-04-16T05:15:03Z</updated>
<entry>
<title>gpg-interface: find the last gpg signature line</title>
<updated>2018-04-16T05:15:03Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2018-04-13T21:18:35Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=8b44b2be89bf59c0fada6095bdfea66ff53c6074'/>
<id>urn:sha1:8b44b2be89bf59c0fada6095bdfea66ff53c6074</id>
<content type='text'>
A signed tag has a detached signature like this:

  object ...
  [...more header...]

  This is the tag body.

  -----BEGIN PGP SIGNATURE-----
  [opaque gpg data]
  -----END PGP SIGNATURE-----

Our parser finds the _first_ line that appears to start a
PGP signature block, meaning we may be confused by a
signature (or a signature-like line) in the actual body.
Let's keep parsing and always find the final block, which
should be the detached signature over all of the preceding
content.

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Ben Toews &lt;mastahyeti@gmail.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>gpg-interface: extract gpg line matching helper</title>
<updated>2018-04-16T05:15:03Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2018-04-13T21:18:34Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=f68f2dd57f55e0b1782b20b615dd7a96d7fb6a41'/>
<id>urn:sha1:f68f2dd57f55e0b1782b20b615dd7a96d7fb6a41</id>
<content type='text'>
Let's separate the actual line-by-line parsing of signatures
from the notion of "is this a gpg signature line". That will
make it easier to do more refactoring of this loop in future
patches.

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Ben Toews &lt;mastahyeti@gmail.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>gpg-interface: fix const-correctness of "eol" pointer</title>
<updated>2018-04-16T05:15:03Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2018-04-13T21:18:33Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=17ef3a421e0a1019592a0b14b95f09df61730071'/>
<id>urn:sha1:17ef3a421e0a1019592a0b14b95f09df61730071</id>
<content type='text'>
We accidentally shed the "const" of our buffer by passing it
through memchr. Let's fix that, and while we're at it, move
our variable declaration inside the loop, which is the only
place that uses it.

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Ben Toews &lt;mastahyeti@gmail.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>gpg-interface: use size_t for signature buffer size</title>
<updated>2018-04-16T05:15:03Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2018-04-13T21:18:32Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=e6fa6cde5bec648f1b8fd7e3f9e5939c5093985a'/>
<id>urn:sha1:e6fa6cde5bec648f1b8fd7e3f9e5939c5093985a</id>
<content type='text'>
Even though our object sizes (from which these buffers would
come) are typically "unsigned long", this is something we'd
like to eventually fix (since it's only 32-bits even on
64-bit Windows). It makes more sense to use size_t when
taking an in-memory buffer.

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Ben Toews &lt;mastahyeti@gmail.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>gpg-interface: modernize function declarations</title>
<updated>2018-04-16T05:15:03Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2018-04-13T21:18:31Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=f80bee27e3c3fc9085427945f97a2f7756500ea9'/>
<id>urn:sha1:f80bee27e3c3fc9085427945f97a2f7756500ea9</id>
<content type='text'>
Let's drop "extern" from our declarations, which brings us
in line with our modern style guidelines. While we're
here, let's wrap some of the overly long lines, and move
docstrings for public functions to their declarations, since
they document the interface.

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Ben Toews &lt;mastahyeti@gmail.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>gpg-interface: handle bool user.signingkey</title>
<updated>2018-04-16T05:15:02Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2018-04-13T21:18:30Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=1b0eeec3f359888f8a638de8af253f621a5b836e'/>
<id>urn:sha1:1b0eeec3f359888f8a638de8af253f621a5b836e</id>
<content type='text'>
The config handler for user.signingkey does not check for a
boolean value, and thus:

  git -c user.signingkey tag

will segfault. We could fix this and even shorten the code
by using git_config_string(). But our set_signing_key()
helper is used by other code outside of gpg-interface.c, so
we must keep it (and we may as well use it, because unlike
git_config_string() it does not leak when we overwrite an
old value).

Ironically, the handler for gpg.program just below _could_
use git_config_string() but doesn't. But since we're going
to touch that in a future patch, we'll leave it alone for
now. We will add some whitespace and returns in preparation
for adding more config keys, though.

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Ben Toews &lt;mastahyeti@gmail.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>tempfile: auto-allocate tempfiles on heap</title>
<updated>2017-09-06T08:19:54Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2017-09-05T12:15:08Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=076aa2cbda5782426c45cd65017b81d77876297a'/>
<id>urn:sha1:076aa2cbda5782426c45cd65017b81d77876297a</id>
<content type='text'>
The previous commit taught the tempfile code to give up
ownership over tempfiles that have been renamed or deleted.
That makes it possible to use a stack variable like this:

  struct tempfile t;

  create_tempfile(&amp;t, ...);
  ...
  if (!err)
          rename_tempfile(&amp;t, ...);
  else
          delete_tempfile(&amp;t);

But doing it this way has a high potential for creating
memory errors. The tempfile we pass to create_tempfile()
ends up on a global linked list, and it's not safe for it to
go out of scope until we've called one of those two
deactivation functions.

Imagine that we add an early return from the function that
forgets to call delete_tempfile(). With a static or heap
tempfile variable, the worst case is that the tempfile hangs
around until the program exits (and some functions like
setup_shallow_temporary rely on this intentionally, creating
a tempfile and then leaving it for later cleanup).

But with a stack variable as above, this is a serious memory
error: the variable goes out of scope and may be filled with
garbage by the time the tempfile code looks at it.  Let's
see if we can make it harder to get this wrong.

Since many callers need to allocate arbitrary numbers of
tempfiles, we can't rely on static storage as a general
solution. So we need to turn to the heap. We could just ask
all callers to pass us a heap variable, but that puts the
burden on them to call free() at the right time.

Instead, let's have the tempfile code handle the heap
allocation _and_ the deallocation (when the tempfile is
deactivated and removed from the list).

This changes the return value of all of the creation
functions. For the cleanup functions (delete and rename),
we'll add one extra bit of safety: instead of taking a
tempfile pointer, we'll take a pointer-to-pointer and set it
to NULL after freeing the object. This makes it safe to
double-call functions like delete_tempfile(), as the second
call treats the NULL input as a noop. Several callsites
follow this pattern.

The resulting patch does have a fair bit of noise, as each
caller needs to be converted to handle:

  1. Storing a pointer instead of the struct itself.

  2. Passing the pointer instead of taking the struct
     address.

  3. Handling a "struct tempfile *" return instead of a file
     descriptor.

We could play games to make this less noisy. For example, by
defining the tempfile like this:

  struct tempfile {
	struct heap_allocated_part_of_tempfile {
                int fd;
                ...etc
        } *actual_data;
  }

Callers would continue to have a "struct tempfile", and it
would be "active" only when the inner pointer was non-NULL.
But that just makes things more awkward in the long run.
There aren't that many callers, so we can simply bite
the bullet and adjust all of them. And the compiler makes it
easy for us to find them all.

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>tempfile: do not delete tempfile on failed close</title>
<updated>2017-09-06T08:19:53Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2017-09-05T12:14:30Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=49bd0fc2220eef17d8f5fd3ee76e391d03df8a6d'/>
<id>urn:sha1:49bd0fc2220eef17d8f5fd3ee76e391d03df8a6d</id>
<content type='text'>
When close_tempfile() fails, we delete the tempfile and
reset the fields of the tempfile struct. This makes it
easier for callers to return without cleaning up, but it
also makes this common pattern:

  if (close_tempfile(tempfile))
	return error_errno("error closing %s", tempfile-&gt;filename.buf);

wrong, because the "filename" field has been reset after the
failed close. And it's not easy to fix, as in many cases we
don't have another copy of the filename (e.g., if it was
created via one of the mks_tempfile functions, and we just
have the original template string).

Let's drop the feature that a failed close automatically
deletes the file. This puts the burden on the caller to do
the deletion themselves, but this isn't that big a deal.
Callers which do:

  if (write(...) || close_tempfile(...)) {
	delete_tempfile(...);
	return -1;
  }

already had to call delete when the write() failed, and so
aren't affected. Likewise, any caller which just calls die()
in the error path is OK; we'll delete the tempfile during
the atexit handler.

Because this patch changes the semantics of close_tempfile()
without changing its signature, all callers need to be
manually checked and converted to the new scheme. This patch
covers all in-tree callers, but there may be others for
not-yet-merged topics. To catch these, we rename the
function to close_tempfile_gently(), which will attract
compile-time attention to new callers. (Technically the
original could be considered "gentle" already in that it
didn't die() on errors, but this one is even more so).

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>always check return value of close_tempfile</title>
<updated>2017-09-06T08:19:53Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2017-09-05T12:14:26Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=45c6b1ed24724f7f3041a60a4313df7d9c4b9909'/>
<id>urn:sha1:45c6b1ed24724f7f3041a60a4313df7d9c4b9909</id>
<content type='text'>
If close_tempfile() encounters an error, then it deletes the
tempfile and resets the "struct tempfile". But many code
paths ignore the return value and continue to use the
tempfile. Instead, we should generally treat this the same
as a write() error.

Note that in the postimage of some of these cases our error
message will be bogus after a failed close because we look
at tempfile-&gt;filename (either directly or via get_tempfile_path).
But after the failed close resets the tempfile object, this
is guaranteed to be the empty string. That will be addressed
in a future patch (because there are many more cases of the
same problem than just these instances).

Note also in the hunk in gpg-interface.c that it's fine to
call delete_tempfile() in the error path, even if
close_tempfile() failed and already deleted the file. The
tempfile code is smart enough to know the second deletion is
a noop.

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>verify_signed_buffer: prefer close_tempfile() to close()</title>
<updated>2017-09-06T08:19:52Z</updated>
<author>
<name>Jeff King</name>
<email>peff@peff.net</email>
</author>
<published>2017-09-05T12:14:23Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=d88ef6605120fd75be38376ba147623cf427bf73'/>
<id>urn:sha1:d88ef6605120fd75be38376ba147623cf427bf73</id>
<content type='text'>
We do a manual close() on the descriptor provided to us by
mks_tempfile. But this runs contrary to the advice in
tempfile.h, which notes that you should always use
close_tempfile(). Otherwise the descriptor may be reused
without the tempfile object knowing it, and the later call
to delete_tempfile() could close a random descriptor.

Signed-off-by: Jeff King &lt;peff@peff.net&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
</feed>
