Skip to content

Commit 4322353

Browse files
peffgitster
authored andcommitted
verify_signed_buffer: use tempfile object
We use git_mkstemp to create a temporary file, and try to clean it up in all exit paths from the function. But that misses any cases where we die by signal, or by calling die() in a sub-function. In addition, we missed one of the exit paths. Let's convert to using a tempfile object, which handles the hard cases for us, and add the missing cleanup call. Note that we would not simply want to rely on program exit to catch our missed cleanup, as this function may be called many times in a single program (for the same reason, we use a static tempfile instead of heap-allocating a new one; that gives an upper bound on our memory usage). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c752fcc commit 4322353

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

gpg-interface.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "strbuf.h"
44
#include "gpg-interface.h"
55
#include "sigchain.h"
6+
#include "tempfile.h"
67

78
static char *configured_signing_key;
89
static const char *gpg_program = "gpg";
@@ -208,28 +209,32 @@ int verify_signed_buffer(const char *payload, size_t payload_size,
208209
struct strbuf *gpg_output, struct strbuf *gpg_status)
209210
{
210211
struct child_process gpg = CHILD_PROCESS_INIT;
211-
char path[PATH_MAX];
212+
static struct tempfile temp;
212213
int fd, ret;
213214
struct strbuf buf = STRBUF_INIT;
214215

215-
fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
216+
fd = mks_tempfile_t(&temp, ".git_vtag_tmpXXXXXX");
216217
if (fd < 0)
217-
return error_errno(_("could not create temporary file '%s'"), path);
218-
if (write_in_full(fd, signature, signature_size) < 0)
219-
return error_errno(_("failed writing detached signature to '%s'"), path);
218+
return error_errno(_("could not create temporary file"));
219+
if (write_in_full(fd, signature, signature_size) < 0) {
220+
error_errno(_("failed writing detached signature to '%s'"),
221+
temp.filename.buf);
222+
delete_tempfile(&temp);
223+
return -1;
224+
}
220225
close(fd);
221226

222227
argv_array_pushl(&gpg.args,
223228
gpg_program,
224229
"--status-fd=1",
225-
"--verify", path, "-",
230+
"--verify", temp.filename.buf, "-",
226231
NULL);
227232
gpg.in = -1;
228233
gpg.out = -1;
229234
if (gpg_output)
230235
gpg.err = -1;
231236
if (start_command(&gpg)) {
232-
unlink(path);
237+
delete_tempfile(&temp);
233238
return error(_("could not run gpg."));
234239
}
235240

@@ -249,7 +254,7 @@ int verify_signed_buffer(const char *payload, size_t payload_size,
249254
ret = finish_command(&gpg);
250255
sigchain_pop(SIGPIPE);
251256

252-
unlink_or_warn(path);
257+
delete_tempfile(&temp);
253258

254259
ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
255260
strbuf_release(&buf); /* no matter it was used or not */

0 commit comments

Comments
 (0)