summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaiki Ueno <ueno@gnu.org>2021-03-09 13:41:59 +0100
committerDaiki Ueno <ueno@gnu.org>2021-04-04 10:20:59 +0200
commit65695373e10ed0654bff60fe0dcd00137ddfffe8 (patch)
treea0a0be455e3a0a3124a9b64c560b03a5b585c9ed
parent54e161b9dabe4bd3b08b532475294a37c7562b45 (diff)
downloadgnutls-65695373e10ed0654bff60fe0dcd00137ddfffe8.tar.gz
_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 <ueno@gnu.org> Co-authored-by: Alexander Sosedkin <asosedkin@redhat.com>
-rw-r--r--lib/str.c32
1 files changed, 32 insertions, 0 deletions
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.
*/