]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
_gnutls_buffer_resize: add option to use allocation simpler logic
authorDaiki Ueno <ueno@gnu.org>
Tue, 9 Mar 2021 12:41:59 +0000 (13:41 +0100)
committerDaiki Ueno <ueno@gnu.org>
Wed, 10 Mar 2021 04:30:14 +0000 (05:30 +0100)
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>
.gitlab-ci.yml
lib/str.c

index 46bb2186e27050c50de2f354f51d66832efc6b84..50798871686884c6579d33b5194387f017a5d36d 100644 (file)
@@ -389,6 +389,33 @@ fedora-valgrind/test:
   only:
     - branches@gnutls/gnutls
 
+fedora-valgrind-aggressive/build:
+  extends:
+    - .build
+    - .fedora
+  script:
+    - ./bootstrap
+    # gcc in fedora31 inlines strcmp in a way that causes valgrind errors
+    - CFLAGS="-O2 -g -fno-builtin-strcmp -DAGGRESSIVE_REALLOC" ./configure --disable-gcc-warnings --disable-doc --cache-file $CCACHE_FILE --disable-guile --disable-full-test-suite --enable-valgrind-tests
+    - make -j$BUILDJOBS
+    - make -j$BUILDJOBS check TESTS=""
+  only:
+    - branches@gnutls/gnutls
+
+fedora-valgrind-aggressive/test:
+  extends:
+    - .test
+    - .fedora
+  script:
+    - GNUTLS_TEST_TIMEOUT=600000 make -j$(nproc) check
+  dependencies:
+    - fedora-valgrind-aggressive/build
+  needs:
+    - fedora-valgrind-aggressive/build
+  timeout: 5h
+  only:
+    - branches@gnutls/gnutls
+
 fedora-threadsan/build:
   extends:
     - .build
index 2247fc322b3df95ff164661e083806a7d9bf02de..506fe172108620312504f5798f9ae576b560c6bf 100644 (file)
--- 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.
  */