unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#55938: [PATCH] bindat (strz): Error on null byte if packing variable-length string
@ 2022-06-13  5:48 Richard Hansen
  2022-06-13 12:32 ` Lars Ingebrigtsen
  2022-06-13 13:12 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 2 replies; 3+ messages in thread
From: Richard Hansen @ 2022-06-13  5:48 UTC (permalink / raw)
  To: 55938; +Cc: monnier


[-- Attachment #1.1.1: Type: text/plain, Size: 260 bytes --]

X-Debbugs-CC: monnier@iro.umontreal.ca

Attached patch:

* 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.

[-- Attachment #1.1.2: 0001-bindat-strz-Error-on-null-byte-if-packing-variable-l.patch --]
[-- Type: text/x-patch, Size: 2113 bytes --]

From f9f93183500aec3a2bf31ba12683861d7295d5b2 Mon Sep 17 00:00:00 2001
From: Richard Hansen <rhansen@rhansen.org>
Date: Mon, 6 Jun 2022 00:52:21 -0400
Subject: [PATCH] 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.
---
 lisp/emacs-lisp/bindat.el            | 5 +++++
 test/lisp/emacs-lisp/bindat-tests.el | 7 ++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/bindat.el b/lisp/emacs-lisp/bindat.el
index 2d6589b52d..e6740cb6e8 100644
--- a/lisp/emacs-lisp/bindat.el
+++ b/lisp/emacs-lisp/bindat.el
@@ -444,6 +444,11 @@ bindat--pack-strz
   (let* ((v (string-to-unibyte v))
          (len (length v)))
     (dotimes (i len)
+      (if (= (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))))
 
diff --git a/test/lisp/emacs-lisp/bindat-tests.el b/test/lisp/emacs-lisp/bindat-tests.el
index 8bb3baa485..7d1233ded7 100644
--- a/test/lisp/emacs-lisp/bindat-tests.el
+++ b/test/lisp/emacs-lisp/bindat-tests.el
@@ -240,7 +240,12 @@ bindat-test--str-strz-multibyte
 
   (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") ""))
-- 
2.36.1


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#55938: [PATCH] bindat (strz): Error on null byte if packing variable-length string
  2022-06-13  5:48 bug#55938: [PATCH] bindat (strz): Error on null byte if packing variable-length string Richard Hansen
@ 2022-06-13 12:32 ` Lars Ingebrigtsen
  2022-06-13 13:12 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 0 replies; 3+ messages in thread
From: Lars Ingebrigtsen @ 2022-06-13 12:32 UTC (permalink / raw)
  To: Richard Hansen; +Cc: 55938, monnier

Richard Hansen <rhansen@rhansen.org> writes:

> * 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.

Thanks; pushed to Emacs 29.  (But I changed the `if' to a `when'.)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#55938: [PATCH] bindat (strz): Error on null byte if packing variable-length string
  2022-06-13  5:48 bug#55938: [PATCH] bindat (strz): Error on null byte if packing variable-length string Richard Hansen
  2022-06-13 12:32 ` Lars Ingebrigtsen
@ 2022-06-13 13:12 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-06-13 13:12 UTC (permalink / raw)
  To: Richard Hansen; +Cc: 55938

>    (let* ((v (string-to-unibyte v))
>           (len (length v)))
>      (dotimes (i len)
> +      (if (= (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"))

I suspect a `string-match` looking for NUL would be faster.


        Stefan






^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-06-13 13:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-13  5:48 bug#55938: [PATCH] bindat (strz): Error on null byte if packing variable-length string Richard Hansen
2022-06-13 12:32 ` Lars Ingebrigtsen
2022-06-13 13:12 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).