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