Skip to contentSkip to conversation

bugreport.c: replace strbuf_write_fd with write_in_full

PATCH In review [email protected] 2 participants 4 messages Jun 19, 2020 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

v2 cover Replace strbuf_write_fd with write_in_full

From: "Randall S. Becker" <[email protected]>

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(-)
Signature · 1 line
2.21.0
#2 lore raw

PATCH v2 1/2 bugreport.c: replace strbuf_write_fd with write_in_full

From: "Randall S. Becker" <[email protected]>

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.

---
 bugreport.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
bugreport.c +3 -1
index aa8a489c35..1aefa4c6bc 100644
@@ -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);
 
 	/*
Signature · 1 line
2.21.0
#3 lore raw

PATCH v2 2/2 strbuf: remove unreferenced strbuf_write_fd method.

In reply to [email protected]

From: "Randall S. Becker" <[email protected]>

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.

---
 strbuf.c | 5 -----
 strbuf.h | 1 -
 2 files changed, 6 deletions(-)
strbuf.c +0 -5
index 2f1a7d3209..e3397cc4c7 100644
@@ -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)
strbuf.h +0 -1
index 7062eb6410..223ee2094a 100644
@@ -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
Signature · 1 line
2.21.0
#4 lore raw

Re: [Patch v2 0/2] Replace strbuf_write_fd with write_in_full

In reply to [email protected]
Quoted 7 lines · [email protected] writes:

From: "Randall S. Becker" <[email protected]>

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.