Sub topic 2
Hunting for Edge Cases and Logic Errors
Assume you can read a Git diff, run unit tests, and write basic Python. Treat generated code as an untrusted contribution. A plausible implementation is not evidence that its logic is correct.
Hunt for Edge Cases First
Start by asking what happens at the boundaries:
- What happens with an empty list, missing value, or zero?
- What happens with duplicate, very large, or unexpected input?
- What happens when a dependency fails or returns a different shape?
- Does the code preserve the requirement for invalid input?
For example, this function fails on an empty list:
def average(values):
return sum(values) / len(values)The requirement might not say what an empty list means. You must decide whether to raise an error, return a special value, or reject the input earlier. Do not silently choose behavior that callers cannot handle.
Use a small decision path while reviewing:
Check That Packages and APIs Are Real
Inspect every new import and unfamiliar function. Confirm that:
- The package exists and is approved for the project.
- The installed version supports the called API.
- Arguments, return values, and error behavior match the documentation.
- The code does not use an obsolete pattern when the project uses a newer one.
Run the project’s dependency and test commands rather than trusting names that look familiar. A hallucinated API can remain invisible until that branch executes.
Review for Security Flaws
Look for data crossing trust boundaries. Common problems include SQL built with string formatting, hardcoded credentials, missing authorization checks, and unvalidated file paths or request data.
Parameterized queries keep input separate from SQL:
def find_user(conn, name):
return conn.execute(
"SELECT id, name FROM users WHERE name = ?", (name,)
).fetchall()Also check logs and error messages for secrets or personal data.
Use Tests as Evidence
Tests should demonstrate the intended behavior, including failure cases:
import pytest
def average(values):
if not values:
raise ValueError("values must not be empty")
return sum(values) / len(values)
def test_empty_average_is_rejected():
with pytest.raises(ValueError):
average([])A passing test proves only what that test covers. Combine tests with the diff, static analysis, and manual reasoning.
Exercise
A generated function accepts a user-provided filename and opens it directly. What should you check first?
Show answer
Check path validation, authorization, traversal sequences such as ../, and tests for invalid paths before accepting the change.