Permalink
Browse files

Fix issue on < 64-bit systems in packing an 8 byte value.

This sends a clear error of what the issue is only when we attempt to
pack that many bytes.  Parsing of this method should now work even on
32 bit systems.
  • Loading branch information...
1 parent 559f84b commit cf885f0eb3bfd8eda973aa4b5e89ec3bb235b01c @ahyatt committed Jul 8, 2012
Showing with 15 additions and 4 deletions.
  1. +5 −1 websocket-test.el
  2. +10 −3 websocket.el
View
@@ -245,7 +245,11 @@
(should (equal 300 (websocket-get-bytes (websocket-to-bytes 300 2) 2)))
(should (equal 70000 (websocket-get-bytes (websocket-to-bytes 70000 8) 8)))
(should-error (websocket-to-bytes 30 3))
- (should-error (websocket-to-bytes 300 1)))
+ (should-error (websocket-to-bytes 300 1))
+ ;; I'd like to test the error for 32-byte systems on 8-byte lengths,
+ ;; but elisp does not allow us to temporarily set constants such as
+ ;; most-positive-fixnum.
+ )
(ert-deftest websocket-encode-frame ()
;; We've tested websocket-read-frame, now we can use that to help
View
@@ -156,9 +156,16 @@ NBYTES much be a power of 2, up to 8."
(error "websocket-to-bytes: Value %d could not be expressed in %d bytes"
val nbytes))
(if (= nbytes 8)
- (bindat-pack `((:val vec 2 u32))
- `((:val . [,(/ val 4294967296)
- ,(mod val 4294967296)])))
+ (progn
+ (when (calc-eval "$ < 4294967296" 'pred most-positive-fixnum)
+ (error "Could not send an 8-byte value on this version of emacs.
+A 64-bit version of emacs may solve your problem."))
+ ;; Need to use calc even though at this point things are manageable,
+ ;; since some emacs cannot parse the value 4294967296, even if
+ ;; they never evaluate it.
+ (bindat-pack `((:val vec 2 u32))
+ `((:val . [,(calc-eval "floor($ / 4294967296)" 'raw val)
+ ,(calc-eval "$ % 4294967296" 'raw val)]))))
(bindat-pack
`((:val ,(cond ((= nbytes 1) 'u8)
((= nbytes 2) 'u16)

0 comments on commit cf885f0

Please sign in to comment.