Skip to main content

SKY-P401 · Quality, structure and performance

Potential unbounded read

Low

The catalog gives no severity. Every finding is reported LOW.

file.read() and file.readlines() without a size load everything that is left in the source into memory at once. SKY-P401 reports such calls when Skylos cannot see that the whole value is needed.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans). Advice that is measured on every --quality scan but not listed by default. It is listed, and counted toward max_quality, with --all-advice or when the rule is named in [tool.skylos.gate] rules.
Finding type
advice
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Reading a 4 GB upload to hash it needs 4 GB of memory; a server handling a few such uploads at once is killed. Reading in fixed-size chunks keeps memory flat whatever the file size.

What Skylos flags

app/storage/checksum.py · PythonReported on line 8
"""Upload checksum for the file store.""" import hashlib  def upload_checksum(path):    with open(path, "rb") as handle:        data = handle.read()    return hashlib.sha256(data).hexdigest()

What Skylos leaves alone

app/settings.py · Python
"""Settings file loader.""" import json  def load_settings(path):    with open(path, encoding="utf-8") as handle:        return json.loads(handle.read())

Why Skylos leaves it alone

json.loads needs the whole text, so reading it in chunks would not help. Skylos does not report a read passed straight to a standard parser that requires the complete value, such as json.loads, ast.parse, tomllib.loads or zlib.decompress.

How Skylos decides

  1. Runs with --quality, on Python files. Test code, examples and benchmarks folders, a top-level docs folder and the root setup.py are skipped.
  2. Finds calls to .read() and .readlines(). A call with a size argument is bounded and left alone, even when the size is a variable; no size, None, a negative number, or readlines(0) reads everything.
  3. Leaves alone read() on an io.BytesIO or io.StringIO the file creates itself, ConfigParser.read() (its argument is file names), OpenCV VideoCapture.read(), a read passed straight to a parser that needs the whole value, and a read returned from a function declared to return str or bytes.
  4. Reports LOW on the line of the call. The message says whether the source is a file from open(), a zipfile member or an unknown stream.

How to fix it

Read in fixed-size chunks and feed each chunk to the consumer, or iterate over the file line by line.

app/storage/checksum.py · Python · the fix, against the flagged file
"""Upload checksum for the file store.""" import hashlib Added: CHUNK = 1024 * 1024 Added:  def upload_checksum(path):Added:     digest = hashlib.sha256()    with open(path, "rb") as handle:Removed:         data = handle.read()Removed:     return hashlib.sha256(data).hexdigest()Added:         while chunk := handle.read(CHUNK):Added:             digest.update(chunk)Added:     return digest.hexdigest()

When it can be wrong

  • A file that is small by design, such as a template or a key file, is reported; Skylos cannot know its size.
  • A read whose result goes to a variable first, and only then to json.loads, is reported; only a direct argument counts.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • On the reported line:

    Python
    data = handle.read()  # skylos: ignore[SKY-P401] files are capped at 5 MB on upload
  • [tool.skylos] ignore = ["SKY-P401"] turns the rule off for the project. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.