assert.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (C) 1991-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <atomic.h>
  16. #include <ldsodefs.h>
  17. #include <libintl.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sysdep.h>
  21. #include <unistd.h>
  22. #include <sys/mman.h>
  23. extern const char *__progname;
  24. #include <wchar.h>
  25. #include <libio/iolibio.h>
  26. #define fflush(s) _IO_fflush (s)
  27. /* This function, when passed a string containing an asserted
  28. expression, a filename, and a line number, prints a message
  29. on the standard error stream of the form:
  30. a.c:10: foobar: Assertion `a == b' failed.
  31. It then aborts program execution via a call to `abort'. */
  32. #ifdef FATAL_PREPARE_INCLUDE
  33. # include FATAL_PREPARE_INCLUDE
  34. #endif
  35. void
  36. __assert_fail_base (const char *fmt, const char *assertion, const char *file,
  37. unsigned int line, const char *function)
  38. {
  39. char *str;
  40. #ifdef FATAL_PREPARE
  41. FATAL_PREPARE;
  42. #endif
  43. int total;
  44. if (__asprintf (&str, fmt,
  45. __progname, __progname[0] ? ": " : "",
  46. file, line,
  47. function ? function : "", function ? ": " : "",
  48. assertion, &total) >= 0)
  49. {
  50. /* Print the message. */
  51. (void) __fxprintf (NULL, "%s", str);
  52. (void) fflush (stderr);
  53. total = (total + 1 + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
  54. struct abort_msg_s *buf = __mmap (NULL, total, PROT_READ | PROT_WRITE,
  55. MAP_ANON | MAP_PRIVATE, -1, 0);
  56. if (__glibc_likely (buf != MAP_FAILED))
  57. {
  58. buf->size = total;
  59. strcpy (buf->msg, str);
  60. /* We have to free the old buffer since the application might
  61. catch the SIGABRT signal. */
  62. struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg, buf);
  63. if (old != NULL)
  64. __munmap (old, old->size);
  65. }
  66. free (str);
  67. }
  68. else
  69. {
  70. /* At least print a minimal message. */
  71. static const char errstr[] = "Unexpected error.\n";
  72. __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
  73. }
  74. abort ();
  75. }
  76. #undef __assert_fail
  77. void
  78. __assert_fail (const char *assertion, const char *file, unsigned int line,
  79. const char *function)
  80. {
  81. __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n%n"),
  82. assertion, file, line, function);
  83. }
  84. hidden_def(__assert_fail)