<p>George Joseph <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/10028">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Benjamin Keith Ford: Looks good to me, but someone else must approve
Joshua Colp: Looks good to me, approved
George Joseph: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">iostream.c: Fix ast_iostream_gets() needlessly returning failure.<br><br>Providing a buffer larger than the internal buffer of ast_iostream_gets()<br>fails to get lines longer than the internal buffer.<br><br>* Made ast_iostream_gets() fill the supplied buffer with read data until<br>either a '\n' is found or the supplied buffer is filled just like fgets().<br><br>Change-Id: If18b3f6ee500e22f0633a68779ed09f7e0f305ed<br>---<br>M main/iostream.c<br>1 file changed, 36 insertions(+), 23 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/iostream.c b/main/iostream.c</span><br><span>index 819616a..15131c0 100644</span><br><span>--- a/main/iostream.c</span><br><span>+++ b/main/iostream.c</span><br><span>@@ -285,46 +285,59 @@</span><br><span> </span><br><span> ssize_t ast_iostream_gets(struct ast_iostream *stream, char *buffer, size_t size)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">- ssize_t r;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t remaining = size;</span><br><span style="color: hsl(120, 100%, 40%);">+ ssize_t accum_size = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ ssize_t len;</span><br><span> char *newline;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- do {</span><br><span style="color: hsl(120, 100%, 40%);">+ for (;;) {</span><br><span> /* Search for newline */</span><br><span> newline = memchr(stream->rbufhead, '\n', stream->rbuflen);</span><br><span> if (newline) {</span><br><span style="color: hsl(0, 100%, 40%);">- r = newline - stream->rbufhead + 1;</span><br><span style="color: hsl(0, 100%, 40%);">- if (r > size-1) {</span><br><span style="color: hsl(0, 100%, 40%);">- r = size-1;</span><br><span style="color: hsl(120, 100%, 40%);">+ len = newline - stream->rbufhead + 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (len > remaining - 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ len = remaining - 1;</span><br><span> }</span><br><span> break;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Enough data? */</span><br><span style="color: hsl(0, 100%, 40%);">- if (stream->rbuflen >= size - 1) {</span><br><span style="color: hsl(0, 100%, 40%);">- r = size - 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Enough buffered line data to fill request buffer? */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (stream->rbuflen >= remaining - 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ len = remaining - 1;</span><br><span> break;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* Try to fill in line buffer */</span><br><span style="color: hsl(0, 100%, 40%);">- if (stream->rbuflen && stream->rbuf != stream->rbufhead) {</span><br><span style="color: hsl(0, 100%, 40%);">- memmove(&stream->rbuf, stream->rbufhead, stream->rbuflen);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (stream->rbuflen) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Put leftover buffered line data into request buffer */</span><br><span style="color: hsl(120, 100%, 40%);">+ memcpy(buffer + accum_size, stream->rbufhead, stream->rbuflen);</span><br><span style="color: hsl(120, 100%, 40%);">+ remaining -= stream->rbuflen;</span><br><span style="color: hsl(120, 100%, 40%);">+ accum_size += stream->rbuflen;</span><br><span style="color: hsl(120, 100%, 40%);">+ stream->rbuflen = 0;</span><br><span> }</span><br><span> stream->rbufhead = stream->rbuf;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- r = iostream_read(stream, stream->rbufhead + stream->rbuflen, sizeof(stream->rbuf) - stream->rbuflen);</span><br><span style="color: hsl(0, 100%, 40%);">- if (r <= 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- return r;</span><br><span style="color: hsl(120, 100%, 40%);">+ len = iostream_read(stream, stream->rbuf, sizeof(stream->rbuf));</span><br><span style="color: hsl(120, 100%, 40%);">+ if (len == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Nothing new was read. Return whatever we have accumulated. */</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- stream->rbuflen += r;</span><br><span style="color: hsl(0, 100%, 40%);">- } while (1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (len < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (accum_size) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* We have an accumulated buffer so return that instead. */</span><br><span style="color: hsl(120, 100%, 40%);">+ len = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return len;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ stream->rbuflen += len;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Return r bytes with termination byte */</span><br><span style="color: hsl(0, 100%, 40%);">- memcpy(buffer, stream->rbufhead, r);</span><br><span style="color: hsl(0, 100%, 40%);">- buffer[r] = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- stream->rbuflen -= r;</span><br><span style="color: hsl(0, 100%, 40%);">- stream->rbufhead += r;</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Return read buffer string length */</span><br><span style="color: hsl(120, 100%, 40%);">+ memcpy(buffer + accum_size, stream->rbufhead, len);</span><br><span style="color: hsl(120, 100%, 40%);">+ buffer[accum_size + len] = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ stream->rbuflen -= len;</span><br><span style="color: hsl(120, 100%, 40%);">+ stream->rbufhead += len;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- return r;</span><br><span style="color: hsl(120, 100%, 40%);">+ return accum_size + len;</span><br><span> }</span><br><span> </span><br><span> ssize_t ast_iostream_discard(struct ast_iostream *stream, size_t size)</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10028">change 10028</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/10028"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: If18b3f6ee500e22f0633a68779ed09f7e0f305ed </div>
<div style="display:none"> Gerrit-Change-Number: 10028 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Richard Mudgett <rmudgett@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>