[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master e66d6b3793 2/5: bindat (strz): Fix off-by-one bug in computed len
From: |
Stefan Monnier |
Subject: |
master e66d6b3793 2/5: bindat (strz): Fix off-by-one bug in computed length |
Date: |
Wed, 1 Jun 2022 22:34:11 -0400 (EDT) |
branch: master
commit e66d6b379345063900eb3e99db6367c69a860cdf
Author: Richard Hansen <rhansen@rhansen.org>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>
bindat (strz): Fix off-by-one bug in computed length
* lisp/emacs-lisp/bindat.el (strz): Include null terminator when
computing packed string length.
* test/lisp/emacs-lisp/bindat-tests.el (strz): Mark tests as passing.
---
lisp/emacs-lisp/bindat.el | 4 ++--
test/lisp/emacs-lisp/bindat-tests.el | 2 --
2 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/lisp/emacs-lisp/bindat.el b/lisp/emacs-lisp/bindat.el
index c6d64975ec..b236e47e5b 100644
--- a/lisp/emacs-lisp/bindat.el
+++ b/lisp/emacs-lisp/bindat.el
@@ -688,9 +688,9 @@ 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
- ((null len) `(length ,val))
+ ((null len) `(1+ (length ,val)))
((numberp len) len)
- (t `(or ,len (length ,val))))))
+ (t `(or ,len (1+ (length ,val)))))))
(`(pack . ,args)
(macroexp-let2 nil len len
`(if ,len
diff --git a/test/lisp/emacs-lisp/bindat-tests.el
b/test/lisp/emacs-lisp/bindat-tests.el
index c8545a216b..cb7b6fe1c2 100644
--- a/test/lisp/emacs-lisp/bindat-tests.el
+++ b/test/lisp/emacs-lisp/bindat-tests.el
@@ -191,12 +191,10 @@
(let ((spec (bindat-type strz)))
(ert-deftest bindat-test--strz-varlen-len ()
- :expected-result :failed
(should (equal (bindat-length spec "") 1))
(should (equal (bindat-length spec "abc") 4)))
(ert-deftest bindat-test--strz-varlen-pack ()
- :expected-result :failed
(should (equal (bindat-pack spec "") "\0"))
(should (equal (bindat-pack spec "abc") "abc\0")))