From c7037219b025ea7a53fc57528eaf5e41511f1e92 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 28 May 2022 23:53:51 -0400 Subject: [PATCH] ; 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. --- lisp/emacs-lisp/bindat.el | 21 +++++++++++++-------- 1 file 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 -- 2.39.2