From: Ben Peart Date: Tue, 25 Apr 2017 15:09:11 GMT Subject: Re: [PATCH v6 1/8] pkt-line: add packet_read_line_gently() Message-ID: <4cadf613-7923-e8bb-c889-34e1bf5eb47c@gmail.com> In-Reply-To: Sorry if you get this twice, somehow Thunderbird converted my response to HTML On 4/24/2017 10:19 PM, Junio C Hamano wrote: > Ben Peart writes: > >> On 4/24/2017 12:21 AM, Junio C Hamano wrote: >>> Ben Peart writes: >>> >>> +{ >>>> + int len = packet_read(fd, NULL, NULL, >>>> + packet_buffer, sizeof(packet_buffer), >>>> + PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF); >>>> + if (dst_len) >>>> + *dst_len = len; >>>> + if (dst_line) >>>> + *dst_line = (len > 0) ? packet_buffer : NULL; >>> I have the same doubt as above for len == 0 case. >> packet_read() returns -1 when PACKET_READ_GENTLE_ON_EOF is passed and >> it hits truncated output from the remote process. > I know, but that is irrelevant to my question, which is about > CHOMP_NEWLINE. I didn't even ask "why a negative len treated > specially?" My question is about the case where len == 0. Your > patch treats len==0 just like len==-1, i.e. an error, but I do not > know if that is correct, hence my question. We both know len < 0 > is an error and you do not need to waste time elaborating on it. > > The packet_read_line() function returns NULL when len == 0 and PACKET_READ_CHOMP_NEWLINE is passed so I wrote the similar packet_read_line_gently() function to behave the same. How about I update the comment in the function header to make this more clear: diff --git a/pkt-line.h b/pkt-line.h index 7c278a158b..b2965869ad 100644 --- a/pkt-line.h +++ b/pkt-line.h @@ -78,9 +78,10 @@ char *packet_read_line(int fd, int *size); * Convenience wrapper for packet_read that sets the PACKET_READ_GENTLE_ON_EOF * and CHOMP_NEWLINE options. The return value specifies the number of bytes * read into the buffer or -1 on truncated input. If the *dst_line parameter - * is not NULL it will return NULL for a flush packet and otherwise points to - * a static buffer (that may be overwritten by subsequent calls). If the size - * parameter is not NULL, the length of the packet is written to it. + * is not NULL it will return NULL for a flush packet or when the number of + * bytes copied is zero and otherwise points to a static buffer (that may be + * overwritten by subsequent calls). If the size parameter is not NULL, the + * length of the packet is written to it. */ int packet_read_line_gently(int fd, int *size, char **dst_line);