scratch_buffer_set_array_size.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Variable-sized buffer with on-stack default allocation.
  2. Copyright (C) 2015-2023 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _LIBC
  16. # include <libc-config.h>
  17. #endif
  18. #include <scratch_buffer.h>
  19. #include <errno.h>
  20. #include <limits.h>
  21. bool
  22. __libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer,
  23. size_t nelem, size_t size)
  24. {
  25. size_t new_length = nelem * size;
  26. /* Avoid overflow check if both values are small. */
  27. if ((nelem | size) >> (sizeof (size_t) * CHAR_BIT / 2) != 0
  28. && nelem != 0 && size != new_length / nelem)
  29. {
  30. /* Overflow. Discard the old buffer, but it must remain valid
  31. to free. */
  32. scratch_buffer_free (buffer);
  33. scratch_buffer_init (buffer);
  34. __set_errno (ENOMEM);
  35. return false;
  36. }
  37. if (new_length <= buffer->length)
  38. return true;
  39. /* Discard old buffer. */
  40. scratch_buffer_free (buffer);
  41. char *new_ptr = malloc (new_length);
  42. if (new_ptr == NULL)
  43. {
  44. /* Buffer must remain valid to free. */
  45. scratch_buffer_init (buffer);
  46. return false;
  47. }
  48. /* Install new heap-based buffer. */
  49. buffer->data = new_ptr;
  50. buffer->length = new_length;
  51. return true;
  52. }
  53. libc_hidden_def (__libc_scratch_buffer_set_array_size)