logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

reallocarray.c (539B)


  1. // Copied from musl
  2. // Copyright © 2020 Ariadne Conill <ariadne@dereferenced.org>
  3. // SPDX-License-Identifier: MIT
  4. #define _POSIX_C_SOURCE 202405L
  5. #define _BSD_SOURCE
  6. #include "./reallocarray.h"
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #ifdef HAS_REALLOCARRAY
  10. void *
  11. utils_reallocarray(void *ptr, size_t m, size_t n)
  12. {
  13. return reallocarray(ptr, m, n);
  14. }
  15. #else /* HAS_REALLOCARRAY */
  16. void *
  17. utils_reallocarray(void *ptr, size_t m, size_t n)
  18. {
  19. if(n && m > -1 / n)
  20. {
  21. errno = ENOMEM;
  22. return 0;
  23. }
  24. return realloc(ptr, m * n);
  25. }
  26. #endif