{"thread":{"id":"9095","subject":"[PATCH] Rename read_pipe() with read_fd() and make its buffer nul-terminated.","startedAt":"2007-07-18T18:31:03Z","lastAt":"2007-07-18T18:31:03Z","messageCount":1,"participants":["Carlos Rica"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"47761","messageId":"469E5C67.3090203@gmail.com","threadId":"9095","inReplyTo":null,"subject":"[PATCH] Rename read_pipe() with read_fd() and make its buffer nul-terminated.","fromName":"Carlos Rica","fromEmail":"jasampler@gmail.com","sentAt":"2007-07-18T18:31:03Z","receivedAt":"2007-07-18T18:31:03Z","isPatch":true,"sender":{"key":"jasampler@gmail.com","avatar":null},"body":"The new name is closer to the purpose of the function.\n\nA NUL-terminated buffer makes things easier when callers need that.\nSince the function returns only the memory written with data,\nalmost always allocating more space than needed because final\nsize is unknown, an extra NUL terminating the buffer is harmless.\nIt is not included in the returned size, so the function\nremains working as before.\n\nAlso, now the function allows the buffer passed to be NULL at first,\nand alloc_nr is now used for growing the buffer, instead size=*2.\n\nSigned-off-by: Carlos Rica <jasampler@gmail.com>\n---\n builtin-stripspace.c |    4 +++-\n cache.h              |    2 +-\n mktag.c              |    2 +-\n sha1_file.c          |   21 +++++++++++++++------\n 4 files changed, 20 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin-stripspace.c b/builtin-stripspace.c\nindex 0c970aa..5571687 100644\n--- a/builtin-stripspace.c\n+++ b/builtin-stripspace.c\n@@ -79,8 +79,10 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)\n\n \tsize = 1024;\n \tbuffer = xmalloc(size);\n-\tif (read_pipe(0, &buffer, &size))\n+\tif (read_fd(0, &buffer, &size)) {\n+\t\tfree(buffer);\n \t\tdie(\"could not read the input\");\n+\t}\n\n \tsize = stripspace(buffer, size, 0);\n \twrite_or_die(1, buffer, size);\ndiff --git a/cache.h b/cache.h\nindex 328c1ad..53801b8 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -265,7 +265,7 @@ extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat\n extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, int);\n extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);\n extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);\n-extern int read_pipe(int fd, char** return_buf, unsigned long* return_size);\n+extern int read_fd(int fd, char **return_buf, unsigned long *return_size);\n extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);\n extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);\n extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);\ndiff --git a/mktag.c b/mktag.c\nindex b82e377..38acd5a 100644\n--- a/mktag.c\n+++ b/mktag.c\n@@ -120,7 +120,7 @@ int main(int argc, char **argv)\n\n \tsetup_git_directory();\n\n-\tif (read_pipe(0, &buffer, &size)) {\n+\tif (read_fd(0, &buffer, &size)) {\n \t\tfree(buffer);\n \t\tdie(\"could not read from stdin\");\n \t}\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 1efd9ae..aca741b 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2304,27 +2304,36 @@ int has_sha1_file(const unsigned char *sha1)\n  *\n  * returns 0 if anything went fine and -1 otherwise\n  *\n+ * The buffer is always NUL-terminated, not including it in returned size.\n+ *\n  * NOTE: both buf and size may change, but even when -1 is returned\n  * you still have to free() it yourself.\n  */\n-int read_pipe(int fd, char** return_buf, unsigned long* return_size)\n+int read_fd(int fd, char **return_buf, unsigned long *return_size)\n {\n-\tchar* buf = *return_buf;\n+\tchar *buf = *return_buf;\n \tunsigned long size = *return_size;\n \tssize_t iret;\n \tunsigned long off = 0;\n\n+\tif (!buf || size <= 1) {\n+\t\tsize = 1024;\n+\t\tbuf = xrealloc(buf, size);\n+\t}\n+\n \tdo {\n-\t\tiret = xread(fd, buf + off, size - off);\n+\t\tiret = xread(fd, buf + off, (size - 1) - off);\n \t\tif (iret > 0) {\n \t\t\toff += iret;\n-\t\t\tif (off == size) {\n-\t\t\t\tsize *= 2;\n+\t\t\tif (off == size - 1) {\n+\t\t\t\tsize = alloc_nr(size);\n \t\t\t\tbuf = xrealloc(buf, size);\n \t\t\t}\n \t\t}\n \t} while (iret > 0);\n\n+\tbuf[off] = '\\0';\n+\n \t*return_buf = buf;\n \t*return_size = off;\n\n@@ -2339,7 +2348,7 @@ int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)\n \tchar *buf = xmalloc(size);\n \tint ret;\n\n-\tif (read_pipe(fd, &buf, &size)) {\n+\tif (read_fd(fd, &buf, &size)) {\n \t\tfree(buf);\n \t\treturn -1;\n \t}\n-- \n1.5.0\n"}]}