Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. /******************************************************************************
  2. * Copyright (C) 2019 Alejandro Colomar Andrés *
  3. * SPDX-License-Identifier: LGPL-2.0-only *
  4. ******************************************************************************/
  5. __attribute__((malloc))
  6. inline
  7. void *mallocarray(ptrdiff_t nmemb, size_t size);
  8.  
  9. inline
  10. void *mallocarray(ptrdiff_t nmemb, size_t size)
  11. {
  12.  
  13. if (nmemb < 0)
  14. goto ovf;
  15. if ((size_t)nmemb > (SIZE_MAX / size))
  16. goto ovf;
  17.  
  18. return malloc(size * (size_t)nmemb);
  19. ovf:
  20. errno = ENOMEM;
  21. return NULL;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement