bindat (strz): Error on null byte if packing variable-length string

* lisp/emacs-lisp/bindat.el (strz): Signal an error if a null byte is
encountered while packing a string to a variable-length strz field.
* test/lisp/emacs-lisp/bindat-tests.el (strz): Add tests (bug#55938).
This commit is contained in:
Richard Hansen 2022-06-13 14:32:01 +02:00 committed by Lars Ingebrigtsen
parent 86f30c972b
commit 86325f960a
2 changed files with 11 additions and 1 deletions

View file

@ -444,6 +444,11 @@ e.g. corresponding to STRUCT.FIELD1[INDEX2].FIELD3..."
(let* ((v (string-to-unibyte v))
(len (length v)))
(dotimes (i len)
(when (= (aref v i) 0)
;; Alternatively we could pretend that this was the end of
;; the string and stop packing, but then bindat-length would
;; need to scan the input string looking for a null byte.
(error "Null byte encountered in input strz string"))
(aset bindat-raw (+ bindat-idx i) (aref v i)))
(setq bindat-idx (+ bindat-idx len 1))))