all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: 35880@debbugs.gnu.org
Cc: Pierre Neidhardt <mail@ambrevar.xyz>
Subject: [bug#35880] [PATCH 7/7] lzlib: 'lzread!' never returns more than it was asked for.
Date: Fri, 24 May 2019 15:42:38 +0200	[thread overview]
Message-ID: <20190524134238.22802-7-ludo@gnu.org> (raw)
In-Reply-To: <20190524134238.22802-1-ludo@gnu.org>

Fixes a bug whereby 'lzread!' could return more than COUNT.

* guix/lzlib.scm (lzread!): Rewrite in a semi-functional style.
---
 guix/lzlib.scm | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/guix/lzlib.scm b/guix/lzlib.scm
index 48927c6262..0e384f6cb8 100644
--- a/guix/lzlib.scm
+++ b/guix/lzlib.scm
@@ -494,29 +494,28 @@ perhaps not yet read."
 
 \f
 ;; High level functions.
-(define* (lzread! decoder file-port bv
+
+(define* (lzread! decoder port bv
                   #:optional (start 0) (count (bytevector-length bv)))
-  "Read up to COUNT bytes from FILE-PORT into BV at offset START.  Return the
+  "Read up to COUNT bytes from PORT into BV at offset START.  Return the
 number of uncompressed bytes actually read; it is zero if COUNT is zero or if
 the end-of-stream has been reached."
-  ;; WARNING: Because we don't alternate between lz-reads and lz-writes, we can't
-  ;; process more than lz-decompress-write-size from the file-port.
-  (when (> count (lz-decompress-write-size decoder))
-    (set! count (lz-decompress-write-size decoder)))
-  (let ((file-bv (get-bytevector-n file-port count)))
-    (unless (eof-object? file-bv)
-      (lz-decompress-write decoder file-bv 0 (bytevector-length file-bv))))
-  (let ((read 0))
-    (let loop ((rd 0))
-      (if (< start (bytevector-length bv))
-          (begin
-            (set! rd (lz-decompress-read decoder bv start (- (bytevector-length bv) start)))
-            (set! start (+ start rd))
-            (set! read (+ read rd)))
-          (set! rd 0))
-      (unless (= rd 0)
-        (loop rd)))
-    read))
+  (define (feed-decoder! decoder)
+    ;; Feed DECODER with data read from PORT.
+    (match (get-bytevector-n port (lz-decompress-write-size decoder))
+      ((? eof-object? eof) eof)
+      (bv (lz-decompress-write decoder bv))))
+
+  (let loop ((read 0)
+             (start start))
+    (cond ((< read count)
+           (match (lz-decompress-read decoder bv start (- count read))
+             (0 (if (eof-object? (feed-decoder! decoder))
+                    read
+                    (loop read start)))
+             (n (loop (+ read n) (+ start n)))))
+          (else
+           read))))
 
 (define (lzwrite! encoder source source-offset source-count
                   target target-offset target-count)
-- 
2.21.0

  parent reply	other threads:[~2019-05-24 13:44 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-24 13:31 [bug#35880] [PATCH 0/7] Lzip support for 'guix publish' and 'guix substitute' Ludovic Courtès
2019-05-24 13:42 ` [bug#35880] [PATCH 1/7] lzlib: Add 'make-lzip-input-port/compressed' Ludovic Courtès
2019-05-24 13:42   ` [bug#35880] [PATCH 2/7] utils: Test 'compressed-port' and 'decompressed-port' for both gzip and xz Ludovic Courtès
2019-05-24 13:42   ` [bug#35880] [PATCH 3/7] utils: Support compression and decompression with lzip Ludovic Courtès
2019-05-25 17:27     ` Pierre Neidhardt
2019-05-26 19:52       ` Ludovic Courtès
2019-05-24 13:42   ` [bug#35880] [PATCH 4/7] publish: Add support for lzip Ludovic Courtès
2019-05-24 13:42   ` [bug#35880] [PATCH 5/7] self: Add dependency on lzlib Ludovic Courtès
2019-05-24 13:42   ` [bug#35880] [PATCH 6/7] gnu: guix: " Ludovic Courtès
2019-05-24 13:42   ` Ludovic Courtès [this message]
2019-05-25 17:31     ` [bug#35880] [PATCH 7/7] lzlib: 'lzread!' never returns more than it was asked for Pierre Neidhardt
2019-05-26 19:54       ` Ludovic Courtès
2019-05-26 20:57         ` Pierre Neidhardt
2019-05-26 21:28           ` Ludovic Courtès
2019-05-27  7:00             ` Pierre Neidhardt
2019-05-27 10:00               ` Ludovic Courtès
2019-05-25 17:24   ` [bug#35880] [PATCH 1/7] lzlib: Add 'make-lzip-input-port/compressed' Pierre Neidhardt
2019-05-26 19:51     ` Ludovic Courtès
2019-05-27 15:45       ` Ludovic Courtès
2019-05-27 16:24         ` Pierre Neidhardt
2019-05-27 20:53           ` bug#35880: " Ludovic Courtès
2019-05-27 21:12             ` [bug#35880] " Pierre Neidhardt
2019-05-28  7:52               ` Ludovic Courtès
2019-05-28  8:46                 ` Pierre Neidhardt
2019-05-28 13:47                   ` Ludovic Courtès
2019-05-29 14:57                     ` Pierre Neidhardt
2019-05-31 20:54                       ` Ludovic Courtès
2019-06-01  6:02                         ` Pierre Neidhardt
2019-06-01  9:41                           ` Ludovic Courtès
2019-06-01  9:58                             ` Pierre Neidhardt
2019-06-01 12:21                               ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190524134238.22802-7-ludo@gnu.org \
    --to=ludo@gnu.org \
    --cc=35880@debbugs.gnu.org \
    --cc=mail@ambrevar.xyz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.