CWE Rule 705
Description
Rule Description
The product does not properly return control flow to the proper location after it has completed a task or detected an unusual condition.
Polyspace Implementation
The rule checker checks for Abnormal termination of exit handler.
Examples
Abnormal termination of exit handler
This issue occurs when an exit handler itself calls another function that interrupts the program’s expected termination sequence and causes an abnormal exit.
Exit handlers are functions designated for execution when a program terminates. These functions are first registered with specific functions such as
atexit
, (WinAPI)_onexit
, orat_quick_exit()
.Some functions that can cause abnormal exits are
exit
,abort
,longjmp
, or (WinAPI)_onexit
.
If your exit handler terminates your program, you can have undefined behavior. Abnormal program termination means other exit handlers are not invoked. These additional exit handlers may do additional clean up or other required termination steps.
In inside exit handlers, remove calls to functions that prevent the exit handler from terminating normally.
#include <stdlib.h> volatile int some_condition = 1; void demo_exit1(void) { /* ... Cleanup code ... */ return; } void exitabnormalhandler(void) { if (some_condition) { /* Clean up */ exit(0); //Noncompliant } return; } int demo_install_exitabnormalhandler(void) { if (atexit(demo_exit1) != 0) /* demo_exit1() performs additional cleanup */ { /* Handle error */ } if (atexit(exitabnormalhandler) != 0) { /* Handle error */ } /* ... Program code ... */ return 0; }
In this example, demo_install_exitabnormalhandler
registers
two exit handlers, demo_exit1
and exitabnormalhandler
.
Exit handlers are invoked in the reverse order of which they are registered.
When the program ends, exitabnormalhandler
runs,
then demo_exit1
. However, exitabnormalhandler
calls exit
interrupting
the program exit process. Having this exit
inside
an exit handler causes undefined behavior because the program is not
finished cleaning up safely.
exit
from Exit
HandlerOne possible correction is to let your exit handlers terminate
normally. For this example, exit
is removed from exitabnormalhandler
,
allowing the exit termination process to complete as expected.
#include <stdlib.h> volatile int some_condition = 1; void demo_exit1(void) { /* ... Cleanup code ... */ return; } void exitabnormalhandler(void) { if (some_condition) { /* Clean up */ /* Return normally */ } return; } int demo_install_exitabnormalhandler(void) { if (atexit(demo_exit1) != 0) /* demo_exit1() continues clean up */ { /* Handle error */ } if (atexit(exitabnormalhandler) != 0) { /* Handle error */ } /* ... Program code ... */ return 0; }
Check Information
Category: Others |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)