[hackers] [libzahl][PATCH] Fix undefined behavior on longjmp

From: Christopher Leonard <chris.j.leonard_AT_gmx.com>
Date: Sun, 29 Jul 2018 03:04:52 +0200

Apologies if libzahl patches don't go here.

By the C standard jmp_buf is some unknown array type. A sane
implementation works around this requirement by making jmp_buf a
length 1 array of an appropriate opaque struct, but this is not
guaranteed. The current code assumes this behavior.

This patch stores the unknown pointer value of arg in zsetup in
libzahl_jmp_buf, instead of trying to copy the buffer itself. This
could cause regressions in any code that changes the original buffer,
but this is an unlikely scenario (who would do that?).

The standard requires that same pointer for the call to longjmp, but
if you still don't want to store the pointer you could still improve
the code by replacing:

    *libzahl_jmp_buf = *env;

with:

    memcpy(libzahl_jmp_buf, env, sizeof(jmp_buf));

(yes I know this is pedantic, I was bored and I haven't tried
submitting a patch yet)

---
 src/internals.h | 2 +-
 src/zsetup.c    | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/internals.h b/src/internals.h
index 25389c3..8de2e18 100644
--- a/src/internals.h
+++ b/src/internals.h
_AT_@ -67,7 +67,7 @@ LIST_CONSTS
 #undef X
 
 extern z_t libzahl_tmp_divmod_ds[BITS_PER_CHAR];
-extern jmp_buf libzahl_jmp_buf;
+extern void *libzahl_jmp_buf;
 extern int libzahl_set_up;
 extern int libzahl_error;
 extern zahl_char_t **libzahl_pool[sizeof(size_t) * 8];
diff --git a/src/zsetup.c b/src/zsetup.c
index 7486860..ebba28f 100644
--- a/src/zsetup.c
+++ b/src/zsetup.c
_AT_@ -9,7 +9,7 @@ LIST_CONSTS
 #undef X
 
 z_t libzahl_tmp_divmod_ds[BITS_PER_CHAR];
-jmp_buf libzahl_jmp_buf;
+void *libzahl_jmp_buf;
 int libzahl_set_up = 0;
 int libzahl_error;
 zahl_char_t **libzahl_pool[sizeof(size_t) * 8];
_AT_@ -29,7 +29,7 @@ void
 zsetup(jmp_buf env)
 {
        size_t i;
-       *libzahl_jmp_buf = *env;
+       libzahl_jmp_buf = env;
 
        if (likely(!libzahl_set_up)) {
                libzahl_set_up = 1;
-- 
2.14.4
Received on Sun Jul 29 2018 - 03:04:52 CEST

This archive was generated by hypermail 2.3.0 : Sun Jul 29 2018 - 03:12:23 CEST