{"thread":{"id":"9088","subject":"[PATCH] Rename read_pipe() with read_fd() and make its buffer nul-terminated.","startedAt":"2007-07-18T07:08:58Z","lastAt":"2007-07-18T15:40:09Z","messageCount":3,"participants":["Carlos Rica","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"47707","messageId":"469DBC8A.6090704@gmail.com","threadId":"9088","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-18T07:08:58Z","receivedAt":"2007-07-18T07:08:58Z","isPatch":true,"sender":{"key":"jasampler@gmail.com","avatar":null},"body":"The new name is closer to the purpose of the function.\n\nThe other change just makes things easier for callers needing a\nNUL-terminated buffer.\n\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          |   17 ++++++++++++++---\n 4 files changed, 19 insertions(+), 6 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..ec9b43d 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..563ec07 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2304,27 +2304,38 @@ 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 \tunsigned long size = *return_size;\n \tssize_t iret;\n \tunsigned long off = 0;\n\n+\tif (!buf || size <= 1) {\n+\t\tsize = alloc_nr(size);\n+\t\tbuf = xrealloc(buf, size);\n+\t}\n+\n \tdo {\n \t\tiret = xread(fd, buf + off, size - 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\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+\tif (off == size)\n+\t\tbuf = xrealloc(buf, size + 1);\n+\tbuf[off] = '\\0';\n+\n \t*return_buf = buf;\n \t*return_size = off;\n\n@@ -2339,7 +2350,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"},{"id":"47713","messageId":"7vps2q6tjf.fsf@assigned-by-dhcp.cox.net","threadId":"9088","inReplyTo":"469DBC8A.6090704@gmail.com","subject":"Re: [PATCH] Rename read_pipe() with read_fd() and make its buffer nul-terminated.","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-07-18T07:49:08Z","receivedAt":"2007-07-18T07:49:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Carlos Rica <jasampler@gmail.com> writes:\n\n> The new name is closer to the purpose of the function.\n>\n> The other change just makes things easier for callers needing a\n> NUL-terminated buffer.\n>\n> Since the function returns only the memory written with data,\n> almost always allocating more space than needed because final\n> size is unknown, an extra NUL terminating the buffer is harmless.\n> It is not included in the returned size, so the function\n> remains working as before.\n>\n> Also, now the function allows the buffer passed to be NULL at first,\n> and alloc_nr is now used for growing the buffer, instead size=*2.\n\nSome people may say function name change should be a separate\npatch by itself, but I'd let it pass for now...\n\n> diff --git a/sha1_file.c b/sha1_file.c\n> index 1efd9ae..563ec07 100644\n> --- a/sha1_file.c\n> +++ b/sha1_file.c\n> @@ -2304,27 +2304,38 @@ 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\nOur code seems to prefer pointer declaration to be spelled as\n\"type **var\", not \"type** var\".\n\n>  \tunsigned long size = *return_size;\n>  \tssize_t iret;\n>  \tunsigned long off = 0;\n>\n> +\tif (!buf || size <= 1) {\n> +\t\tsize = alloc_nr(size);\n> +\t\tbuf = xrealloc(buf, size);\n> +\t}\n> +\n\nHmph.  The reason this is not \"!size\" is because you would want\nmore than one.  As your plan is to use this mostly for the -F\noption of \"tag/commit\", I suspect using a bit larger default,\nsuch as 80 (just a line), or probably 1k (most log messages\nwould fit in such a buffer), would be more practical.\n\n>  \tdo {\n>  \t\tiret = xread(fd, buf + off, size - 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\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> +\tif (off == size)\n> +\t\tbuf = xrealloc(buf, size + 1);\n> +\tbuf[off] = '\\0';\n> +\n\nI wonder if doing xread(... (size-1) - off) in the loop would\nensure (off <= size-1) here.  You also would need to update the\nrealloc condition inside loop if you do so.\n"},{"id":"47740","messageId":"1b46aba20707180840l6c12fecfy4c610fe3a1a36c5e@mail.gmail.com","threadId":"9088","inReplyTo":"7vps2q6tjf.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] Rename read_pipe() with read_fd() and make its buffer nul-terminated.","fromName":"Carlos Rica","fromEmail":"jasampler@gmail.com","sentAt":"2007-07-18T15:40:09Z","receivedAt":"2007-07-18T15:40:09Z","isPatch":true,"sender":{"key":"jasampler@gmail.com","avatar":null},"body":"2007/7/18, Junio C Hamano <gitster@pobox.com>:\n>\n> >       unsigned long size = *return_size;\n> >       ssize_t iret;\n> >       unsigned long off = 0;\n> >\n> > +     if (!buf || size <= 1) {\n> > +             size = alloc_nr(size);\n> > +             buf = xrealloc(buf, size);\n> > +     }\n> > +\n>\n> Hmph.  The reason this is not \"!size\" is because you would want\n> more than one.  As your plan is to use this mostly for the -F\n> option of \"tag/commit\", I suspect using a bit larger default,\n> such as 80 (just a line), or probably 1k (most log messages\n> would fit in such a buffer), would be more practical.\n\nThe true reason is because I before tried using (size-1) instead of (size)\nin xread(..., size - off), and then I forgot to remove that condition. Sorry.\n\n>\n> >       do {\n> >               iret = xread(fd, buf + off, size - off);\n> >               if (iret > 0) {\n> >                       off += iret;\n> >                       if (off == size) {\n> > -                             size *= 2;\n> > +                             size = alloc_nr(size);\n> >                               buf = xrealloc(buf, size);\n> >                       }\n> >               }\n> >       } while (iret > 0);\n> >\n> > +     if (off == size)\n> > +             buf = xrealloc(buf, size + 1);\n> > +     buf[off] = '\\0';\n> > +\n>\n> I wonder if doing xread(... (size-1) - off) in the loop would\n> ensure (off <= size-1) here.  You also would need to update the\n> realloc condition inside loop if you do so.\n\nI have chosen that solution to leave the code easy to read, but the\n(size-1) version will avoid that additional (but non-frequent) realloc.\n\nI will resend the fixed patch along with the builtin-tag.c one.\n"}]}