From 926ebb68acc6d24980e2a8e90383d21636657012 Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Fri, 25 Jun 2004 16:37:50 +0000 Subject: [PATCH] added lizard_realloc() to avoid restoring and reestablishing SIGSEGv handler --- lib/lizard-safe.c | 19 ++++++++++++++++--- lib/lizard.h | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/lizard-safe.c b/lib/lizard-safe.c index 1c46cbdc..4e07036a 100644 --- a/lib/lizard-safe.c +++ b/lib/lizard-safe.c @@ -18,16 +18,22 @@ #include #include -struct lizard_buffer * -lizard_alloc(uns max_len) +static void +lizard_alloc_internal(struct lizard_buffer *buf, uns max_len) { - struct lizard_buffer *buf = xmalloc(sizeof(struct lizard_buffer)); buf->len = ALIGN(max_len + 3, PAGE_SIZE); // +3 due to the unaligned access buf->start = mmap(NULL, buf->len + PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (buf->start == MAP_FAILED) die("mmap(anonymous): %m"); if (mprotect(buf->start + buf->len, PAGE_SIZE, PROT_NONE) < 0) die("mprotect: %m"); +} + +struct lizard_buffer * +lizard_alloc(uns max_len) +{ + struct lizard_buffer *buf = xmalloc(sizeof(struct lizard_buffer)); + lizard_alloc_internal(buf, max_len); buf->old_sigsegv_handler = xmalloc(sizeof(struct sigaction)); handle_signal(SIGSEGV, buf->old_sigsegv_handler); return buf; @@ -42,6 +48,13 @@ lizard_free(struct lizard_buffer *buf) xfree(buf); } +void +lizard_realloc(struct lizard_buffer *buf, uns max_len) +{ + munmap(buf->start, buf->len + PAGE_SIZE); + lizard_alloc_internal(buf, max_len); +} + static jmp_buf safe_decompress_jump; static int sigsegv_handler(int signal UNUSED) diff --git a/lib/lizard.h b/lib/lizard.h index fc3631ac..3f72b96c 100644 --- a/lib/lizard.h +++ b/lib/lizard.h @@ -37,4 +37,5 @@ struct lizard_buffer { struct lizard_buffer *lizard_alloc(uns max_len); void lizard_free(struct lizard_buffer *buf); +void lizard_realloc(struct lizard_buffer *buf, uns max_len); int lizard_decompress_safe(byte *in, struct lizard_buffer *buf, uns expected_length); -- 2.39.2