From 62c8b7b665b7c4c54d53496407342c5aefc9f8d1 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 28 Dec 2020 11:38:58 -0800 Subject: [PATCH 1/3] canonicalize: simplify via scratch_buffer_dupfree * config/srclist.txt: Adjust accordingly. * lib/canonicalize-lgpl.c (realpath_stk): * lib/canonicalize.c (canonicalize_filename_mode_stk): Simplify by using scratch_buffer_dupfree. * lib/malloc/scratch_buffer.h (scratch_buffer_dupfree): New function. * lib/malloc/scratch_buffer_dupfree.c: New file. * modules/scratch_buffer (Files, Depends-on): Add malloc/scratch_buffer_dupfree.c. --- ChangeLog | 12 +++++++++ config/srclist.txt | 3 ++- lib/canonicalize-lgpl.c | 20 ++++---------- lib/canonicalize.c | 9 +++---- lib/malloc/scratch_buffer.h | 16 +++++++++++ lib/malloc/scratch_buffer_dupfree.c | 41 +++++++++++++++++++++++++++++ modules/scratch_buffer | 4 ++- 7 files changed, 83 insertions(+), 22 deletions(-) create mode 100644 lib/malloc/scratch_buffer_dupfree.c diff --git a/ChangeLog b/ChangeLog index 97966b427..0428619c1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2020-12-28 Paul Eggert + + canonicalize: simplify via scratch_buffer_dupfree + * config/srclist.txt: Adjust accordingly. + * lib/canonicalize-lgpl.c (realpath_stk): + * lib/canonicalize.c (canonicalize_filename_mode_stk): + Simplify by using scratch_buffer_dupfree. + * lib/malloc/scratch_buffer.h (scratch_buffer_dupfree): New function. + * lib/malloc/scratch_buffer_dupfree.c: New file. + * modules/scratch_buffer (Files, Depends-on): + Add malloc/scratch_buffer_dupfree.c. + 2020-12-27 Paul Eggert regex: remove glibc21.m4 diff --git a/config/srclist.txt b/config/srclist.txt index f33b1353f..3956082c8 100644 --- a/config/srclist.txt +++ b/config/srclist.txt @@ -49,7 +49,8 @@ $GNUORG Copyright/request-assign.future doc/Copyright $GNUORG Copyright/request-assign.program doc/Copyright $GNUORG Copyright/request-disclaim.changes doc/Copyright -$LIBCSRC include/scratch_buffer.h lib/malloc +#$LIBCSRC include/scratch_buffer.h lib/malloc +#$LIBCSRC malloc/scratch_buffer_dupfree.c lib/malloc $LIBCSRC malloc/scratch_buffer_grow.c lib/malloc $LIBCSRC malloc/scratch_buffer_grow_preserve.c lib/malloc $LIBCSRC malloc/scratch_buffer_set_array_size.c lib/malloc diff --git a/lib/canonicalize-lgpl.c b/lib/canonicalize-lgpl.c index 68b6fdf6e..7ac5d412d 100644 --- a/lib/canonicalize-lgpl.c +++ b/lib/canonicalize-lgpl.c @@ -413,24 +413,14 @@ error: error_nomem: scratch_buffer_free (&extra_buffer); scratch_buffer_free (&link_buffer); - if (failed || rname == resolved) - scratch_buffer_free (rname_buf); - - if (failed) - return NULL; - if (rname == resolved) - return rname; - idx_t rname_size = dest - rname; - if (rname == rname_on_stack) + if (failed || rname == resolved) { - rname = malloc (rname_size); - if (rname == NULL) - return NULL; - return memcpy (rname, rname_on_stack, rname_size); + scratch_buffer_free (rname_buf); + return failed ? NULL : resolved; } - char *result = realloc (rname, rname_size); - return result != NULL ? result : rname; + + return scratch_buffer_dupfree (rname_buf, dest - rname); } /* Return the canonical absolute name of file NAME. A canonical name diff --git a/lib/canonicalize.c b/lib/canonicalize.c index e8b74d855..48a49e412 100644 --- a/lib/canonicalize.c +++ b/lib/canonicalize.c @@ -469,11 +469,10 @@ error: return NULL; } - idx_t rname_size = dest - rname; - if (rname == rname_on_stack) - return xmemdup (rname, rname_size); - char *result = realloc (rname, rname_size); - return result != NULL ? result : rname; + char *result = scratch_buffer_dupfree (rname_buf, dest - rname); + if (!result) + xalloc_die (); + return result; } /* Return the canonical absolute name of file NAME, while treating diff --git a/lib/malloc/scratch_buffer.h b/lib/malloc/scratch_buffer.h index c39da7862..48d651b41 100644 --- a/lib/malloc/scratch_buffer.h +++ b/lib/malloc/scratch_buffer.h @@ -132,4 +132,20 @@ scratch_buffer_set_array_size (struct scratch_buffer *buffer, (buffer, nelem, size)); } +/* Return a copy of *BUFFER's first SIZE bytes as a heap-allocated block, + deallocating *BUFFER if it was heap-allocated. SIZE must be at + most *BUFFER's size. Return NULL (setting errno) on memory + exhaustion. */ +void *__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer, + size_t size); +libc_hidden_proto (__libc_scratch_buffer_dupfree) + +/* Alias for __libc_scratch_dupfree. */ +static __always_inline void * +scratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size) +{ + void *r = __libc_scratch_buffer_dupfree (buffer, size); + return __glibc_likely (r != NULL) ? r : NULL; +} + #endif /* _SCRATCH_BUFFER_H */ diff --git a/lib/malloc/scratch_buffer_dupfree.c b/lib/malloc/scratch_buffer_dupfree.c new file mode 100644 index 000000000..5561e99b0 --- /dev/null +++ b/lib/malloc/scratch_buffer_dupfree.c @@ -0,0 +1,41 @@ +/* Variable-sized buffer with on-stack default allocation. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _LIBC +# include +#endif + +#include +#include + +void * +__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size) +{ + void *data = buffer->data; + if (data == buffer->__space.__c) + { + void *copy = malloc (size); + return copy != NULL ? memcpy (copy, data, size) : NULL; + } + else + { + void *copy = realloc (data, size); + return copy != NULL ? copy : data; + } +} +libc_hidden_def (__libc_scratch_buffer_dupfree) diff --git a/modules/scratch_buffer b/modules/scratch_buffer index 4f9a72581..7eedae7cc 100644 --- a/modules/scratch_buffer +++ b/modules/scratch_buffer @@ -4,6 +4,7 @@ Variable-sized buffer with on-stack default allocation. Files: lib/scratch_buffer.h lib/malloc/scratch_buffer.h +lib/malloc/scratch_buffer_dupfree.c lib/malloc/scratch_buffer_grow.c lib/malloc/scratch_buffer_grow_preserve.c lib/malloc/scratch_buffer_set_array_size.c @@ -17,7 +18,8 @@ stddef configure.ac: Makefile.am: -lib_SOURCES += malloc/scratch_buffer_grow.c \ +lib_SOURCES += malloc/scratch_buffer_dupfree.c \ + malloc/scratch_buffer_grow.c \ malloc/scratch_buffer_grow_preserve.c \ malloc/scratch_buffer_set_array_size.c -- 2.27.0