From 630ccd27d546aa434301139d12545750d021f5db Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Sat, 9 Nov 2019 20:51:53 -0800 Subject: [PATCH] Fix infinite loop on premature end-of-file. When files end early, read() will return with a zero return code. Before this patch, this would result in an infinite loop as no progress is ever made. Treat this gracefully and finish copying. (This came up reading generated files from a fuse-filesystem whose initial predicted stat() filesise didn't match the final size) --- upnphttp.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/upnphttp.c b/upnphttp.c index 974434e..90ecc7c 100644 --- a/upnphttp.c +++ b/upnphttp.c @@ -1289,6 +1289,10 @@ send_file(struct upnphttp * h, int sendfd, off_t offset, off_t end_offset) else if( errno != EAGAIN ) break; } + else if( ret == 0 ) + { + break; /* Premature end of file */ + } else { //DPRINTF(E_DEBUG, L_HTTP, "sent %lld bytes to %d. offset is now %lld.\n", ret, h->socket, offset); @@ -1309,6 +1313,10 @@ send_file(struct upnphttp * h, int sendfd, off_t offset, off_t end_offset) else break; } + else if( ret == 0 ) + { + break; /* premature end of file */ + } ret = write(h->ev.fd, buf, ret); if( ret == -1 ) { DPRINTF(E_DEBUG, L_HTTP, "write error :: error no. %d [%s]\n", errno, strerror(errno));