diff --git a/taglib/flac/flacfile.cpp b/taglib/flac/flacfile.cpp
index c890a9a5..f0d93bd4 100644
--- a/taglib/flac/flacfile.cpp
+++ b/taglib/flac/flacfile.cpp
@@ -265,12 +265,19 @@ bool FLAC::File::save()
   // Render data for the metadata blocks
 
   ByteVector data;
-  for(const auto &block : std::as_const(d->blocks)) {
-    ByteVector blockData = block->render();
+  for(auto it = d->blocks.begin(); it != d->blocks.end();) {
+    ByteVector blockData = (*it)->render();
     ByteVector blockHeader = ByteVector::fromUInt(blockData.size());
-    blockHeader[0] = block->code();
+    if(blockHeader[0] != 0) {
+      debug("FLAC::File::save() -- Removing too large block.");
+      delete *it;
+      it = d->blocks.erase(it);
+      continue;
+    }
+    blockHeader[0] = (*it)->code();
     data.append(blockHeader);
     data.append(blockData);
+    ++it;
   }
 
   // Compute the amount of padding, and append that to data.
diff --git a/taglib/ogg/flac/oggflacfile.cpp b/taglib/ogg/flac/oggflacfile.cpp
index 58a96956..c283eddf 100644
--- a/taglib/ogg/flac/oggflacfile.cpp
+++ b/taglib/ogg/flac/oggflacfile.cpp
@@ -113,6 +113,17 @@ bool Ogg::FLAC::File::save()
   // Put the size in the first 32 bit (I assume no more than 24 bit are used)
 
   ByteVector v = ByteVector::fromUInt(d->xiphCommentData.size());
+  if(v[0] != 0) {
+    // Block size uses more than 24 bits, try again with pictures removed.
+    d->comment->removeAllPictures();
+    d->xiphCommentData = d->comment->render(false);
+    v = ByteVector::fromUInt(d->xiphCommentData.size());
+    if(v[0] != 0) {
+      debug("Ogg::FLAC::File::save() -- Invalid, metadata block is too large.");
+      return false;
+    }
+    debug("Ogg::FLAC::File::save() -- Metadata block is too large, pictures removed.");
+  }
 
   // Set the type of the metadata-block to be a Xiph / Vorbis comment