Skip to content

SeekSuccessCheckedAsTruthy ​

warning native off by default

Group: 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 fseek call whose result is tested for truthiness as the condition of if, elseif, else if, while, do … while or for (only the last expression of a for condition list decides the loop). The result may reach the condition directly, through parentheses, through logical negation, through a loose or strict comparison with true/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

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.