From a7613f605ae0acbe3a3fd8cde397464706c88952 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 29 May 2022 17:31:18 -0400 Subject: [PATCH 4/7] bindat (strz): Always write null terminator * lisp/emacs-lisp/bindat.el (strz): When specifying a fixed length for a packed strz string, make sure the null terminator is always written even if the length of the string to pack is greater than or equal to the fixed length. * test/lisp/emacs-lisp/bindat-tests.el (strz): Mark test as passing. --- lisp/emacs-lisp/bindat.el | 12 +++++++----- test/lisp/emacs-lisp/bindat-tests.el | 1 - 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lisp/emacs-lisp/bindat.el b/lisp/emacs-lisp/bindat.el index d64be721b2..12b2d20981 100644 --- a/lisp/emacs-lisp/bindat.el +++ b/lisp/emacs-lisp/bindat.el @@ -693,11 +693,13 @@ bindat--type (`(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) + `(progn + ;; Same as the str type, except always leave room for the null + ;; terminator. This assumes that `len' > 0. + (bindat--pack-str ,(1- len) . ,args) + ;; "Write" the null terminator. This assumes that `bindat-raw' was + ;; initialized with zeroes. + (setq bindat-idx (1+ bindat-idx))) `(bindat--pack-strz . ,args)))))) (cl-defmethod bindat--type (op (_ (eql 'bits)) len) diff --git a/test/lisp/emacs-lisp/bindat-tests.el b/test/lisp/emacs-lisp/bindat-tests.el index ea6d110b8b..5152f67c01 100644 --- a/test/lisp/emacs-lisp/bindat-tests.el +++ b/test/lisp/emacs-lisp/bindat-tests.el @@ -177,7 +177,6 @@ bindat-test--recursive (should (equal (bindat-pack spec "a") "\141\0"))) (ert-deftest bindat-test--strz-fixedlen-pack-overflow () - :expected-result :failed (should (equal (bindat-pack spec "abc") "\141\0"))) (ert-deftest bindat-test--strz-fixedlen-unpack () -- 2.36.1