changeset 29553:87eff21d2609

ignore spurious error from zlib (bug #60450) * ls-mat5.cc (read_mat5_binary_element): Use uncompress2 instead of uncompress so we can track the number of bytes processed in addition to the number of bytes of uncompressed data produced. If all input has been processed and the expected number of bytes of uncompressed data have been generated, then don't fail if uncompress2 returns a status code of Z_BUF_ERROR.
author John W. Eaton <jwe@octave.org>
date Sat, 24 Apr 2021 08:29:22 -0400
parents 33556123b892
children 0f57f0f47216
files libinterp/corefcn/ls-mat5.cc
diffstat 1 files changed, 16 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/ls-mat5.cc	Fri Apr 23 19:30:43 2021 +0200
+++ b/libinterp/corefcn/ls-mat5.cc	Sat Apr 24 08:29:22 2021 -0400
@@ -529,9 +529,10 @@
       // We uncompress the first 8 bytes of the header to get the buffer length
       // This will fail with an error Z_MEM_ERROR
       uLongf destLen = 8;
+      uLongf elt_len = element_length;
       OCTAVE_LOCAL_BUFFER (unsigned int, tmp, 2);
-      if (uncompress (reinterpret_cast<Bytef *> (tmp), &destLen,
-                      reinterpret_cast<Bytef *> (inbuf), element_length)
+      if (uncompress2 (reinterpret_cast<Bytef *> (tmp), &destLen,
+                       reinterpret_cast<Bytef *> (inbuf), &elt_len)
           == Z_MEM_ERROR)
         error ("load: error probing size of compressed data element");
 
@@ -544,10 +545,19 @@
 
       // FIXME: find a way to avoid casting away const here!
 
-      int err = uncompress (reinterpret_cast<Bytef *>
-                            (const_cast<char *> (outbuf.c_str ())),
-                            &destLen, reinterpret_cast<Bytef *> (inbuf),
-                            element_length);
+      elt_len = element_length;
+      int err = uncompress2 (reinterpret_cast<Bytef *>
+                             (const_cast<char *> (outbuf.c_str ())),
+                             &destLen, reinterpret_cast<Bytef *> (inbuf),
+                             &elt_len);
+
+      // Ignore buffer error if we have consumed all the input buffer
+      // and uncompressing the data generated as many bytes of output as
+      // we were expecting given the data element size that was stored
+      // in the Matlab data element header.
+      if (err == Z_BUF_ERROR && destLen == tmp[1] + 8
+          && elt_len == static_cast<uLongf> (element_length))
+        err = Z_OK;
 
       if (err != Z_OK)
         {