From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefano Brivio To: passt-dev@passt.top Subject: Re: [PATCH v2 1/4] Add cleaner line-by-line reading primitives Date: Mon, 27 Jun 2022 12:36:32 +0200 Message-ID: <20220627123632.3c25bb18@elisabeth> In-Reply-To: <20220624021732.4062212-2-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============2304731041930393577==" --===============2304731041930393577== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit On Fri, 24 Jun 2022 12:17:29 +1000 David Gibson wrote: > [...] > > +/** > + * lineread_get() - Read a single line from file (no allocation) > + * @lr: Line reader state structure > + * @line: Place a pointer to the next line in this variable > + * > + * Return: Length of line read on success, 0 on EOF, negative on error > + */ > +int lineread_get(struct lineread *lr, char **line) > +{ > + bool eof = false; > + int line_len; > + > + while ((line_len = peek_line(lr, eof)) < 0) { > + int rc; > + > + if ((lr->next_line + lr->count) == LINEREAD_BUFFER_SIZE) { > + /* No space at end */ > + if (lr->next_line == 0) { > + /* Buffer is full, which means we've > + * hit a line too long for us to > + * process. FIXME: report error > + * better > + */ > + return -1; > + } > + memmove(lr->buf, lr->buf + lr->next_line, lr->count); > + lr->next_line = 0; > + } > + > + /* Read more data into the end of buffer */ > + rc = read(lr->fd, lr->buf + lr->next_line + lr->count, > + LINEREAD_BUFFER_SIZE - lr->next_line - lr->count); > + if (rc < 0) { > + return rc; > + } else if (rc == 0) { clang-tidy still complains about this one, and I think it's reasonable (though not fundamental) to change it, because (rc < 0) is a clear error condition we're returning right away. I'd just change this on merge. -- Stefano --===============2304731041930393577==--