Use of abort, exit, getenv or system
ID |
objectivec.correctness.avoid_stdlib_process_functions |
Severity |
low |
Remediation Complexity |
hard |
Remediation Risk |
high |
Remediation Effort |
medium |
Resource |
Api Misuse |
Language |
Objective-C |
Description
Reports a call to the <stdlib.h> process-control functions - abort, exit, _Exit, quick_exit, getenv and system.
Rationale
Each of these hands control of the process to the C runtime, past everything Cocoa expects to run. abort and exit end the process without unwinding, so dealloc, autorelease pools, applicationWillTerminate: and any pending write never happen, and on iOS an app that exits itself is indistinguishable from a crash to both the user and App Review. getenv reads ambient state that is empty or meaningless in a launched app bundle, which makes behaviour depend on how the process happened to be started. system forks a shell, is unavailable on iOS, and turns any interpolated value into shell input.
The following code illustrates the pattern detected by this rule:
@implementation Bootstrap
- (void)failHard:(NSString *)reason {
// FLAGGED: Use of abort, exit, getenv or system
abort();
}
@end
Remediation
Return an error up the call chain (NSError **) and let the caller decide, rather than ending the process. Where termination really is correct, let the application’s own lifecycle handle it. Read configuration from NSUserDefaults, the Info.plist via objectForInfoDictionaryKey:, or [[NSProcessInfo processInfo] environment] rather than getenv, and replace system with NSTask on macOS, which takes an argument array and needs no shell.