git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 6/7] sign_buffer: use pipe_command

From
Jeff King <peff@peff.net>
Date
Jun 16, 2016, 09:39 UTC
Message-ID
<20160616093931.GF15851@sigill.intra.peff.net>
In-Reply-To
<20160616093248.GA15130@sigill.intra.peff.net>

Similar to the prior commit for verify_signed_buffer, the motivation here is both to make the code simpler, and to avoid any possible deadlocks with gpg.

In this case we have the same "write to stdin, then read from stdout" that the verify case had. This is unlikely to be a problem in practice, since stdout has the detached signature, which it cannot compute until it has read all of stdin (if it were a non-detached signature, that would be a problem, though).

We don't read from stderr at all currently. However, we will want to in a future patch, so this also prepares us there (and in that case gpg _does_ write before reading all of the input, though again, it is unlikely that a key uid will fill up a pipe buffer).

Signed-off-by: Jeff King <peff@peff.net>
---
 gpg-interface.c | 24 +++++-------------------
 1 file changed, 5 insertions(+), 19 deletions(-)
diff --git a/gpg-interface.c b/gpg-interface.c
index c98035d..74f08a2 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -151,40 +151,26 @@ const char *get_signing_key(void)
 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
 {
 	struct child_process gpg = CHILD_PROCESS_INIT;
-	ssize_t len;
+	int ret;
 	size_t i, j, bottom;
 
-	gpg.in = -1;
-	gpg.out = -1;
 	argv_array_pushl(&gpg.args,
 			 gpg_program,
 			 "-bsau", signing_key,
 			 NULL);
 
-	if (start_command(&gpg))
-		return error(_("could not run gpg."));
+	bottom = signature->len;
 
 	/*
 	 * When the username signingkey is bad, program could be terminated
 	 * because gpg exits without reading and then write gets SIGPIPE.
 	 */
 	sigchain_push(SIGPIPE, SIG_IGN);
-
-	if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
-		close(gpg.in);
-		close(gpg.out);
-		finish_command(&gpg);
-		return error(_("gpg did not accept the data"));
-	}
-	close(gpg.in);
-
-	bottom = signature->len;
-	len = strbuf_read(signature, gpg.out, 1024);
-	close(gpg.out);
-
+	ret = pipe_command(&gpg, buffer->buf, buffer->len,
+			   signature, 1024, NULL, 0);
 	sigchain_pop(SIGPIPE);
 
-	if (finish_command(&gpg) || !len || len < 0)
+	if (ret || signature->len == bottom)
 		return error(_("gpg failed to sign the data"));
 
 	/* Strip CR from the line endings, in case we are on Windows. */
-- 
2.9.0.160.g4984cba
Previous: Jeff KingNext: Jeff King
Message 10 of 22 in “gpg-interface cleanups”
  1. 0/7 gpg-interface cleanupsJeff King, Jun 16, 2016
  2. 1/7 gpg-interface: use child_process.argsJeff King, Jun 16, 2016
  3. 2/7 verify_signed_buffer: drop pbuf variableJeff King, Jun 16, 2016
  4. 3/7 verify_signed_buffer: use tempfile objectJeff King, Jun 16, 2016
  5. 4/7 run-command: add pipe_command helperJeff King, Jun 16, 2016
  6. Eric SunshineJun 17, 2016
  7. Jeff KingJun 17, 2016
  8. Eric SunshineJun 17, 2016
  9. 5/7 verify_signed_buffer: use pipe_commandJeff King, Jun 16, 2016
  10. 6/7 sign_buffer: use pipe_commandJeff King, Jun 16, 2016
  11. 7/7 gpg-interface: check gpg signature creation statusJeff King, Jun 16, 2016
  12. Junio C HamanoJun 16, 2016
  13. Michael J GruberJun 17, 2016
  14. Jeff KingJun 17, 2016
  15. 0/7 gpg-interface cleanupsJeff King, Jun 17, 2016
  16. 1/7 gpg-interface: use child_process.argsJeff King, Jun 17, 2016
  17. 2/7 verify_signed_buffer: drop pbuf variableJeff King, Jun 17, 2016
  18. 3/7 verify_signed_buffer: use tempfile objectJeff King, Jun 17, 2016
  19. 4/7 run-command: add pipe_command helperJeff King, Jun 17, 2016
  20. 5/7 verify_signed_buffer: use pipe_commandJeff King, Jun 17, 2016
  21. 6/7 sign_buffer: use pipe_commandJeff King, Jun 17, 2016
  22. 7/7 gpg-interface: check gpg signature creation statusJeff King, Jun 17, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.