diff options
author | Richard Hansen <rhansen@rhansen.org> | 2022-06-16 15:21:57 -0400 |
---|---|---|
committer | Eli Zaretskii <eliz@gnu.org> | 2022-06-22 16:55:40 +0300 |
commit | 55c2102560751ae05c98fd04120abcf4595d2a57 (patch) | |
tree | fefaefc7181a31eb469e2e14ed9d12d80ad45764 /lisp/emacs-lisp | |
parent | eff42dc0af741cc56c52d7d9577d29fc16f9f665 (diff) | |
download | emacs-55c2102560751ae05c98fd04120abcf4595d2a57.tar.gz emacs-55c2102560751ae05c98fd04120abcf4595d2a57.tar.bz2 emacs-55c2102560751ae05c98fd04120abcf4595d2a57.zip |
bindat (strz): Null terminate fixed-length strings if there is room
* lisp/emacs-lisp/bindat.el (bindat--pack-strz): For fixed-length strz
fields, explicitly write a null terminator after the packed string if
there is room (bug#56048).
* doc/lispref/processes.texi (Bindat Types): Update documentation.
* test/lisp/emacs-lisp/bindat-tests.el (bindat-test--str-strz-prealloc):
Update tests.
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r-- | lisp/emacs-lisp/bindat.el | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/lisp/emacs-lisp/bindat.el b/lisp/emacs-lisp/bindat.el index 4a642bb9c5a..0ecac3d52aa 100644 --- a/lisp/emacs-lisp/bindat.el +++ b/lisp/emacs-lisp/bindat.el @@ -443,11 +443,14 @@ e.g. corresponding to STRUCT.FIELD1[INDEX2].FIELD3..." (defun bindat--pack-strz (len v) (let* ((v (string-to-unibyte v)) (vlen (length v))) + ;; Explicitly write a null terminator (if there's room) in case + ;; the user provided a pre-allocated string to `bindat-pack' that + ;; wasn't already zeroed. + (when (or (null len) (< vlen len)) + (aset bindat-raw (+ bindat-idx vlen) 0)) (if len ;; 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). + ;; (except for the null terminator possibly written above). (bindat--pack-str len v) (dotimes (i vlen) (when (= (aref v i) 0) @@ -456,10 +459,6 @@ e.g. corresponding to STRUCT.FIELD1[INDEX2].FIELD3..." ;; 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))) - ;; Explicitly write a null terminator in case the user provided - ;; a pre-allocated string to `bindat-pack' that wasn't already - ;; zeroed. - (aset bindat-raw (+ bindat-idx vlen) 0) (setq bindat-idx (+ bindat-idx vlen 1))))) (defun bindat--pack-bits (len v) |