SKY-P401 · Quality, structure and performance
Potential unbounded read
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
--qualityscan but not listed by default. It is listed, and counted towardmax_quality, with--all-adviceor when the rule is named in[tool.skylos.gate] rules. - Finding type
- advice
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
"""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
"""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
- Runs with
--quality, on Python files. Test code,examplesandbenchmarksfolders, a top-leveldocsfolder and the rootsetup.pyare skipped. - 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, orreadlines(0)reads everything. - Leaves alone
read()on anio.BytesIOorio.StringIOthe file creates itself,ConfigParser.read()(its argument is file names), OpenCVVideoCapture.read(), a read passed straight to a parser that needs the whole value, and a read returned from a function declared to returnstrorbytes. - Reports LOW on the line of the call. The message says whether the source is a file from
open(), azipfilemember 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.
"""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.