# [Patch v2 1/2] bugreport.c: replace strbuf_write_fd with write_in_full

4 messages from 2020-06-19 to 2020-06-19. Participants: randall.s.becker@rogers.com, Junio C Hamano.
Thread: https://gitlist.dev/t/53721

## randall.s.becker@rogers.com, 2020-06-19 20:23

Subject: [Patch v2 1/2] bugreport.c: replace strbuf_write_fd with write_in_full
Message-ID: <20200619202320.4619-2-randall.s.becker@rogers.com>
URL: https://gitlist.dev/e/20200619202320.4619-2-randall.s.becker%40rogers.com
In-Reply-To: <20200619202320.4619-1-randall.s.becker@rogers.com>

```
From: "Randall S. Becker" <randall.becker@nexbridge.ca>

The strbuf_write_fd method did not provide checks for buffers larger
than MAX_IO_SIZE. Replacing with write_in_full ensures the entire
buffer will always be written to disk or report an error and die.

Signed-off-by: Randall S. Becker <rsbecker@nexbridge.com>
---
 bugreport.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/bugreport.c b/bugreport.c
index aa8a489c35..1aefa4c6bc 100644
--- a/bugreport.c
+++ b/bugreport.c
@@ -174,7 +174,9 @@ int cmd_main(int argc, const char **argv)
 		die(_("couldn't create a new file at '%s'"), report_path.buf);
 	}
 
-	strbuf_write_fd(&buffer, report);
+	if (write_in_full(report, buffer.buf, buffer.len) < 0)
+		die_errno(_("unable to write to %s"), report_path.buf);
+
 	close(report);
 
 	/*
-- 
2.21.0


```

## randall.s.becker@rogers.com, 2020-06-19 20:23

Subject: [Patch v2 0/2] Replace strbuf_write_fd with write_in_full
Message-ID: <20200619202320.4619-1-randall.s.becker@rogers.com>
URL: https://gitlist.dev/e/20200619202320.4619-1-randall.s.becker%40rogers.com
In-Reply-To: <20200619202320.4619-1-randall.s.becker.ref@rogers.com>

```
From: "Randall S. Becker" <randall.becker@nexbridge.ca>

The strbuf_write_fd method does not check whether the buffer exceeds
MAX_IO_SIZE on the target platform. This fix replaces the use of that
method with write_in_full, which does. Since this is the only use of
strbuf_write_fd, and since the method was unsafe, it has been removed
from strbuf.c and strbuf.h.

Randall S. Becker (2):
  bugreport.c: replace strbuf_write_fd with write_in_full
  strbuf: remove unreferenced strbuf_write_fd method.

 bugreport.c | 4 +++-
 strbuf.c    | 5 -----
 strbuf.h    | 1 -
 3 files changed, 3 insertions(+), 7 deletions(-)

-- 
2.21.0


```

## randall.s.becker@rogers.com, 2020-06-19 20:23

Subject: [Patch v2 2/2] strbuf: remove unreferenced strbuf_write_fd method.
Message-ID: <20200619202320.4619-3-randall.s.becker@rogers.com>
URL: https://gitlist.dev/e/20200619202320.4619-3-randall.s.becker%40rogers.com
In-Reply-To: <20200619202320.4619-1-randall.s.becker@rogers.com>

```
From: "Randall S. Becker" <randall.becker@nexbridge.ca>

strbuf_write_fd was only used in bugreport.c. Since that file now uses
write_in_full, this method is no longer needed. In addition, strbuf_write_fd
did not guard against exceeding MAX_IO_SIZE for the platform, nor
provided error handling in the event of a failure if only partial data
was written to the file descriptor. Since already write_in_full has this 
capability and is in general use, it should be used instead. The change
impacts strbuf.c and strbuf.h.

Signed-off-by: Randall S. Becker <rsbecker@nexbridge.com>
---
 strbuf.c | 5 -----
 strbuf.h | 1 -
 2 files changed, 6 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index 2f1a7d3209..e3397cc4c7 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -556,11 +556,6 @@ ssize_t strbuf_write(struct strbuf *sb, FILE *f)
 	return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
 }
 
-ssize_t strbuf_write_fd(struct strbuf *sb, int fd)
-{
-	return sb->len ? write(fd, sb->buf, sb->len) : 0;
-}
-
 #define STRBUF_MAXLINK (2*PATH_MAX)
 
 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
diff --git a/strbuf.h b/strbuf.h
index 7062eb6410..223ee2094a 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -473,7 +473,6 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
  * NUL bytes.
  */
 ssize_t strbuf_write(struct strbuf *sb, FILE *stream);
-ssize_t strbuf_write_fd(struct strbuf *sb, int fd);
 
 /**
  * Read a line from a FILE *, overwriting the existing contents of
-- 
2.21.0


```

## Junio C Hamano, 2020-06-19 20:51

Subject: Re: [Patch v2 0/2] Replace strbuf_write_fd with write_in_full
Message-ID: <xmqqo8peg4o4.fsf@gitster.c.googlers.com>
URL: https://gitlist.dev/e/xmqqo8peg4o4.fsf%40gitster.c.googlers.com
In-Reply-To: <20200619202320.4619-1-randall.s.becker@rogers.com>

```
randall.s.becker@rogers.com writes:

> From: "Randall S. Becker" <randall.becker@nexbridge.ca>
>
> The strbuf_write_fd method does not check whether the buffer exceeds
> MAX_IO_SIZE on the target platform. This fix replaces the use of that
> method with write_in_full, which does. Since this is the only use of
> strbuf_write_fd, and since the method was unsafe, it has been removed
> from strbuf.c and strbuf.h.

Excellent.  Thanks.

Will queue.

```
