From da20104b5f2329f831db599c8904cbdf35a7c5f0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Tue, 13 Dec 2011 15:11:27 +0100
Subject: [PATCH] Stdio.sendfile: Fixed handling of file length argument in
 nb_sendfile.

Fixes [bug 6118].
---
 lib/modules/Stdio.pmod/module.pmod | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/lib/modules/Stdio.pmod/module.pmod b/lib/modules/Stdio.pmod/module.pmod
index c5e3607900..4fdcb047a5 100644
--- a/lib/modules/Stdio.pmod/module.pmod
+++ b/lib/modules/Stdio.pmod/module.pmod
@@ -2966,7 +2966,12 @@ protected class nb_sendfile
     SF_WERR("Blocking read.");
     if( sizeof( to_write ) > 2)
       return;
-    string more_data = from->read(DATA_CHUNK_SIZE, 1);
+    string more_data = "";
+    if ((len < 0) || (len > DATA_CHUNK_SIZE)) {
+      more_data = from->read(DATA_CHUNK_SIZE, 1);
+    } else if (len) {
+      more_data = from->read(len, 1);
+    }
     if (!more_data) {
       SF_WERR(sprintf("Blocking read failed with errno: %d\n", from->errno()));
       more_data = "";
@@ -2981,6 +2986,7 @@ protected class nb_sendfile
 	trailers = 0;
       }
     } else {
+      if (len > 0) len -= sizeof(more_data);
       to_write += ({ more_data });
     }
   }
@@ -2988,12 +2994,13 @@ protected class nb_sendfile
   protected void read_cb(mixed ignored, string data)
   {
     SF_WERR("Read callback.");
-    if (len > 0) {
+    if (len >= 0) {
       if (sizeof(data) < len) {
 	len -= sizeof(data);
 	to_write += data / (float) DATA_CHUNK_SIZE;
       } else {
 	to_write += data[..len-1] / (float) DATA_CHUNK_SIZE;
+	len = 0;
 	from->set_blocking();
 	reader_done();
 	return;
-- 
GitLab