aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/util
diff options
context:
space:
mode:
authorMarvin W <git@larma.de>2026-02-06 18:23:55 +0100
committerfiaxh <git@lightrise.org>2026-02-06 18:24:25 +0100
commita4e696261fb2e15fc4e883d10b074f107f8f40b8 (patch)
tree3867c1c53d6f2428222304495303886518565d1a /libdino/src/util
parente0254904da928290d6d510a10dbfdfd2a42250f9 (diff)
Fix accidental blocking reads in non-blocking downloads
Diffstat (limited to 'libdino/src/util')
-rw-r--r--libdino/src/util/limit_input_stream.vala11
1 files changed, 10 insertions, 1 deletions
diff --git a/libdino/src/util/limit_input_stream.vala b/libdino/src/util/limit_input_stream.vala
index 8f95ce62..0fc8c776 100644
--- a/libdino/src/util/limit_input_stream.vala
+++ b/libdino/src/util/limit_input_stream.vala
@@ -62,8 +62,17 @@ public class Dino.LimitInputStream : InputStream, PollableInputStream {
}
public ssize_t read_nonblocking_fn(uint8[] buffer) throws Error {
+ if (remaining_bytes == 0) return 0;
if (!is_readable()) throw new IOError.WOULD_BLOCK("Stream is not readable");
- return read(buffer);
+ int original_buffer_length = buffer.length;
+ if (remaining_bytes != -1 && (int64) buffer.length > remaining_bytes) {
+ // Never read more than remaining_bytes by limiting the buffer length
+ buffer.length = (int) remaining_bytes;
+ }
+ ssize_t read_bytes = ((PollableInputStream)inner).read_nonblocking_fn(buffer);
+ this.retrieved_bytes += read_bytes;
+ buffer.length = original_buffer_length;
+ return read_bytes;
}
public override bool close(Cancellable? cancellable = null) throws IOError {