---
name: errno-handling-finder
description: Finds errno handling mistakes
---

**Finding ID Prefix:** `ERRNO` (e.g., ERRNO-001, ERRNO-002)

**Bug Patterns to Find:**

1. **Negative Return Values Not Handled**
   - `read`/`write` can return -1
   - Network functions returning errors
   - Treating negative as valid count

2. **Partial Operations Not Handled**
   - `read` may not read all requested bytes
   - `write` may not write all requested bytes
   - Must loop until complete or error

3. **Functions Requiring errno Check**
   - `strtoul`/`strtol` don't return error
   - Must set `errno = 0` before, check after
   - `atoi` has no error indication at all

4. **Incorrect Error Comparison**
   - Function returns 1 on success, code checks `!= 0`
   - Function returns -1 on error, code checks `!= 1`
   - Wrong error code comparison

**Common False Positives to Avoid:**

- **Error handled in caller:** Error propagates up and is handled at a higher level
- **Intentional ignore:** Some return values legitimately don't need checking (e.g., `printf`)
- **Wrapper function handles it:** Low-level call wrapped in function that checks
- **Loop handles partial ops:** Outer loop already handles partial read/write
- **Best-effort operations:** Some operations are intentionally fire-and-forget

**Search Patterns:**
```
=\s*read\s*\(|=\s*write\s*\(|=\s*recv\s*\(|=\s*send\s*\(
strtoul\s*\(|strtol\s*\(|strtod\s*\(|strtof\s*\(
atoi\s*\(|atol\s*\(|atof\s*\(
errno\s*=\s*0|if\s*\(.*errno
if\s*\(.*!=\s*0|if\s*\(.*==\s*-1
```
