summaryrefslogtreecommitdiff
path: root/lib/str.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/str.c')
-rw-r--r--lib/str.c74
1 files changed, 45 insertions, 29 deletions
diff --git a/lib/str.c b/lib/str.c
index e31449937d..8007340f1e 100644
--- a/lib/str.c
+++ b/lib/str.c
@@ -87,15 +87,6 @@ void _gnutls_buffer_clear(gnutls_buffer_st * str)
#define MIN_CHUNK 1024
-static void align_allocd_with_data(gnutls_buffer_st * dest)
-{
- assert(dest->allocd != NULL);
- assert(dest->data != NULL);
- if (dest->length)
- memmove(dest->allocd, dest->data, dest->length);
- dest->data = dest->allocd;
-}
-
/**
* gnutls_buffer_append_data:
* @dest: the buffer to append to
@@ -113,7 +104,7 @@ gnutls_buffer_append_data(gnutls_buffer_t dest, const void *data,
size_t data_size)
{
size_t const tot_len = data_size + dest->length;
- size_t const unused = MEMSUB(dest->data, dest->allocd);
+ int ret;
if (unlikely(dest->data != NULL && dest->allocd == NULL))
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
@@ -126,25 +117,9 @@ gnutls_buffer_append_data(gnutls_buffer_t dest, const void *data,
return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
}
- if (dest->max_length >= tot_len) {
-
- if (dest->max_length - unused <= tot_len) {
- align_allocd_with_data(dest);
- }
- } else {
- size_t const new_len =
- MAX(data_size, MIN_CHUNK) + MAX(dest->max_length,
- MIN_CHUNK);
-
- dest->allocd = gnutls_realloc_fast(dest->allocd, new_len);
- if (dest->allocd == NULL) {
- gnutls_assert();
- return GNUTLS_E_MEMORY_ERROR;
- }
- dest->max_length = new_len;
- dest->data = dest->allocd + unused;
-
- align_allocd_with_data(dest);
+ ret = _gnutls_buffer_resize(dest, tot_len);
+ if (ret < 0) {
+ return ret;
}
assert(dest->data != NULL);
@@ -154,6 +129,45 @@ 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 + unused);
+ if (dest->allocd == NULL) {
+ gnutls_assert();
+ return GNUTLS_E_MEMORY_ERROR;
+ }
+ dest->max_length = new_size + unused;
+ dest->data = dest->allocd + unused;
+
+ return 0;
+}
+
+#else
+
+static void align_allocd_with_data(gnutls_buffer_st * dest)
+{
+ assert(dest->allocd != NULL);
+ assert(dest->data != NULL);
+ if (dest->length)
+ memmove(dest->allocd, dest->data, dest->length);
+ dest->data = dest->allocd;
+}
+
int _gnutls_buffer_resize(gnutls_buffer_st * dest, size_t new_size)
{
if (unlikely(dest->data != NULL && dest->allocd == NULL))
@@ -187,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.
*/