SeekSuccessCheckedAsTruthy
warning native off by defaultGroup: Probable bugs
Compare the seek result with zero.
Example
Finding:
php
<?php
if (fseek($fp, 0)) { echo "seeked"; }Reported:
- line 2: Compare the seek result with zero.
Detection
This is an opt-in policy. Truthiness, previous-state checks, raw wait-status comparisons and dispatch acknowledgements can be intentional; enabling this rule requests explicit handling of the contract described below.
- Report a resolved
fseekcall whose result is tested for truthiness as the condition ofif,elseif,else if,while,do … whileorfor(only the last expression of aforcondition list decides the loop). The result may reach the condition directly, through parentheses, through logical negation, through a loose or strict comparison withtrue/false, or through a plain=assignment whose assigned value is the call. These tests treat nonzero as success although fseek returns zero on success and minus one on failure. No claim is made about other positions such as ternary conditions,&&/||operands or branch bodies. - Emit one finding per violating operation. Flow facts require a reachable path with no intervening mutation, escaped alias, unknown call involving the tracked value or analysis-budget exhaustion. Uncertain facts cannot prove a violation.
Exceptions (no report)
- Strict result comparisons against 0 or -1 and merely storing the result are excluded. rewind returns boolean and must not be flagged.
- Exclude shadowed builtin symbols, unresolved required types, incomplete syntax and unsupported PHP versions.
- A first-class callable
fseek(...)creates a closure and performs no seek, so it is never reported. - A compound assignment in the condition (
$x += fseek(…),$x ??= fseek(…), …) tests the combined value rather than the seek result, and a call used inside the assignment target ($a[fseek(…)] = 1) is not the tested value; neither is reported.
Fix
No automatic fix. Choosing a repair requires runtime information, error-handling policy or a semantic decision.
All edits use exact byte ranges, preserve comments and evaluation order, and require syntax supported by the target PHP version. A fix must never add an evaluation or silently change unrelated arguments.
PHP versions
Available throughout PHP 5.3–8.5.
Configure
In custos.json:
json
{
"rules": {
"SeekSuccessCheckedAsTruthy": {
"enabled": true
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
php
// @custos-ignore SeekSuccessCheckedAsTruthy