From bd511d381f605aa09a17373e18b11f4a4a403cd8 Mon Sep 17 00:00:00 2001 From: "Philip.Hazel" Date: Wed, 12 Apr 2017 13:48:11 +0000 Subject: [PATCH] Fix valgrind warnings for unset padding at the end of the pcre2_real_code structure. --- ChangeLog | 5 +++++ src/pcre2_compile.c | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/ChangeLog b/ChangeLog index 177c3ef..ca793e6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -126,6 +126,11 @@ pcre2_set_heap_limit() or (*LIMIT_HEAP=xxx). Upgraded pcre2test to show the heap limit along with other pattern information, and to find the minimum when the find_limits modifier is set. +23. Write to the last 8 bytes of the pcre2_real_code structure when a compiled +pattern is set up so as to initialize any padding the compiler might have +included. This avoids valgrind warnings when a compiled pattern is copied, in +particular when it is serialized. + Version 10.23 14-February-2017 ------------------------------ diff --git a/src/pcre2_compile.c b/src/pcre2_compile.c index e823747..0c46b52 100644 --- a/src/pcre2_compile.c +++ b/src/pcre2_compile.c @@ -9283,7 +9283,14 @@ if (re == NULL) errorcode = ERR21; goto HAD_CB_ERROR; } + +/* The compiler may put padding at the end of the pcre2_real_code structure in +order to round it up to a multiple of 4 or 8 bytes. This means that when a +compiled pattern is copied (for example, when serialized) undefined bytes are +read, and this annoys debuggers such as valgrind. To avoid this, we explicitly +write to the last 8 bytes of the structure before setting the fields. */ +memset((char *)re + sizeof(pcre2_real_code) - 8, 0, 8); re->memctl = ccontext->memctl; re->tables = tables; re->executable_jit = NULL;