* 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).
(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))))
(ert-deftest bindat-test--strz-varlen-pack ()
(should (equal (bindat-pack spec "") "\0"))
- (should (equal (bindat-pack spec "abc") "abc\0")))
+ (should (equal (bindat-pack spec "abc") "abc\0"))
+ ;; Null bytes in the input string break unpacking.
+ (should-error (bindat-pack spec "\0"))
+ (should-error (bindat-pack spec "\0x"))
+ (should-error (bindat-pack spec "x\0"))
+ (should-error (bindat-pack spec "x\0y")))
(ert-deftest bindat-test--strz-varlen-unpack ()
(should (equal (bindat-unpack spec "\0") ""))