From 80cf0f3c1652196fc689bf72ca3b751fb3c52a01 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 5 Jun 2022 23:44:42 -0400 Subject: [PATCH 1/2] bindat (str, strz): Reject multibyte input strings * lisp/emacs-lisp/bindat.el (str) (strz): Signal an error if the user attempts to pack a multibyte string. * test/lisp/emacs-lisp/bindat-tests.el (str) (strz): Add tests. --- lisp/emacs-lisp/bindat.el | 4 ++++ test/lisp/emacs-lisp/bindat-tests.el | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lisp/emacs-lisp/bindat.el b/lisp/emacs-lisp/bindat.el index 5f3c772983..9ac24fa008 100644 --- a/lisp/emacs-lisp/bindat.el +++ b/lisp/emacs-lisp/bindat.el @@ -435,11 +435,15 @@ bindat--pack-u64r (bindat--pack-u32r (ash v -32))) (defun bindat--pack-str (len v) + (if (multibyte-string-p v) + (signal 'wrong-type-argument `(multibyte-string-p ,v))) (dotimes (i (min len (length v))) (aset bindat-raw (+ bindat-idx i) (aref v i))) (setq bindat-idx (+ bindat-idx len))) (defun bindat--pack-strz (v) + (if (multibyte-string-p v) + (signal 'wrong-type-argument `(multibyte-string-p ,v))) (let ((len (length v))) (dotimes (i len) (aset bindat-raw (+ bindat-idx i) (aref v i))) diff --git a/test/lisp/emacs-lisp/bindat-tests.el b/test/lisp/emacs-lisp/bindat-tests.el index 4817072752..da688d1e82 100644 --- a/test/lisp/emacs-lisp/bindat-tests.el +++ b/test/lisp/emacs-lisp/bindat-tests.el @@ -189,6 +189,20 @@ bindat-test--str-strz-prealloc (apply #'bindat-pack (append (car tc) (list prealloc))) (should (equal prealloc (cdr tc)))))) +(ert-deftest bindat-test--str-strz-multibyte () + (dolist (spec (list (bindat-type str 2) + (bindat-type strz 2) + (bindat-type strz))) + (should-error (bindat-pack spec (string-to-multibyte "x"))) + (should-error (bindat-pack spec (string-to-multibyte "\xff"))) + (should-error (bindat-pack spec "💩")) + (should-error (bindat-pack spec "\N{U+ff}"))) + (dolist (spec (list '((x str 2)) '((x strz 2)))) + (should-error (bindat-pack spec `((x . ,(string-to-multibyte "x"))))) + (should-error (bindat-pack spec `((x . ,(string-to-multibyte "\xff"))))) + (should-error (bindat-pack spec '((x . "💩")))) + (should-error (bindat-pack spec '((x . "\N{U+ff}")))))) + (let ((spec (bindat-type strz 2))) (ert-deftest bindat-test--strz-fixedlen-len () (should (equal (bindat-length spec "") 2)) -- 2.36.1