|
|
147e83 |
Backport of the following upstream commits:
|
|
|
147e83 |
|
|
|
147e83 |
commit c04af6068b2c2c65c92b3360cbc4b51ad9843f21
|
|
|
147e83 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
147e83 |
Date: Thu Apr 7 13:46:28 2016 +0200
|
|
|
147e83 |
|
|
|
147e83 |
scratch_buffer_set_array_size: Include <limits.h>
|
|
|
147e83 |
|
|
|
147e83 |
It is needed for CHAR_BIT.
|
|
|
147e83 |
|
|
|
147e83 |
commit dbb7600658d8ea633083ee99572622b04ef23a3f
|
|
|
147e83 |
Author: Joseph Myers <joseph@codesourcery.com>
|
|
|
147e83 |
Date: Thu Oct 29 12:46:22 2015 +0000
|
|
|
147e83 |
|
|
|
147e83 |
Use max_align_t from <stddef.h>.
|
|
|
147e83 |
|
|
|
147e83 |
Now that we build with -std=gnu11 and can rely on a compiler providing
|
|
|
147e83 |
max_align_t in <stddef.h>, we no longer need our own version
|
|
|
147e83 |
libc_max_align_t. This patch removes it and replaces the single user
|
|
|
147e83 |
with a use of max_align_t.
|
|
|
147e83 |
|
|
|
147e83 |
Tested for x86_64 and x86 (testsuite, and that installed stripped
|
|
|
147e83 |
shared libraries are unchanged by the patch for x86_64; for x86, I see
|
|
|
147e83 |
some code reordering of no significance).
|
|
|
147e83 |
|
|
|
147e83 |
* include/libc-internal.h (libc_max_align_t): Remove typedef.
|
|
|
147e83 |
* include/scratch_buffer.h: Include <stddef.h> instead of
|
|
|
147e83 |
<libc-internal.h>.
|
|
|
147e83 |
(struct scratch_buffer): Use max_align_t instead of
|
|
|
147e83 |
libc_max_align_t.
|
|
|
147e83 |
|
|
|
147e83 |
(The above is a partial backport. It depends on max_align_t addition to
|
|
|
147e83 |
<include/sys/cdefs.h> because glibc 2.17 is not compiled in C11 mode, so
|
|
|
147e83 |
GCC's <stddef.h> does not provide it.)
|
|
|
147e83 |
|
|
|
147e83 |
commit 2902af1631c0c74c2f0c0edd7a85a523370e5027
|
|
|
147e83 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
147e83 |
Date: Thu Apr 9 17:12:42 2015 +0200
|
|
|
147e83 |
|
|
|
147e83 |
scratch_buffer: Suppress truncation warning on 32-bit
|
|
|
147e83 |
|
|
|
147e83 |
commit 72301304a5655662baf2bae88a7aceeabc4a753e
|
|
|
147e83 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
147e83 |
Date: Tue Apr 7 17:46:58 2015 +0200
|
|
|
147e83 |
|
|
|
147e83 |
scratch_buffer_grow_preserve: Add missing #include <string.h>
|
|
|
147e83 |
|
|
|
147e83 |
commit cfcfd4614b8b01b2782ac4dcafb21d14d74d5184
|
|
|
147e83 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
147e83 |
Date: Tue Apr 7 11:03:43 2015 +0200
|
|
|
147e83 |
|
|
|
147e83 |
Add struct scratch_buffer and its internal helper functions
|
|
|
147e83 |
|
|
|
147e83 |
These will be used from NSS modules, so they have to be exported.
|
|
|
147e83 |
|
|
|
147e83 |
diff --git a/include/scratch_buffer.h b/include/scratch_buffer.h
|
|
|
147e83 |
new file mode 100644
|
|
|
147e83 |
index 0000000000000000..dd17a4a7e13596aa
|
|
|
147e83 |
--- /dev/null
|
|
|
147e83 |
+++ b/include/scratch_buffer.h
|
|
|
147e83 |
@@ -0,0 +1,136 @@
|
|
|
147e83 |
+/* Variable-sized buffer with on-stack default allocation.
|
|
|
147e83 |
+ Copyright (C) 2015-2017 Free Software Foundation, Inc.
|
|
|
147e83 |
+ This file is part of the GNU C Library.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
147e83 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
147e83 |
+ License as published by the Free Software Foundation; either
|
|
|
147e83 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
147e83 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
147e83 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
147e83 |
+ Lesser General Public License for more details.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
147e83 |
+ License along with the GNU C Library; if not, see
|
|
|
147e83 |
+ <http://www.gnu.org/licenses/>. */
|
|
|
147e83 |
+
|
|
|
147e83 |
+#ifndef _SCRATCH_BUFFER_H
|
|
|
147e83 |
+#define _SCRATCH_BUFFER_H
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Scratch buffers with a default stack allocation and fallback to
|
|
|
147e83 |
+ heap allocation. It is expected that this function is used in this
|
|
|
147e83 |
+ way:
|
|
|
147e83 |
+
|
|
|
147e83 |
+ struct scratch_buffer tmpbuf;
|
|
|
147e83 |
+ scratch_buffer_init (&tmpbuf);
|
|
|
147e83 |
+
|
|
|
147e83 |
+ while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length))
|
|
|
147e83 |
+ if (!scratch_buffer_grow (&tmpbuf))
|
|
|
147e83 |
+ return -1;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ scratch_buffer_free (&tmpbuf);
|
|
|
147e83 |
+ return 0;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The allocation functions (scratch_buffer_grow,
|
|
|
147e83 |
+ scratch_buffer_grow_preserve, scratch_buffer_set_array_size) make
|
|
|
147e83 |
+ sure that the heap allocation, if any, is freed, so that the code
|
|
|
147e83 |
+ above does not have a memory leak. The buffer still remains in a
|
|
|
147e83 |
+ state that can be deallocated using scratch_buffer_free, so a loop
|
|
|
147e83 |
+ like this is valid as well:
|
|
|
147e83 |
+
|
|
|
147e83 |
+ struct scratch_buffer tmpbuf;
|
|
|
147e83 |
+ scratch_buffer_init (&tmpbuf);
|
|
|
147e83 |
+
|
|
|
147e83 |
+ while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length))
|
|
|
147e83 |
+ if (!scratch_buffer_grow (&tmpbuf))
|
|
|
147e83 |
+ break;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ scratch_buffer_free (&tmpbuf);
|
|
|
147e83 |
+
|
|
|
147e83 |
+ scratch_buffer_grow and scratch_buffer_grow_preserve are guaranteed
|
|
|
147e83 |
+ to grow the buffer by at least 512 bytes. This means that when
|
|
|
147e83 |
+ using the scratch buffer as a backing store for a non-character
|
|
|
147e83 |
+ array whose element size, in bytes, is 512 or smaller, the scratch
|
|
|
147e83 |
+ buffer only has to grow once to make room for at least one more
|
|
|
147e83 |
+ element.
|
|
|
147e83 |
+*/
|
|
|
147e83 |
+
|
|
|
147e83 |
+#include <stdbool.h>
|
|
|
147e83 |
+#include <stddef.h>
|
|
|
147e83 |
+#include <stdlib.h>
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Scratch buffer. Must be initialized with scratch_buffer_init
|
|
|
147e83 |
+ before its use. */
|
|
|
147e83 |
+struct scratch_buffer {
|
|
|
147e83 |
+ void *data; /* Pointer to the beginning of the scratch area. */
|
|
|
147e83 |
+ size_t length; /* Allocated space at the data pointer, in bytes. */
|
|
|
147e83 |
+ char __space[1024]
|
|
|
147e83 |
+ __attribute__ ((aligned (__alignof__ (max_align_t))));
|
|
|
147e83 |
+};
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Initializes *BUFFER so that BUFFER->data points to BUFFER->__space
|
|
|
147e83 |
+ and BUFFER->length reflects the available space. */
|
|
|
147e83 |
+static inline void
|
|
|
147e83 |
+scratch_buffer_init (struct scratch_buffer *buffer)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ buffer->data = buffer->__space;
|
|
|
147e83 |
+ buffer->length = sizeof (buffer->__space);
|
|
|
147e83 |
+}
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Deallocates *BUFFER (if it was heap-allocated). */
|
|
|
147e83 |
+static inline void
|
|
|
147e83 |
+scratch_buffer_free (struct scratch_buffer *buffer)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ if (buffer->data != buffer->__space)
|
|
|
147e83 |
+ free (buffer->data);
|
|
|
147e83 |
+}
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Grow *BUFFER by some arbitrary amount. The buffer contents is NOT
|
|
|
147e83 |
+ preserved. Return true on success, false on allocation failure (in
|
|
|
147e83 |
+ which case the old buffer is freed). On success, the new buffer is
|
|
|
147e83 |
+ larger than the previous size. On failure, *BUFFER is deallocated,
|
|
|
147e83 |
+ but remains in a free-able state, and errno is set. */
|
|
|
147e83 |
+bool __libc_scratch_buffer_grow (struct scratch_buffer *buffer);
|
|
|
147e83 |
+libc_hidden_proto (__libc_scratch_buffer_grow)
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Alias for __libc_scratch_buffer_grow. */
|
|
|
147e83 |
+static __always_inline bool
|
|
|
147e83 |
+scratch_buffer_grow (struct scratch_buffer *buffer)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ return __glibc_likely (__libc_scratch_buffer_grow (buffer));
|
|
|
147e83 |
+}
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Like __libc_scratch_buffer_grow, but preserve the old buffer
|
|
|
147e83 |
+ contents on success, as a prefix of the new buffer. */
|
|
|
147e83 |
+bool __libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer);
|
|
|
147e83 |
+libc_hidden_proto (__libc_scratch_buffer_grow_preserve)
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Alias for __libc_scratch_buffer_grow_preserve. */
|
|
|
147e83 |
+static __always_inline bool
|
|
|
147e83 |
+scratch_buffer_grow_preserve (struct scratch_buffer *buffer)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ return __glibc_likely (__libc_scratch_buffer_grow_preserve (buffer));
|
|
|
147e83 |
+}
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Grow *BUFFER so that it can store at least NELEM elements of SIZE
|
|
|
147e83 |
+ bytes. The buffer contents are NOT preserved. Both NELEM and SIZE
|
|
|
147e83 |
+ can be zero. Return true on success, false on allocation failure
|
|
|
147e83 |
+ (in which case the old buffer is freed, but *BUFFER remains in a
|
|
|
147e83 |
+ free-able state, and errno is set). It is unspecified whether this
|
|
|
147e83 |
+ function can reduce the array size. */
|
|
|
147e83 |
+bool __libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer,
|
|
|
147e83 |
+ size_t nelem, size_t size);
|
|
|
147e83 |
+libc_hidden_proto (__libc_scratch_buffer_set_array_size)
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* Alias for __libc_scratch_set_array_size. */
|
|
|
147e83 |
+static __always_inline bool
|
|
|
147e83 |
+scratch_buffer_set_array_size (struct scratch_buffer *buffer,
|
|
|
147e83 |
+ size_t nelem, size_t size)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ return __glibc_likely (__libc_scratch_buffer_set_array_size
|
|
|
147e83 |
+ (buffer, nelem, size));
|
|
|
147e83 |
+}
|
|
|
147e83 |
+
|
|
|
147e83 |
+#endif /* _SCRATCH_BUFFER_H */
|
|
|
147e83 |
diff --git a/malloc/Makefile b/malloc/Makefile
|
|
|
147e83 |
index 70e3cefa7b06157e..38aa9e0993d4880c 100644
|
|
|
147e83 |
--- a/malloc/Makefile
|
|
|
147e83 |
+++ b/malloc/Makefile
|
|
|
147e83 |
@@ -33,6 +33,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
|
|
|
147e83 |
tst-interpose-thread \
|
|
|
147e83 |
tst-interpose-static-nothread \
|
|
|
147e83 |
tst-interpose-static-thread \
|
|
|
147e83 |
+ tst-scratch_buffer \
|
|
|
147e83 |
|
|
|
147e83 |
tests-static := \
|
|
|
147e83 |
tst-interpose-static-nothread \
|
|
|
147e83 |
@@ -40,7 +41,11 @@ tests-static := \
|
|
|
147e83 |
|
|
|
147e83 |
test-srcs = tst-mtrace
|
|
|
147e83 |
|
|
|
147e83 |
-routines = malloc morecore mcheck mtrace obstack
|
|
|
147e83 |
+routines = malloc morecore mcheck mtrace obstack \
|
|
|
147e83 |
+ scratch_buffer_grow \
|
|
|
147e83 |
+ scratch_buffer_grow_preserve \
|
|
|
147e83 |
+ scratch_buffer_set_array_size \
|
|
|
147e83 |
+
|
|
|
147e83 |
|
|
|
147e83 |
install-lib := libmcheck.a
|
|
|
147e83 |
non-lib.a := libmcheck.a
|
|
|
147e83 |
diff --git a/malloc/Versions b/malloc/Versions
|
|
|
147e83 |
index 7ca9bdf25fcafdf2..f3c3d8a0934bdcd3 100644
|
|
|
147e83 |
--- a/malloc/Versions
|
|
|
147e83 |
+++ b/malloc/Versions
|
|
|
147e83 |
@@ -67,5 +67,10 @@ libc {
|
|
|
147e83 |
|
|
|
147e83 |
# Internal destructor hook for libpthread.
|
|
|
147e83 |
__libc_thread_freeres;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ # struct scratch_buffer support
|
|
|
147e83 |
+ __libc_scratch_buffer_grow;
|
|
|
147e83 |
+ __libc_scratch_buffer_grow_preserve;
|
|
|
147e83 |
+ __libc_scratch_buffer_set_array_size;
|
|
|
147e83 |
}
|
|
|
147e83 |
}
|
|
|
147e83 |
diff --git a/malloc/scratch_buffer_grow.c b/malloc/scratch_buffer_grow.c
|
|
|
147e83 |
new file mode 100644
|
|
|
147e83 |
index 0000000000000000..22bae506a1946eb4
|
|
|
147e83 |
--- /dev/null
|
|
|
147e83 |
+++ b/malloc/scratch_buffer_grow.c
|
|
|
147e83 |
@@ -0,0 +1,52 @@
|
|
|
147e83 |
+/* Variable-sized buffer with on-stack default allocation.
|
|
|
147e83 |
+ Copyright (C) 2015-2017 Free Software Foundation, Inc.
|
|
|
147e83 |
+ This file is part of the GNU C Library.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
147e83 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
147e83 |
+ License as published by the Free Software Foundation; either
|
|
|
147e83 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
147e83 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
147e83 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
147e83 |
+ Lesser General Public License for more details.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
147e83 |
+ License along with the GNU C Library; if not, see
|
|
|
147e83 |
+ <http://www.gnu.org/licenses/>. */
|
|
|
147e83 |
+
|
|
|
147e83 |
+#include <scratch_buffer.h>
|
|
|
147e83 |
+#include <errno.h>
|
|
|
147e83 |
+
|
|
|
147e83 |
+bool
|
|
|
147e83 |
+__libc_scratch_buffer_grow (struct scratch_buffer *buffer)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ void *new_ptr;
|
|
|
147e83 |
+ size_t new_length = buffer->length * 2;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Discard old buffer. */
|
|
|
147e83 |
+ scratch_buffer_free (buffer);
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Check for overflow. */
|
|
|
147e83 |
+ if (__glibc_likely (new_length >= buffer->length))
|
|
|
147e83 |
+ new_ptr = malloc (new_length);
|
|
|
147e83 |
+ else
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ __set_errno (ENOMEM);
|
|
|
147e83 |
+ new_ptr = NULL;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+
|
|
|
147e83 |
+ if (__glibc_unlikely (new_ptr == NULL))
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Buffer must remain valid to free. */
|
|
|
147e83 |
+ scratch_buffer_init (buffer);
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Install new heap-based buffer. */
|
|
|
147e83 |
+ buffer->data = new_ptr;
|
|
|
147e83 |
+ buffer->length = new_length;
|
|
|
147e83 |
+ return true;
|
|
|
147e83 |
+}
|
|
|
147e83 |
+libc_hidden_def (__libc_scratch_buffer_grow);
|
|
|
147e83 |
diff --git a/malloc/scratch_buffer_grow_preserve.c b/malloc/scratch_buffer_grow_preserve.c
|
|
|
147e83 |
new file mode 100644
|
|
|
147e83 |
index 0000000000000000..18543ef85b7a48e1
|
|
|
147e83 |
--- /dev/null
|
|
|
147e83 |
+++ b/malloc/scratch_buffer_grow_preserve.c
|
|
|
147e83 |
@@ -0,0 +1,63 @@
|
|
|
147e83 |
+/* Variable-sized buffer with on-stack default allocation.
|
|
|
147e83 |
+ Copyright (C) 2015-2017 Free Software Foundation, Inc.
|
|
|
147e83 |
+ This file is part of the GNU C Library.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
147e83 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
147e83 |
+ License as published by the Free Software Foundation; either
|
|
|
147e83 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
147e83 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
147e83 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
147e83 |
+ Lesser General Public License for more details.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
147e83 |
+ License along with the GNU C Library; if not, see
|
|
|
147e83 |
+ <http://www.gnu.org/licenses/>. */
|
|
|
147e83 |
+
|
|
|
147e83 |
+#include <scratch_buffer.h>
|
|
|
147e83 |
+#include <errno.h>
|
|
|
147e83 |
+#include <string.h>
|
|
|
147e83 |
+
|
|
|
147e83 |
+bool
|
|
|
147e83 |
+__libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ size_t new_length = 2 * buffer->length;
|
|
|
147e83 |
+ void *new_ptr;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ if (buffer->data == buffer->__space)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Move buffer to the heap. No overflow is possible because
|
|
|
147e83 |
+ buffer->length describes a small buffer on the stack. */
|
|
|
147e83 |
+ new_ptr = malloc (new_length);
|
|
|
147e83 |
+ if (new_ptr == NULL)
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ memcpy (new_ptr, buffer->__space, buffer->length);
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ else
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Buffer was already on the heap. Check for overflow. */
|
|
|
147e83 |
+ if (__glibc_likely (new_length >= buffer->length))
|
|
|
147e83 |
+ new_ptr = realloc (buffer->data, new_length);
|
|
|
147e83 |
+ else
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ __set_errno (ENOMEM);
|
|
|
147e83 |
+ new_ptr = NULL;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+
|
|
|
147e83 |
+ if (__glibc_unlikely (new_ptr == NULL))
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Deallocate, but buffer must remain valid to free. */
|
|
|
147e83 |
+ free (buffer->data);
|
|
|
147e83 |
+ scratch_buffer_init (buffer);
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Install new heap-based buffer. */
|
|
|
147e83 |
+ buffer->data = new_ptr;
|
|
|
147e83 |
+ buffer->length = new_length;
|
|
|
147e83 |
+ return true;
|
|
|
147e83 |
+}
|
|
|
147e83 |
+libc_hidden_def (__libc_scratch_buffer_grow_preserve);
|
|
|
147e83 |
diff --git a/malloc/scratch_buffer_set_array_size.c b/malloc/scratch_buffer_set_array_size.c
|
|
|
147e83 |
new file mode 100644
|
|
|
147e83 |
index 0000000000000000..8ab6d9d300d5c10d
|
|
|
147e83 |
--- /dev/null
|
|
|
147e83 |
+++ b/malloc/scratch_buffer_set_array_size.c
|
|
|
147e83 |
@@ -0,0 +1,60 @@
|
|
|
147e83 |
+/* Variable-sized buffer with on-stack default allocation.
|
|
|
147e83 |
+ Copyright (C) 2015-2017 Free Software Foundation, Inc.
|
|
|
147e83 |
+ This file is part of the GNU C Library.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
147e83 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
147e83 |
+ License as published by the Free Software Foundation; either
|
|
|
147e83 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
147e83 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
147e83 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
147e83 |
+ Lesser General Public License for more details.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
147e83 |
+ License along with the GNU C Library; if not, see
|
|
|
147e83 |
+ <http://www.gnu.org/licenses/>. */
|
|
|
147e83 |
+
|
|
|
147e83 |
+#include <scratch_buffer.h>
|
|
|
147e83 |
+#include <errno.h>
|
|
|
147e83 |
+#include <limits.h>
|
|
|
147e83 |
+
|
|
|
147e83 |
+bool
|
|
|
147e83 |
+__libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer,
|
|
|
147e83 |
+ size_t nelem, size_t size)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ size_t new_length = nelem * size;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Avoid overflow check if both values are small. */
|
|
|
147e83 |
+ if ((nelem | size) >> (sizeof (size_t) * CHAR_BIT / 2) != 0
|
|
|
147e83 |
+ && nelem != 0 && size != new_length / nelem)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Overflow. Discard the old buffer, but it must remain valid
|
|
|
147e83 |
+ to free. */
|
|
|
147e83 |
+ scratch_buffer_free (buffer);
|
|
|
147e83 |
+ scratch_buffer_init (buffer);
|
|
|
147e83 |
+ __set_errno (ENOMEM);
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+
|
|
|
147e83 |
+ if (new_length <= buffer->length)
|
|
|
147e83 |
+ return true;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Discard old buffer. */
|
|
|
147e83 |
+ scratch_buffer_free (buffer);
|
|
|
147e83 |
+
|
|
|
147e83 |
+ char *new_ptr = malloc (new_length);
|
|
|
147e83 |
+ if (new_ptr == NULL)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Buffer must remain valid to free. */
|
|
|
147e83 |
+ scratch_buffer_init (buffer);
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Install new heap-based buffer. */
|
|
|
147e83 |
+ buffer->data = new_ptr;
|
|
|
147e83 |
+ buffer->length = new_length;
|
|
|
147e83 |
+ return true;
|
|
|
147e83 |
+}
|
|
|
147e83 |
+libc_hidden_def (__libc_scratch_buffer_set_array_size);
|
|
|
147e83 |
diff --git a/malloc/tst-scratch_buffer.c b/malloc/tst-scratch_buffer.c
|
|
|
147e83 |
new file mode 100644
|
|
|
147e83 |
index 0000000000000000..5c9f3442ae8d6ef9
|
|
|
147e83 |
--- /dev/null
|
|
|
147e83 |
+++ b/malloc/tst-scratch_buffer.c
|
|
|
147e83 |
@@ -0,0 +1,155 @@
|
|
|
147e83 |
+/*
|
|
|
147e83 |
+ Copyright (C) 2015-2017 Free Software Foundation, Inc.
|
|
|
147e83 |
+ This file is part of the GNU C Library.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
147e83 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
147e83 |
+ License as published by the Free Software Foundation; either
|
|
|
147e83 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
147e83 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
147e83 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
147e83 |
+ Lesser General Public License for more details.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
147e83 |
+ License along with the GNU C Library; if not, see
|
|
|
147e83 |
+ <http://www.gnu.org/licenses/>. */
|
|
|
147e83 |
+
|
|
|
147e83 |
+#include <scratch_buffer.h>
|
|
|
147e83 |
+#include <stdbool.h>
|
|
|
147e83 |
+#include <stdio.h>
|
|
|
147e83 |
+#include <string.h>
|
|
|
147e83 |
+
|
|
|
147e83 |
+static bool
|
|
|
147e83 |
+unchanged_array_size (struct scratch_buffer *buf, size_t a, size_t b)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ size_t old_length = buf->length;
|
|
|
147e83 |
+ if (!scratch_buffer_set_array_size (buf, a, b))
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_set_array_size failed: %zu %zu\n",
|
|
|
147e83 |
+ a, b);
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ if (old_length != buf->length)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_set_array_size did not preserve size: %zu %zu\n",
|
|
|
147e83 |
+ a, b);
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ return true;
|
|
|
147e83 |
+}
|
|
|
147e83 |
+
|
|
|
147e83 |
+static bool
|
|
|
147e83 |
+array_size_must_fail (size_t a, size_t b)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ for (int pass = 0; pass < 2; ++pass)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ struct scratch_buffer buf;
|
|
|
147e83 |
+ scratch_buffer_init (&buf;;
|
|
|
147e83 |
+ if (pass > 0)
|
|
|
147e83 |
+ if (!scratch_buffer_grow (&buf))
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_grow in array_size_must_fail failed\n");
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ if (scratch_buffer_set_array_size (&buf, a, b))
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_set_array_size passed: %d %zu %zu\n",
|
|
|
147e83 |
+ pass, a, b);
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ if (buf.data != buf.__space)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_set_array_size did not free: %d %zu %zu\n",
|
|
|
147e83 |
+ pass, a, b);
|
|
|
147e83 |
+ return false;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ return true;
|
|
|
147e83 |
+}
|
|
|
147e83 |
+
|
|
|
147e83 |
+static int
|
|
|
147e83 |
+do_test (void)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ struct scratch_buffer buf;
|
|
|
147e83 |
+ scratch_buffer_init (&buf;;
|
|
|
147e83 |
+ memset (buf.data, ' ', buf.length);
|
|
|
147e83 |
+ scratch_buffer_free (&buf;;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ struct scratch_buffer buf;
|
|
|
147e83 |
+ scratch_buffer_init (&buf;;
|
|
|
147e83 |
+ memset (buf.data, ' ', buf.length);
|
|
|
147e83 |
+ size_t old_length = buf.length;
|
|
|
147e83 |
+ scratch_buffer_grow (&buf;;
|
|
|
147e83 |
+ if (buf.length <= old_length)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_grow did not enlarge buffer\n");
|
|
|
147e83 |
+ return 1;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ memset (buf.data, ' ', buf.length);
|
|
|
147e83 |
+ scratch_buffer_free (&buf;;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ struct scratch_buffer buf;
|
|
|
147e83 |
+ scratch_buffer_init (&buf;;
|
|
|
147e83 |
+ memset (buf.data, '@', buf.length);
|
|
|
147e83 |
+ strcpy (buf.data, "prefix");
|
|
|
147e83 |
+ size_t old_length = buf.length;
|
|
|
147e83 |
+ scratch_buffer_grow_preserve (&buf;;
|
|
|
147e83 |
+ if (buf.length <= old_length)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_grow_preserve did not enlarge buffer\n");
|
|
|
147e83 |
+ return 1;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ if (strcmp (buf.data, "prefix") != 0)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_grow_preserve did not copy buffer\n");
|
|
|
147e83 |
+ return 1;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ for (unsigned i = 7; i < old_length; ++i)
|
|
|
147e83 |
+ if (((char *)buf.data)[i] != '@')
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_grow_preserve did not copy buffer (%u)\n",
|
|
|
147e83 |
+ i);
|
|
|
147e83 |
+ return 1;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ scratch_buffer_free (&buf;;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ struct scratch_buffer buf;
|
|
|
147e83 |
+ scratch_buffer_init (&buf;;
|
|
|
147e83 |
+ for (int pass = 0; pass < 4; ++pass)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ if (!(unchanged_array_size (&buf, 0, 0)
|
|
|
147e83 |
+ && unchanged_array_size (&buf, 1, 0)
|
|
|
147e83 |
+ && unchanged_array_size (&buf, 0, 1)
|
|
|
147e83 |
+ && unchanged_array_size (&buf, -1, 0)
|
|
|
147e83 |
+ && unchanged_array_size (&buf, 0, -1)
|
|
|
147e83 |
+ && unchanged_array_size (&buf, 1ULL << 16, 0)
|
|
|
147e83 |
+ && unchanged_array_size (&buf, 0, 1ULL << 16)
|
|
|
147e83 |
+ && unchanged_array_size (&buf, (size_t) (1ULL << 32), 0)
|
|
|
147e83 |
+ && unchanged_array_size (&buf, 0, (size_t) (1ULL << 32))))
|
|
|
147e83 |
+ return 1;
|
|
|
147e83 |
+ if (!scratch_buffer_grow (&buf))
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ printf ("scratch_buffer_grow_failed (pass %d)\n", pass);
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ scratch_buffer_free (&buf;;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ if (!(array_size_must_fail (-1, 1)
|
|
|
147e83 |
+ && array_size_must_fail (-1, -1)
|
|
|
147e83 |
+ && array_size_must_fail (1, -1)
|
|
|
147e83 |
+ && array_size_must_fail (((size_t)-1) / 4, 4)
|
|
|
147e83 |
+ && array_size_must_fail (4, ((size_t)-1) / 4)))
|
|
|
147e83 |
+ return 1;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ return 0;
|
|
|
147e83 |
+}
|
|
|
147e83 |
+
|
|
|
147e83 |
+#define TEST_FUNCTION do_test ()
|
|
|
147e83 |
+#include "../test-skeleton.c"
|