Yalantis/uCrop · error · CImgArgumentException

CImgList<%s>::get_unserialize(): Specified serialized buffer

Error message

CImgList<%s>::get_unserialize(): Specified serialized buffer is (null).

What it means

CImgList::get_unserialize() was called with an empty buffer (CImg buffer is_empty()), so there is no data to deserialize at all. CImg rejects this up front with CImgArgumentException rather than attempting to parse a null stream.

Source

Thrown at ucrop/src/main/jni/CImg.h:68846

          if (W*H*D*C>0) { \
            CImg<Tss> raw; \
            CImg<T> &img = res._data[l]; \
            if (err==5) _cimgz_unserialize_case(Tss) \
            else { \
              raw.assign(W,H,D,C); \
              CImg<ucharT> _raw((unsigned char*)raw._data,W*sizeof(Tss),H,D,C,true); \
              if (sizeof(t)==1) { std::memcpy(_raw,stream,_raw.size()); stream+=_raw.size(); } \
              else cimg_for(_raw,p,unsigned char) *p = (unsigned char)*(stream++); \
            } \
            if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \
            raw.move_to(img); \
          } \
        } \
        loaded = true; \
      }

      if (buffer.is_empty())
        throw CImgArgumentException("CImgList<%s>::get_unserialize(): Specified serialized buffer is (null).",
                                    pixel_type());
      CImgList<T> res;
      const t *stream = buffer._data + header_size, *const estream = buffer._data + buffer.size();
      bool loaded = false, endian = cimg::endianness(), is_bytef = false;
      CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
      *tmp = *str_pixeltype = *str_endian = 0;
      unsigned int j, N = 0, W, H, D, C;
      uint64T csiz;
      int i, err;
      cimg::unused(is_bytef);
      do {
        j = 0; while ((i=(int)*stream)!='\n' && stream<estream && j<255) { ++stream; tmp[j++] = (char)i; }
        ++stream; tmp[j] = 0;
      } while (*tmp=='#' && stream<estream);
      err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z123468_]%*c%255[sA-Za-z_ ]",
                        &N,str_pixeltype._data,str_endian._data);
      if (err<2)
        throw CImgArgumentException("CImgList<%s>::get_unserialize(): CImg header not found in serialized buffer.",

View on GitHub (pinned to f788b534b4)

Solutions

  1. Check buffer.is_empty() (or file size > 0) before calling unserialize.
  2. Fix the upstream load/read that produced the empty buffer — verify the source file exists and is non-empty.
  3. On Android, confirm the asset/resource was actually bundled and opened successfully before deserializing.

Example fix

// before
CImgList<unsigned char> imgs;
imgs.unserialize(raw); // throws if raw is empty
// after
if (raw.is_empty()) throw std::runtime_error("serialized buffer is empty");
CImgList<unsigned char> imgs;
imgs.unserialize(raw);
Defensive patterns

Strategy: validation

Validate before calling

if (raw.is_empty()) {
  throw std::runtime_error("cannot unserialize: buffer is empty");
}

Type guard

template <typename t>
bool nonEmptyBuffer(const CImg<t>& b) { return !b.is_empty(); }

Try / catch

try {
  imgs.unserialize(raw);
} catch (CImgArgumentException& e) {
  cimg::warn("empty buffer passed to unserialize: %s", e.what());
}

Prevention

When it happens

Trigger: Calling unserialize()/get_unserialize() with a default-constructed CImg<t> buffer, a failed load that produced an empty buffer, or a zero-length file read passed straight into unserialize.

Common situations: Reading an input file that came back empty (missing resource in APK/assets, failed download saved as 0 bytes); forgetting to check the result of a previous load before re-serializing.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/c746779e9f402f85. Report an issue: GitHub.