Untrusted input
An upload is the most common way to give a program a file it did not choose. A hostile one can say in its header that it is 15 000 by 15 000 pixels and have nothing behind it, hoping the decoder allocates 675 MB before it finds out. libstb decides from the header, before any pixel memory exists, and every kind of failure is a different exception, so a bad upload is something your code can name.
import libstb as stb
with open("chelsea.png", "rb") as f:
png = f.read()
def attempt(what, data, **options):
"""Decodes `data` and says what happened: every kind of failure is an exception you can name."""
try:
img = stb.Image.decode(data, **options)
print(f"{what}: decoded {img.width}x{img.height}")
except stb.LimitError:
print(f"{what}: limit error")
except stb.DecodeError:
print(f"{what}: decode error")
# A whole file in memory: the format is found from the bytes, not from a name.
attempt("a real png", png)
# The header alone: the size and the channels, with no pixel memory.
info = stb.iminfo(png)
print(f"the header says {info.width}x{info.height}, {info.channels} channels")
# The limit counts the bytes the pixels would need, and it is checked from the header, before any pixel memory exists.
print("default limit:", stb.DEFAULT_MAX_BYTES >> 20, "MiB")
attempt("1 MiB limit", png, max_bytes=1 << 20)
attempt("1 KiB limit", png, max_bytes=1 << 10)
# What is not an image, or not all of one.
attempt("garbage", bytes(100))
attempt("half a png", png[: len(png) // 2])
try:
stb.Image.open("no/such/file.png")
except OSError:
print("missing file: io error")
a real png: decoded 451x300
the header says 451x300, 3 channels
default limit: 512 MiB
1 MiB limit: decoded 451x300
1 KiB limit: limit error
garbage: decode error
half a png: decode error
missing file: io error
The limit
Every decode has a limit on the bytes the pixels would need, 512 MiB by default. The size comes from the header, so a picture over the limit is refused without being decoded or allocated. You set it per call:
stb.Image.open(path, max_bytes=...), stb.Image.decode(data, max_bytes=...) and stb.imread(source, max_bytes=...).
Decide it from what you are willing to hold in memory. The example's real picture needs 451 by 300 by 3 bytes, about 396 KiB, so a limit of 1 MiB accepts it and a limit of 1 KiB does not.
The errors
| What happened | Python | C++ |
|---|---|---|
| Over the size limit | stb.LimitError | stb::limit_error |
| Not an image, or not all of one | stb.DecodeError | stb::decode_error |
| The file cannot be read | OSError (FileNotFoundError) | stb::io_error |
| The picture cannot be written | stb.EncodeError | stb::encode_error |
| A call that makes no sense, such as a crop outside the image | ValueError | std::invalid_argument |
stb.Error, so one except stb.Error catches everything that came from the data.
Fonts have the same rule: the font reader checks its bounds, but it has not been fuzzed, so only load fonts you trust.
The same bound exists for compressed data: a zip bomb is a limit_error, not an out-of-memory. The limits are in stb/zlib.hpp.
API at a glance
| Header without decoding | stb.iminfo(data) |
|---|---|
| Decode from memory with a limit | stb.Image.decode(data, max_bytes=n) |
| The default limit | stb.DEFAULT_MAX_BYTES |
| Catch everything about the data | except stb.Error |