Bug in interaction of closures, set! and `fxarithmetic-shift-right'

Bug #438835 reported by Andreas Rottmann
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Ikarus Scheme
Fix Committed
High
Abdulaziz Ghuloum

Bug Description

The following program produces a bogus result:

;; ----------------------------------
(import (rnrs))

(define-syntax trace
  (syntax-rules ()
    ((_ . args)
     (begin
       (for-each display (list . args))
       (newline)))
    #;
    ((_ . args) (begin))))

(define (make-bit-reader port)
  (let ((buf 61) (buflen 8))
    (lambda (count)
      (let ((v (fxbit-field buf 0 count)))
        (trace "BEFORE READ (c=" count "): buf=" buf ", buflen=" buflen)
        (set! buf (fxarithmetic-shift-right buf count))
        (set! buflen (fx- buflen count))
        (trace "AFTER READ: buf=" buf ", buflen=" buflen)
        v))))

(let ((get-bits (make-bit-reader
                 (open-bytevector-input-port #vu8(#b00111101 #b11000011 #b10100000)))))
  (get-bits 3))
;;--------------------------------

Running this with "ikarus --r6rs-script ikarus-set-bug.scm" results in
the following output:

BEFORE READ (c=3): buf=61, buflen=8
AFTER READ: buf=61, buflen=5

Note that the value of `buf' is unchanged after the `set!', even
though the shift amount `count' is 3. When one changes
`fxarithmetic-shift-right' to `bitwise-arithmetic-shift-right', the
problem goes away and the correct output is displayed:

BEFORE READ (c=3): buf=61, buflen=8
AFTER READ: buf=7, buflen=5

This is all with: Ikarus Scheme version 0.0.4-rc1+, 64-bit (revision
1856, build 2009-09-29).

I suspect this is some kind of compiler bug.

Related branches

Revision history for this message
Abdulaziz Ghuloum (aghuloum) wrote :

Fixed in revision 1859. Thanks.

It had nothing to do with closures and set!. It was just fxarithmetic-shift-right that was broken when the second argument is not a constant value (it was missing a shift of 3 bits).

You can test this by just saying
> (define x 3)
> (fxarithmetic-shift-right 61 x)
61

Thanks.

Changed in ikarus:
assignee: nobody → Abdulaziz Ghuloum (aghuloum)
importance: Undecided → High
status: New → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.