Crash with vector::_M_default_append on a fully transparent input

Open
#29 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp

Research direction

Start in src/image.cpp at the trim bounds around lines 391-394 and the width/height calculation around lines 508-509; inspect include/mb/flex.h:26 to understand the resize failure. Reproduce with two RGBA TIFFs, including one with an all-zero alpha channel. Done means fully transparent inputs no longer trigger undefined bounds or an escaping length_error, and the input is either skipped or rejected with its filename.

Written by the indexing model from the issue text.

Description

Reproduction

Two RGBA TIFFs, the second with an alpha channel that is zero everywhere. Under a megabyte in
total, no flags needed:

$ multiblend -o out.tif big.tif zeroalpha.tif
Multiblend v2.0.0 (c) 2021 David Horman
Error: vector::_M_default_append

Generator:

import numpy as np, tifffile

def write(name, alpha):
    a = np.zeros((900, 900, 4), np.uint16)
    a[..., :3] = 30000
    a[..., 3] = alpha
    tifffile.imwrite(name, a, photometric="rgb",
                     extrasamples="unassalpha", compression=None)

write("big.tif", 65535)
write("zeroalpha.tif", 0)

It reproduces with an explicit --levels 20 and with the automatic level count, so the pyramid
depth is not involved.

Where it comes from

vector::_M_default_append is libstdc++'s std::vector::resize raising std::length_error, so
the size is wrong rather than the memory unavailable.

In src/image.cpp, the trim block declares its bounds uninitialised:

391:  int top;
392:  int left;
393:  int bottom;
394:  int right;

and every assignment to them happens inside an if (alpha == 0xffff) branch. On an image whose
alpha is zero everywhere no branch is taken, so

508:    width_  = right  + 1 - left;
509:    height_ = bottom + 1 - top;

reads uninitialised locals. Flex(width, height) then does rows_.resize(height_)
(include/mb/flex.h:26) with an int that can be negative, which converts to a huge size_t and
throws.

So the visible length_error is the symptom; the defect is undefined behaviour, and the result
depends on what the stack happened to hold. That matches what we saw in practice: in one run a
band of twelve images failed while the band before it, with the same twelve, had succeeded.

Why fully transparent inputs turn up

nona (hugin) writes them for tiles whose geometry only grazes the region being drawn: the file
has the right dimensions and its alpha is zero throughout. Any pipeline that crops remapped
intermediates per band produces them routinely.

Impact

Ours is a panorama pipeline. A transparent tile reached the blender, multiblend exited 1, and the
caller's fallback reduced the canvas twice — to 84%, then 75% — because everything pointed at
memory. Peak resident on the previous band was 1.4 GB against a 6.4 GB budget with 51.9 GB of disk
free, so memory was never the constraint. A 356-megapixel panorama came out at 200. The wrong
diagnosis was the expensive part.

Suggested behaviour

Skip an input whose alpha is zero everywhere, or refuse it with a message naming the file. Either
beats a length_error escaping from inside a container. Initialising the four bounds and
detecting "no opaque pixel found" would fix the undefined behaviour as well as the message.

Version

Multiblend v2.0.0, built from this repository on 19 August 2026 (identifiable as this fork
rather than the 2.0rc5 tarball because it links libspdlog). Current main here is 8d470cb
(2025‑02‑20), and nothing newer exists on any branch, so the binary is at that commit.

I could not rebuild main locally to re-confirm — the build wants BS_thread_pool.hpp, which
comes through vcpkg and is not on this machine — so the reproduction above is from that binary, and
the code reading is of current main. Both point at the same four lines.

Dominant language
C++
Stars
3
Forks
2
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.