diff mbox series

[v2] pkt-line: do not report packet write errors twice

Message ID d477a069eed1cfa10ebe68991caed6c2b0ebb43b.1618522570.git.matheus.bernardino@usp.br (mailing list archive)
State Accepted
Commit 332ec963bc6fedc0ca9c0b5a66ea842d7fbd6baa
Headers show
Series [v2] pkt-line: do not report packet write errors twice | expand

Commit Message

Matheus Tavares April 15, 2021, 9:57 p.m. UTC
On write() errors, packet_write() dies with the same error message that
is already printed by its callee, packet_write_gently(). This produces
an unnecessarily verbose and repetitive output:

error: packet write failed
fatal: packet write failed: <strerror() message>

In addition to that, packet_write_gently() does not always fulfill its
caller expectation that errno will be properly set before a non-zero
return. In particular, that is not the case for a "data exceeds max
packet size" error. So, in this case, packet_write() will call
die_errno() and print an strerror(errno) message that might be totally
unrelated to the actual error.

Fix both those issues by turning packet_write() and
packet_write_gently() into wrappers to a common lower level function
that doesn't print the error message, but instead returns it on a buffer
for the caller to die() or error() as appropriate.

Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
---
 pkt-line.c | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)

Comments

Junio C Hamano April 15, 2021, 9:59 p.m. UTC | #1
Matheus Tavares <matheus.bernardino@usp.br> writes:

> On write() errors, packet_write() dies with the same error message that
> is already printed by its callee, packet_write_gently(). This produces
> an unnecessarily verbose and repetitive output:
>
> error: packet write failed
> fatal: packet write failed: <strerror() message>
>
> In addition to that, packet_write_gently() does not always fulfill its
> caller expectation that errno will be properly set before a non-zero
> return. In particular, that is not the case for a "data exceeds max
> packet size" error. So, in this case, packet_write() will call
> die_errno() and print an strerror(errno) message that might be totally
> unrelated to the actual error.
>
> Fix both those issues by turning packet_write() and
> packet_write_gently() into wrappers to a common lower level function
> that doesn't print the error message, but instead returns it on a buffer
> for the caller to die() or error() as appropriate.
>
> Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
> ---
>  pkt-line.c | 31 ++++++++++++++++++++++++-------
>  1 file changed, 24 insertions(+), 7 deletions(-)

Nicely done.  Thanks, will queue.

>
> diff --git a/pkt-line.c b/pkt-line.c
> index 0194137528..98304ce374 100644
> --- a/pkt-line.c
> +++ b/pkt-line.c
> @@ -194,13 +194,16 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)
>  	return status;
>  }
>  
> -static int packet_write_gently(const int fd_out, const char *buf, size_t size)
> +static int do_packet_write(const int fd_out, const char *buf, size_t size,
> +			   struct strbuf *err)
>  {
>  	char header[4];
>  	size_t packet_size;
>  
> -	if (size > LARGE_PACKET_DATA_MAX)
> -		return error(_("packet write failed - data exceeds max packet size"));
> +	if (size > LARGE_PACKET_DATA_MAX) {
> +		strbuf_addstr(err, _("packet write failed - data exceeds max packet size"));
> +		return -1;
> +	}
>  
>  	packet_trace(buf, size, 1);
>  	packet_size = size + 4;
> @@ -215,15 +218,29 @@ static int packet_write_gently(const int fd_out, const char *buf, size_t size)
>  	 */
>  
>  	if (write_in_full(fd_out, header, 4) < 0 ||
> -	    write_in_full(fd_out, buf, size) < 0)
> -		return error(_("packet write failed"));
> +	    write_in_full(fd_out, buf, size) < 0) {
> +		strbuf_addf(err, _("packet write failed: %s"), strerror(errno));
> +		return -1;
> +	}
> +	return 0;
> +}
> +
> +static int packet_write_gently(const int fd_out, const char *buf, size_t size)
> +{
> +	struct strbuf err = STRBUF_INIT;
> +	if (do_packet_write(fd_out, buf, size, &err)) {
> +		error("%s", err.buf);
> +		strbuf_release(&err);
> +		return -1;
> +	}
>  	return 0;
>  }
>  
>  void packet_write(int fd_out, const char *buf, size_t size)
>  {
> -	if (packet_write_gently(fd_out, buf, size))
> -		die_errno(_("packet write failed"));
> +	struct strbuf err = STRBUF_INIT;
> +	if (do_packet_write(fd_out, buf, size, &err))
> +		die("%s", err.buf);
>  }
>  
>  void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
diff mbox series

Patch

diff --git a/pkt-line.c b/pkt-line.c
index 0194137528..98304ce374 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -194,13 +194,16 @@  int packet_write_fmt_gently(int fd, const char *fmt, ...)
 	return status;
 }
 
-static int packet_write_gently(const int fd_out, const char *buf, size_t size)
+static int do_packet_write(const int fd_out, const char *buf, size_t size,
+			   struct strbuf *err)
 {
 	char header[4];
 	size_t packet_size;
 
-	if (size > LARGE_PACKET_DATA_MAX)
-		return error(_("packet write failed - data exceeds max packet size"));
+	if (size > LARGE_PACKET_DATA_MAX) {
+		strbuf_addstr(err, _("packet write failed - data exceeds max packet size"));
+		return -1;
+	}
 
 	packet_trace(buf, size, 1);
 	packet_size = size + 4;
@@ -215,15 +218,29 @@  static int packet_write_gently(const int fd_out, const char *buf, size_t size)
 	 */
 
 	if (write_in_full(fd_out, header, 4) < 0 ||
-	    write_in_full(fd_out, buf, size) < 0)
-		return error(_("packet write failed"));
+	    write_in_full(fd_out, buf, size) < 0) {
+		strbuf_addf(err, _("packet write failed: %s"), strerror(errno));
+		return -1;
+	}
+	return 0;
+}
+
+static int packet_write_gently(const int fd_out, const char *buf, size_t size)
+{
+	struct strbuf err = STRBUF_INIT;
+	if (do_packet_write(fd_out, buf, size, &err)) {
+		error("%s", err.buf);
+		strbuf_release(&err);
+		return -1;
+	}
 	return 0;
 }
 
 void packet_write(int fd_out, const char *buf, size_t size)
 {
-	if (packet_write_gently(fd_out, buf, size))
-		die_errno(_("packet write failed"));
+	struct strbuf err = STRBUF_INIT;
+	if (do_packet_write(fd_out, buf, size, &err))
+		die("%s", err.buf);
 }
 
 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)