From 65695373e10ed0654bff60fe0dcd00137ddfffe8 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Tue, 9 Mar 2021 13:41:59 +0100 Subject: _gnutls_buffer_resize: add option to use allocation simpler logic This helps detect common mistakes[1] in realloc usage with valgrind, where the caller assumes that the original ptr is always returned. 1. https://bugzilla.mozilla.org/show_bug.cgi?id=1377618 Signed-off-by: Daiki Ueno Co-authored-by: Alexander Sosedkin --- lib/str.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/str.c b/lib/str.c index 2247fc322b..506fe17210 100644 --- a/lib/str.c +++ b/lib/str.c @@ -138,6 +138,36 @@ gnutls_buffer_append_data(gnutls_buffer_t dest, const void *data, return 0; } +#ifdef AGGRESSIVE_REALLOC + +/* Use a simpler logic for reallocation; i.e., always call + * gnutls_realloc_fast() and do not reclaim the no-longer-used + * area which has been removed from the beginning of buffer + * with _gnutls_buffer_pop_datum(). This helps hit more + * issues when running under valgrind. + */ +int _gnutls_buffer_resize(gnutls_buffer_st * dest, size_t new_size) +{ + size_t unused; + + if (unlikely(dest->data != NULL && dest->allocd == NULL)) + return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); + + unused = MEMSUB(dest->data, dest->allocd); + dest->allocd = + gnutls_realloc_fast(dest->allocd, new_size); + if (dest->allocd == NULL) { + gnutls_assert(); + return GNUTLS_E_MEMORY_ERROR; + } + dest->max_length = new_size; + dest->data = dest->allocd + unused; + + return 0; +} + +#else + int _gnutls_buffer_resize(gnutls_buffer_st * dest, size_t new_size) { if (unlikely(dest->data != NULL && dest->allocd == NULL)) @@ -171,6 +201,8 @@ int _gnutls_buffer_resize(gnutls_buffer_st * dest, size_t new_size) } } +#endif + /* Appends the provided string. The null termination byte is appended * but not included in length. */ -- cgit v1.2.1