From c92d7c3bc8c6d9c15424711bed198b8a43bbaa58 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Sat, 25 Aug 2012 22:10:45 +0100 Subject: [PATCH] Fix for Android: fread() might return < 0 --- mongoose.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mongoose.c b/mongoose.c index ef6b40d9..976870d3 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2581,16 +2581,19 @@ static void send_file_data(struct mg_connection *conn, FILE *fp, int64_t len) { while (len > 0) { // Calculate how much to read from the file in the buffer to_read = sizeof(buf); - if ((int64_t) to_read > len) + if ((int64_t) to_read > len) { to_read = (int) len; + } // Read from file, exit the loop on error - if ((num_read = fread(buf, 1, (size_t)to_read, fp)) == 0) + if ((num_read = fread(buf, 1, (size_t)to_read, fp)) <= 0) { break; + } // Send read bytes to the client, exit the loop on error - if ((num_written = mg_write(conn, buf, (size_t)num_read)) != num_read) + if ((num_written = mg_write(conn, buf, (size_t)num_read)) != num_read) { break; + } // Both read and were successful, adjust counters conn->num_bytes_sent += num_written;