Skip to contentSkip to conversation

Rename read_pipe() with read_fd() and make its buffer nul-terminated.

PATCH In review Carlos Rica 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Rename read_pipe() with read_fd() and make its buffer nul-terminated.

The new name is closer to the purpose of the function.

The other change just makes things easier for callers needing a NUL-terminated buffer.

Since the function returns only the memory written with data, almost always allocating more space than needed because final size is unknown, an extra NUL terminating the buffer is harmless. It is not included in the returned size, so the function remains working as before.

Also, now the function allows the buffer passed to be NULL at first, and alloc_nr is now used for growing the buffer, instead size=*2.

---
 builtin-stripspace.c |    4 +++-
 cache.h              |    2 +-
 mktag.c              |    2 +-
 sha1_file.c          |   17 ++++++++++++++---
 4 files changed, 19 insertions(+), 6 deletions(-)
builtin-stripspace.c +3 -1
index 0c970aa..5571687 100644
@@ -79,8 +79,10 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 
 	size = 1024;
 	buffer = xmalloc(size);
-	if (read_pipe(0, &buffer, &size))
+	if (read_fd(0, &buffer, &size)) {
+		free(buffer);
 		die("could not read the input");
+	}
 
 	size = stripspace(buffer, size, 0);
 	write_or_die(1, buffer, size);
cache.h +1 -1
index 328c1ad..ec9b43d 100644
@@ -265,7 +265,7 @@ extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat
 extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, int);
 extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
 extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
-extern int read_pipe(int fd, char** return_buf, unsigned long* return_size);
+extern int read_fd(int fd, char** return_buf, unsigned long* return_size);
 extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
 extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
mktag.c +1 -1
index b82e377..38acd5a 100644
@@ -120,7 +120,7 @@ int main(int argc, char **argv)
 
 	setup_git_directory();
 
-	if (read_pipe(0, &buffer, &size)) {
+	if (read_fd(0, &buffer, &size)) {
 		free(buffer);
 		die("could not read from stdin");
 	}
sha1_file.c +14 -3
index 1efd9ae..563ec07 100644
@@ -2304,27 +2304,38 @@ int has_sha1_file(const unsigned char *sha1)
  *
  * returns 0 if anything went fine and -1 otherwise
  *
+ * The buffer is always NUL-terminated, not including it in returned size.
+ *
  * NOTE: both buf and size may change, but even when -1 is returned
  * you still have to free() it yourself.
  */
-int read_pipe(int fd, char** return_buf, unsigned long* return_size)
+int read_fd(int fd, char** return_buf, unsigned long* return_size)
 {
 	char* buf = *return_buf;
 	unsigned long size = *return_size;
 	ssize_t iret;
 	unsigned long off = 0;
 
+	if (!buf || size <= 1) {
+		size = alloc_nr(size);
+		buf = xrealloc(buf, size);
+	}
+
 	do {
 		iret = xread(fd, buf + off, size - off);
 		if (iret > 0) {
 			off += iret;
 			if (off == size) {
-				size *= 2;
+				size = alloc_nr(size);
 				buf = xrealloc(buf, size);
 			}
 		}
 	} while (iret > 0);
 
+	if (off == size)
+		buf = xrealloc(buf, size + 1);
+	buf[off] = '\0';
+
 	*return_buf = buf;
 	*return_size = off;
 
@@ -2339,7 +2350,7 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
 	char *buf = xmalloc(size);
 	int ret;
 
-	if (read_pipe(fd, &buf, &size)) {
+	if (read_fd(fd, &buf, &size)) {
 		free(buf);
 		return -1;
 	}
Signature · 1 line
1.5.0
#1 lore raw
Quoted 5 lines · Carlos Rica

The new name is closer to the purpose of the function.

The other change just makes things easier for callers needing a NUL-terminated buffer.

Quoted 8 lines

Since the function returns only the memory written with data, almost always allocating more space than needed because final size is unknown, an extra NUL terminating the buffer is harmless. It is not included in the returned size, so the function remains working as before.

Also, now the function allows the buffer passed to be NULL at first, and alloc_nr is now used for growing the buffer, instead size=*2.

Some people may say function name change should be a separate patch by itself, but I'd let it pass for now...

Quoted 7 lines
sha1_file.c +0 -0
index 1efd9ae..563ec07 100644
@@ -2304,27 +2304,38 @@ int has_sha1_file(const unsigned char *sha1)
  *
  * returns 0 if anything went fine and -1 otherwise

sha1_file.c int has_sha1_file(const unsigned char *sha1) · line 2314

Quoted 10 lines
sha1_file.c +3 -1
  *
+ * The buffer is always NUL-terminated, not including it in returned size.
+ *
  * NOTE: both buf and size may change, but even when -1 is returned
  * you still have to free() it yourself.
  */
-int read_pipe(int fd, char** return_buf, unsigned long* return_size)
+int read_fd(int fd, char** return_buf, unsigned long* return_size)
 {
 	char* buf = *return_buf;

Our code seems to prefer pointer declaration to be spelled as "type **var", not "type** var".

sha1_file.c int has_sha1_file(const unsigned char *sha1) · line 2323

Quoted 9 lines
sha1_file.c +5 -0
 	unsigned long size = *return_size;
 	ssize_t iret;
 	unsigned long off = 0;
 
+	if (!buf || size <= 1) {
+		size = alloc_nr(size);
+		buf = xrealloc(buf, size);
+	}
+

Hmph. The reason this is not "!size" is because you would want more than one. As your plan is to use this mostly for the -F option of "tag/commit", I suspect using a bit larger default, such as 80 (just a line), or probably 1k (most log messages would fit in such a buffer), would be more practical.

Quoted 6 lines
sha1_file.c +0 -1
 	do {
 		iret = xread(fd, buf + off, size - off);
 		if (iret > 0) {
 			off += iret;
 			if (off == size) {
-				size *= 2;

sha1_file.c int has_sha1_file(const unsigned char *sha1) · line 2338

Quoted 10 lines
sha1_file.c +5 -0
+				size = alloc_nr(size);
 				buf = xrealloc(buf, size);
 			}
 		}
 	} while (iret > 0);
 
+	if (off == size)
+		buf = xrealloc(buf, size + 1);
+	buf[off] = '\0';
+

I wonder if doing xread(... (size-1) - off) in the loop would ensure (off <= size-1) here. You also would need to update the realloc condition inside loop if you do so.

#2 lore raw

2007/7/18, Junio C Hamano <[email protected]>:

Quoted 11 lines

sha1_file.c int has_sha1_file(const unsigned char *sha1) · line 2323

Quoted 10 lines
      unsigned long size = *return_size;
      ssize_t iret;
      unsigned long off = 0;
sha1_file.c +5 -0
+     if (!buf || size <= 1) {
+             size = alloc_nr(size);
+             buf = xrealloc(buf, size);
+     }
+
Quoted 5 lines

Hmph. The reason this is not "!size" is because you would want more than one. As your plan is to use this mostly for the -F option of "tag/commit", I suspect using a bit larger default, such as 80 (just a line), or probably 1k (most log messages would fit in such a buffer), would be more practical.

The true reason is because I before tried using (size-1) instead of (size) in xread(..., size - off), and then I forgot to remove that condition. Sorry.

Quoted 13 lines
Quoted 13 lines
      do {
              iret = xread(fd, buf + off, size - off);
              if (iret > 0) {
                      off += iret;
                      if (off == size) {
sha1_file.c +1 -1
-                             size *= 2;
+                             size = alloc_nr(size);
                              buf = xrealloc(buf, size);
                      }
              }
      } while (iret > 0);
Quoted 8 lines

sha1_file.c int has_sha1_file(const unsigned char *sha1) · line 2338

Quoted 4 lines
sha1_file.c +4 -0
+     if (off == size)
+             buf = xrealloc(buf, size + 1);
+     buf[off] = '\0';
+

I wonder if doing xread(... (size-1) - off) in the loop would ensure (off <= size-1) here. You also would need to update the realloc condition inside loop if you do so.

I have chosen that solution to leave the code easy to read, but the (size-1) version will avoid that additional (but non-frequent) realloc.

I will resend the fixed patch along with the builtin-tag.c one.