diff options
author | Richard Hansen <rhansen@rhansen.org> | 2022-05-28 23:53:51 -0400 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2022-06-10 11:53:30 +0200 |
commit | c7037219b025ea7a53fc57528eaf5e41511f1e92 (patch) | |
tree | 0daa6ae5865ff73de8265d783a27ec93c6b278ba /lisp/emacs-lisp/bindat.el | |
parent | 4dfa7061588c63158e32d8af2f554c1182618ec0 (diff) | |
download | emacs-c7037219b025ea7a53fc57528eaf5e41511f1e92.tar.gz emacs-c7037219b025ea7a53fc57528eaf5e41511f1e92.tar.bz2 emacs-c7037219b025ea7a53fc57528eaf5e41511f1e92.zip |
; bindat (strz): Consistent length type check, take two
Commit 30ec4a7347b2944818c6fc469ae871374ce7caa4 is incorrect -- the
length computation logic uses a simple nilness test, not `numberp'.
The `numberp' case is just an optimization if `len' is a literal
number; it does not affect the behavior.
Revert that commit, add some comments to help future readers avoid the
same mistake, and update the pack logic to use the same optimization
as the length computation for consistency.
Diffstat (limited to 'lisp/emacs-lisp/bindat.el')
-rw-r--r-- | lisp/emacs-lisp/bindat.el | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/lisp/emacs-lisp/bindat.el b/lisp/emacs-lisp/bindat.el index 0725b677cff..760c86feb41 100644 --- a/lisp/emacs-lisp/bindat.el +++ b/lisp/emacs-lisp/bindat.el @@ -688,18 +688,23 @@ is the name of a variable that will hold the value we need to pack.") ('unpack `(bindat--unpack-strz ,len)) (`(length ,val) `(cl-incf bindat-idx ,(cond + ;; Optimizations if len is a literal number or nil. ((null len) `(1+ (length ,val))) ((numberp len) len) + ;; General expression support. (t `(or ,len (1+ (length ,val))))))) (`(pack . ,args) - (macroexp-let2 nil len len - `(if (numberp ,len) - ;; Same as non-zero terminated strings since we don't actually add - ;; the terminating zero anyway (because we rely on the fact that - ;; `bindat-raw' was presumably initialized with all-zeroes before - ;; we started). - (bindat--pack-str ,len . ,args) - (bindat--pack-strz . ,args)))))) + ;; When len is specified, behave the same as the str type since we don't + ;; actually add the terminating zero anyway (because we rely on the fact + ;; that `bindat-raw' was presumably initialized with all-zeroes before we + ;; started). + (cond ; Same optimizations as 'length above. + ((null len) `(bindat--pack-strz . ,args)) + ((numberp len) `(bindat--pack-str ,len . ,args)) + (t (macroexp-let2 nil len len + `(if ,len + (bindat--pack-str ,len . ,args) + (bindat--pack-strz . ,args)))))))) (cl-defmethod bindat--type (op (_ (eql 'bits)) len) (bindat--pcase op |