Use of atoi, atol, atoll or atof

ID

objectivec.correctness.avoid_atoi_family

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Error Handling

Language

Objective-C

Description

Reports a call to atoi, atol, atoll or atof, the C string-to-number conversions that cannot report a conversion failure.

Rationale

These functions have no way to signal an error. A string that is not a number converts to 0, which is indistinguishable from the correct conversion of "0", so malformed configuration, user input or network data silently becomes a plausible-looking zero. A value too large for the return type is undefined behaviour rather than a reported overflow. Neither failure mode leaves any trace at the call site, which is what makes the resulting bugs so hard to trace back.

The following code illustrates the pattern detected by this rule:

@implementation SettingsParser
- (NSInteger)portFromString:(const char *)text {
    // FLAGGED: Use of atoi, atol, atoll or atof
    int port = atoi(text);
    return port;
}
@end

Remediation

Use a conversion that can fail. strtol/strtoll/strtod report where parsing stopped through their end pointer, so an unconsumed remainder or an empty conversion can be rejected. In Objective-C prefer NSScanner (scanInteger: followed by isAtEnd) or NSNumberFormatter numberFromString:, both of which return a definite success indication.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved