07a7da
diff --git a/src/mux/muxi.h b/src/mux/muxi.h
07a7da
index 6b57eea..14fd6e2 100644
07a7da
--- a/src/mux/muxi.h
07a7da
+++ b/src/mux/muxi.h
07a7da
07a7da
@@ -14,6 +14,7 @@
07a7da
 #ifndef WEBP_MUX_MUXI_H_
07a7da
 #define WEBP_MUX_MUXI_H_
07a7da
 
07a7da
+#include <assert.h>
07a7da
 #include <stdlib.h>
07a7da
 #include "src/dec/vp8i_dec.h"
07a7da
 #include "src/dec/vp8li_dec.h"
07a7da
@@ -143,13 +144,13 @@
07a7da
 
07a7da
 // Returns size of the chunk including chunk header and padding byte (if any).
07a7da
 static WEBP_INLINE size_t SizeWithPadding(size_t chunk_size) {
07a7da
+  assert(chunk_size <= MAX_CHUNK_PAYLOAD);
07a7da
   return CHUNK_HEADER_SIZE + ((chunk_size + 1) & ~1U);
07a7da
 }
07a7da
 
07a7da
 // Size of a chunk including header and padding.
07a7da
 static WEBP_INLINE size_t ChunkDiskSize(const WebPChunk* chunk) {
07a7da
   const size_t data_size = chunk->data_.size;
07a7da
-  assert(data_size < MAX_CHUNK_PAYLOAD);
07a7da
   return SizeWithPadding(data_size);
07a7da
 }
07a7da
 
07a7da
07a7da
diff --git a/src/mux/muxread.c b/src/mux/muxread.c
07a7da
index eb5070b..ef50dae 100644
07a7da
--- a/src/mux/muxread.c
07a7da
+++ b/src/mux/muxread.c
07a7da
07a7da
@@ -59,6 +59,7 @@
07a7da
   // Sanity checks.
07a7da
   if (data_size < CHUNK_HEADER_SIZE) return WEBP_MUX_NOT_ENOUGH_DATA;
07a7da
   chunk_size = GetLE32(data + TAG_SIZE);
07a7da
+  if (chunk_size > MAX_CHUNK_PAYLOAD) return WEBP_MUX_BAD_DATA;
07a7da
 
07a7da
   {
07a7da
     const size_t chunk_disk_size = SizeWithPadding(chunk_size);
07a7da
@@ -203,9 +204,14 @@
07a7da
     goto Err;  // First chunk should be VP8, VP8L or VP8X.
07a7da
   }
07a7da
 
07a7da
-  riff_size = SizeWithPadding(GetLE32(data + TAG_SIZE));
07a7da
+  riff_size = GetLE32(data + TAG_SIZE);
07a7da
+  if (riff_size > MAX_CHUNK_PAYLOAD) goto Err;
07a7da
+
07a7da
+  // Note this padding is historical and differs from demux.c which does not
07a7da
+  // pad the file size.
07a7da
+  riff_size = SizeWithPadding(riff_size);
07a7da
   if (riff_size < CHUNK_HEADER_SIZE) goto Err;
07a7da
-  if (riff_size > MAX_CHUNK_PAYLOAD || riff_size > size) goto Err;
07a7da
+  if (riff_size > size) goto Err;
07a7da
   // There's no point in reading past the end of the RIFF chunk.
07a7da
   if (size > riff_size + CHUNK_HEADER_SIZE) {
07a7da
     size = riff_size + CHUNK_HEADER_SIZE;
07a7da
07a7da