Commit ed43f660 for libheif

commit ed43f6606b61682104a463c2a5be02b8ffe6d26a
Author: Dirk Farin <dirk.farin@gmail.com>
Date:   Thu Aug 27 02:53:12 2026 +0200

    refuse to write contexts without a brand or read from a file; fix null mini box push

    - A context holding only metadata items (no image, no sequence) has no brand.
      compute_compatible_brands() returned 0 and an all-zero 16-byte ftyp was written
      that no reader, libheif included, accepts. HeifContext::write() now returns a
      Usage_error for such a context.
    - parse_heif_file() pushed m_mini_box into m_top_level_boxes even when the file
      has no mini box, so writing a context that was read from a file crashed in
      derive_box_versions().
    - With that crash gone, writing a loaded context produced a truncated file: the
      item and sample data are never copied out of the input. Until that is
      implemented, HeifContext::write() refuses a context with an input stream
      (Unsupported_feature) instead of silently writing a broken file.

diff --git a/libheif/context.cc b/libheif/context.cc
index 9208c2f7..3b3e1c10 100644
--- a/libheif/context.cc
+++ b/libheif/context.cc
@@ -391,6 +391,15 @@ static uint64_t rescale(uint64_t duration, uint32_t old_base, uint32_t new_base)

 Error HeifContext::write(StreamWriter& writer)
 {
+  // Writing is only implemented for contexts that were built in memory. In a context read
+  // from a file, the item and sample data still live in the input file and would not be
+  // copied to the output: the result would be a truncated file that no reader accepts.
+  if (m_heif_file->get_reader()) {
+    return Error(heif_error_Unsupported_feature,
+                 heif_suberror_Unspecified,
+                 "Writing a context that was read from a file is not supported");
+  }
+
   // --- finalize some parameters

   uint64_t max_sequence_duration = 0;
@@ -510,6 +519,15 @@ Error HeifContext::write(StreamWriter& writer)
     ftyp->set_major_brand(main_brand);
   }

+  // A file without images and without an image sequence (e.g. only metadata items) has no
+  // brand that describes it. Instead of writing a file with an all-zero 'ftyp' box that no
+  // reader (including libheif) accepts, refuse to write it.
+  if (ftyp->get_major_brand() == 0) {
+    return Error(heif_error_Usage_error,
+                 heif_suberror_Unspecified,
+                 "Cannot write a file that contains neither images nor an image sequence");
+  }
+
   ftyp->set_minor_version(0);
   for (auto brand : compatible_brands) {
     ftyp->add_compatible_brand(brand);
diff --git a/libheif/file.cc b/libheif/file.cc
index 1a495b45..02b449c4 100644
--- a/libheif/file.cc
+++ b/libheif/file.cc
@@ -500,9 +500,10 @@ Error HeifFile::parse_heif_file()
       }

   m_mini_box = m_file_layout->get_mini_box();
-  m_top_level_boxes.push_back(m_mini_box);

   if (m_mini_box) {
+    m_top_level_boxes.push_back(m_mini_box);
+
     Error err = m_mini_box->create_expanded_boxes(this);
     if (err) {
       return err;