From 478b53f935264bdfe4efe394f8d804a1361a6770 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 8 Apr 2017 13:57:36 +0200 Subject: Document memory requirements, minor style fixes, more use of BUFSIZ, fix warnings, and fix potential buffer overflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/stream.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'src/stream.c') diff --git a/src/stream.c b/src/stream.c index 4b2cc23..a54f7f0 100644 --- a/src/stream.c +++ b/src/stream.c @@ -32,14 +32,11 @@ eninit_stream(int status, struct stream *stream) } *p = '\0'; - w = strchr(stream->buf, ' '); - if (!w) + if (!(w = strchr(stream->buf, ' '))) goto bad_format; - h = strchr(w + 1, ' '); - if (!h) + if (!(h = strchr(w + 1, ' '))) goto bad_format; - f = strchr(h + 1, ' '); - if (!f) + if (!(f = strchr(h + 1, ' '))) goto bad_format; *w++ = *h++ = *f++ = '\0'; @@ -89,6 +86,8 @@ eninit_stream(int status, struct stream *stream) enset_pixel_size(status, stream); + stream->xptr = 0; + return; bad_format: enprintf(status, "%s: file format not supported\n", stream->file); @@ -181,19 +180,29 @@ enread_frame(int status, struct stream *stream, void *buf, size_t n) { char *buffer = buf; ssize_t r; - for (; stream->ptr < n; stream->ptr += (size_t)r) { - r = read(stream->fd, buffer + stream->ptr, n - stream->ptr); + size_t m; + + if (stream->ptr) { + m = stream->ptr < n ? stream->ptr : n; + memcpy(buffer + stream->xptr, stream->buf, m); + memmove(stream->buf, stream->buf + m, stream->ptr -= m); + stream->xptr += m; + } + + for (; stream->xptr < n; stream->xptr += (size_t)r) { + r = read(stream->fd, buffer + stream->xptr, n - stream->xptr); if (r < 0) { enprintf(status, "read %s:", stream->file); } else if (r == 0) { - if (!stream->ptr) + if (!stream->xptr) break; enprintf(status, "%s: incomplete frame", stream->file); } } - if (!stream->ptr) + + if (!stream->xptr) return 0; - stream->ptr -= n; + stream->xptr -= n; return 1; } -- cgit v1.2.3-70-g09d2