Made assert() send SIGABRT when an assertion fails.

A signal handler can catch it, and do anything needed to make stderr work.
This commit is contained in:
Greg King
2019-11-10 12:46:01 -05:00
parent 7f7db01e25
commit ac4866c027
2 changed files with 18 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
<!doctype linuxdoc system> <!-- -*- text-mode -*- --> <!doctype linuxdoc system>
<article> <article>
<title>cc65 function reference <title>cc65 function reference
@@ -1316,11 +1316,11 @@ disabled.
<quote> <quote>
<descrip> <descrip>
<tag/Function/Terminates a program abnormally. <tag/Function/Terminate a program abnormally.
<tag/Header/<tt/<ref id="stdlib.h" name="stdlib.h">/ <tag/Header/<tt/<ref id="stdlib.h" name="stdlib.h">/
<tag/Declaration/<tt/void abort (void);/ <tag/Declaration/<tt/void abort (void);/
<tag/Description/<tt/abort/ raises <tt/SIGABRT/, writes a termination message <tag/Description/<tt/abort()/ raises <tt/SIGABRT/, writes a termination message
on stderr, then terminates the program with an exit code of 3. on <tt/stderr/, then terminates the program with an exit code of 3.
<tag/Availability/ISO 9899 <tag/Availability/ISO 9899
<tag/See also/ <tag/See also/
<ref id="assert" name="assert">, <ref id="assert" name="assert">,
@@ -1357,19 +1357,20 @@ used in presence of a prototype.
<quote> <quote>
<descrip> <descrip>
<tag/Function/Test a condition and possibly abort. <tag/Function/Test a condition, and possibly abort.
<tag/Header/<tt/<ref id="assert.h" name="assert.h">/ <tag/Header/<tt/<ref id="assert.h" name="assert.h">/
<tag/Declaration/<tt/void assert (int cond);/ <tag/Declaration/<tt/void assert (int cond);/
<tag/Description/<tt/assert/ is a macro that expands to an <tt/id/ <tag/Description/<tt/assert()/ is a macro that expands to an <tt/if/ statement.
statement. If the condition evaluates to zero (false), assert prints a message If the condition evaluates to zero (false), <tt/assert()/ raises <tt/SIGABRT/,
on stderr and aborts the program. prints a message on <tt/stderr/, then exits the program with an exit code of 2.
<tag/Notes/<itemize> <tag/Notes/<itemize>
<item>The function is actually a macro. <item>The function is actually a macro.
</itemize> </itemize>
<tag/Availability/ISO 9899 <tag/Availability/ISO 9899
<tag/See also/ <tag/See also/
<ref id="abort" name="abort">, <ref id="abort" name="abort">,
<ref id="exit" name="exit"> <ref id="exit" name="exit">,
<ref id="raise" name="raise">
<tag/Example/None. <tag/Example/None.
</descrip> </descrip>
</quote> </quote>
@@ -5820,18 +5821,19 @@ calling convention.
<tag/Function/Send a signal to the executing program. <tag/Function/Send a signal to the executing program.
<tag/Header/<tt/<ref id="signal.h" name="signal.h">/ <tag/Header/<tt/<ref id="signal.h" name="signal.h">/
<tag/Declaration/<tt/int __fastcall__ raise (int sig);/ <tag/Declaration/<tt/int __fastcall__ raise (int sig);/
<tag/Description/<tt/raise/ sends the given signal to the program. If the <tag/Description/<tt/raise()/ sends the given signal to the program. If the
program has installed a signal handler for the signal, this signal handler program has installed a signal handler for the signal, this signal handler
will be executed. If no handler has been installed, the default action for will be executed. If no handler has been installed, the default action for
the raised signal will be taken. The function returns zero on success, the raised signal will be taken. The function returns zero on success,
nonzero otherwise. non-zero otherwise.
<tag/Notes/<itemize> <tag/Notes/<itemize>
<item>The function is only available as fastcall function, so it may only <item>The function is available only as a fastcall function,
be used in presence of a prototype. so it may be used only in the presence of a prototype.
</itemize> </itemize>
<tag/Availability/ISO 9899 <tag/Availability/ISO 9899
<tag/See also/ <tag/See also/
<ref id="abort" name="abort">, <ref id="abort" name="abort">,
<ref id="assert" name="assert">,
<ref id="signal" name="signal"> <ref id="signal" name="signal">
<tag/Example/None. <tag/Example/None.
</descrip> </descrip>

View File

@@ -2,11 +2,12 @@
** _afailed.c ** _afailed.c
** **
** 1998-06-06, Ullrich von Bassewitz ** 1998-06-06, Ullrich von Bassewitz
** 2015-03-13, Greg King ** 2019-11-10, Greg King
*/ */
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -14,9 +15,7 @@
void __fastcall__ _afailed (char* file, unsigned line) void __fastcall__ _afailed (char* file, unsigned line)
{ {
raise (SIGABRT);
fprintf (stderr, "ASSERTION FAILED IN %s(%u)\n", file, line); fprintf (stderr, "ASSERTION FAILED IN %s(%u)\n", file, line);
exit (2); exit (2);
} }