[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] fport_write: Fix test of remaining bytes.

From: Ludovic Courtès
Subject: Re: [PATCH] fport_write: Fix test of remaining bytes.
Date: Tue, 31 Mar 2015 21:31:06 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

Doug Evans <address@hidden> skribis:

> diff --git a/libguile/fports.c b/libguile/fports.c
> index 8395f0e..ce1bf54 100644
> --- a/libguile/fports.c
> +++ b/libguile/fports.c
> @@ -869,7 +869,7 @@ fport_write (SCM port, const void *data, size_t size)
>         const void *ptr = ((const char *) data) + space;
>         size_t remaining = size - space;
> -       if (size >= pt->write_buf_size)
> +       if (remaining >= pt->write_buf_size)
>           {
>             if (full_write (SCM_FPORT_FDES (port), ptr, remaining)
>                 < remaining)

I spent a bit of time looking at this code.  The change makes sense, so
I have committed it.  AFAICS, it has no observable effect though,
because (size >= remaining) is always true here.

The attached program reproduces a case where we would take one branch
before the patch, and the other after the patch.


(use-modules (rnrs io ports)
             (rnrs bytevectors))

(define %seed
  (seed->random-state (logxor (getpid) (car (gettimeofday)))))

(define (random-bytevector n)
  "Return a random bytevector of N bytes."
  (let ((bv (make-bytevector n)))
    (let loop ((i 0))
      (if (< i n)
            (bytevector-u8-set! bv i (random 256 %seed))
            (loop (1+ i)))

(let* ((file ",t")
       (port (open-file file "wb"))
       (size 777)
       (bv1  (random-bytevector (- size 1)))
       (bv2  (random-bytevector size)))
  (setvbuf port _IOFBF size)
  (put-bytevector port bv1)
  (put-bytevector port bv2)
  (close-port port)

  (call-with-input-file file
    (lambda (port)
      (let ((one (get-bytevector-n port (bytevector-length bv1)))
            (two (get-bytevector-n port (bytevector-length bv2))))
        (pk (and (pk 'one (bytevector=? one bv1))
                 (pk 'two (bytevector=? two bv2))))))))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]