Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Made the max number of bytes transimssible 2^29 - 1 instead of 2^32 - 1.

This is the maximum positive value an elisp integer can have on 32 bit
machines.
  • Loading branch information...
commit b65328cc09e7c802ae29142ec5e7fa1f18ecacf1 1 parent 29d8f2f
@ahyatt authored
Showing with 12 additions and 15 deletions.
  1. +5 −11 websocket-test.el
  2. +7 −4 websocket.el
View
16 websocket-test.el
@@ -45,17 +45,13 @@
"\x81\x85\x37\xfa\x21\x3d\x7f\x9f\x4d\x51\x58"
"'Hello' masked string example, taken from the RFC.")
-(defconst websocket-test-64-bit-p
- (calc-eval '("2^32 <= $") 'pred most-positive-fixnum))
-
(ert-deftest websocket-get-bytes ()
(should (equal #x5 (websocket-get-bytes "\x5" 1)))
(should (equal #x101 (websocket-get-bytes "\x1\x1" 2)))
(should (equal #xffffff
(websocket-get-bytes "\x0\x0\x0\x0\x0\xFF\xFF\xFF" 8)))
- (when websocket-test-64-bit-p
- (should-error (websocket-get-bytes "\x0\x0\x0\x1\x0\x0\x0\x1" 8)
- :type 'websocket-unparseable-frame))
+ (should-error (websocket-get-bytes "\x0\x0\x0\x1\x0\x0\x0\x1" 8)
+ :type 'websocket-unparseable-frame)
(should-error (websocket-get-bytes "\x0\x0\x0" 3))
(should-error (websocket-get-bytes "\x0" 2) :type 'websocket-unparseable-frame))
@@ -276,10 +272,8 @@
;; help test websocket-to-bytes.
(should (equal 30 (websocket-get-bytes (websocket-to-bytes 30 1) 1)))
(should (equal 300 (websocket-get-bytes (websocket-to-bytes 300 2) 2)))
- (let ((f (lambda () (websocket-to-bytes 70000 8))))
- (if websocket-test-64-bit-p
- (should (equal 70000 (websocket-get-bytes (funcall f) 8)))
- (should-error (funcall f))))
+ (should (equal 70000 (websocket-get-bytes (websocket-to-bytes 70000 8) 8)))
+ (should-error (websocket-to-bytes 536870912 8) :type 'websocket-frame-too-large)
(should-error (websocket-to-bytes 30 3))
(should-error (websocket-to-bytes 300 1))
;; I'd like to test the error for 32-byte systems on 8-byte lengths,
@@ -295,7 +289,7 @@
websocket-test-hello
(websocket-encode-frame
(make-websocket-frame :opcode 'text :payload "Hello" :completep t))))
- (dolist (len (if websocket-test-64-bit-p '(200 70000) '(200 60000)))
+ (dolist (len '(200 70000))
(let ((long-string (make-string len ?x)))
(should (equal long-string
(websocket-frame-payload
View
11 websocket.el
@@ -198,13 +198,15 @@ This is based on the KEY from the Sec-WebSocket-Key header."
Return the value as an unsigned integer. The value N must be a
power of 2, up to 8.
-We support getting frames up to 4294967295 bytes (2^32) long."
+We support getting frames up to 536870911 bytes (2^29 - 1),
+approximately 537M long."
(if (= n 8)
(let* ((32-bit-parts
(bindat-get-field (bindat-unpack '((:val vec 2 u32)) s) :val))
(cval
(logior (lsh (aref 32-bit-parts 0) 32) (aref 32-bit-parts 1))))
- (if (= (aref 32-bit-parts 0) 0)
+ (if (and (= (aref 32-bit-parts 0) 0)
+ (= (lsh (aref 32-bit-parts 1) -29) 0))
cval
(signal 'websocket-unparseable-frame
"Frame value found too large to parse!")))
@@ -229,7 +231,8 @@ We support getting frames up to 4294967295 bytes (2^32) long."
"Encode the integer VAL in NBYTES of data.
NBYTES much be a power of 2, up to 8.
-This supports encoding values up to "
+This supports encoding values up to 536870911 bytes (2^29 - 1),
+approximately 537M long."
(when (and (< nbytes 8)
(> val (expt 2 (* 8 nbytes))))
;; not a user-facing error, this must be caused from an error in
@@ -240,7 +243,7 @@ This supports encoding values up to "
(progn
(let ((hi-32bits (lsh val -32))
(low-32bits (logand #xffffffff val)))
- (when (> hi-32bits 0)
+ (when (or (> hi-32bits 0) (> (lsh low-32bits -29) 0))
(signal 'websocket-frame-too-large val))
(bindat-pack `((:val vec 2 u32))
`((:val . [,hi-32bits ,low-32bits])))))
Please sign in to comment.
Something went wrong with that request. Please try again.