emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master c7037219b0 3/4: ; bindat (strz): Consistent length type check, ta


From: Lars Ingebrigtsen
Subject: master c7037219b0 3/4: ; bindat (strz): Consistent length type check, take two
Date: Fri, 10 Jun 2022 05:54:00 -0400 (EDT)

branch: master
commit c7037219b025ea7a53fc57528eaf5e41511f1e92
Author: Richard Hansen <rhansen@rhansen.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    ; 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 0725b677cf..760c86feb4 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]