{"record":{"id":"5d640d60ef504612","repo":"Yalantis/uCrop","slug":"cimglist-s-unserialize-invalid-specified-siz","errorCode":null,"errorMessage":"CImgList<%s>::unserialize(): Invalid specified size (%u,%u,%u,%u) for image #%u in serialized buffer.","messagePattern":"CImgList<(.+?)>::unserialize\\(\\): Invalid specified size \\(%u,%u,%u,%u\\) for image #%u in serialized buffer\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":68825,"sourceCode":"        if (!is_bytef) delete[] cbuf; \\\n      }\n#else\n#define _cimgz_unserialize_case(Tss) \\\n      throw CImgArgumentException(\"CImgList<%s>::get_unserialize(): Unable to unserialize compressed data \" \\\n                                  \"unless zlib is enabled.\", \\\n                                  pixel_type());\n#endif\n\n#define _cimg_unserialize_case(Ts1,Ts2,Ts3,Tss) \\\n      if (!loaded && ((Ts1 && !cimg::strcasecmp(Ts1,str_pixeltype)) || \\\n                      (Ts2 && !cimg::strcasecmp(Ts2,str_pixeltype)) || \\\n                      (Ts3 && !cimg::strcasecmp(Ts3,str_pixeltype)))) { \\\n        for (unsigned int l = 0; l<N; ++l) { \\\n          j = 0; while ((i=(int)*stream)!='\\n' && stream<estream && j<255) { ++stream; tmp[j++] = (char)i; } \\\n          ++stream; tmp[j] = 0; \\\n          W = H = D = C = 0; csiz = 0; \\\n          if ((err = cimg_sscanf(tmp,\"%u %u %u %u #\" cimg_fuint64,&W,&H,&D,&C,&csiz))<4) \\\n            throw CImgArgumentException(\"CImgList<%s>::unserialize(): Invalid specified size (%u,%u,%u,%u) for \" \\\n                                        \"image #%u in serialized buffer.\", \\\n                                        pixel_type(),W,H,D,C,l); \\\n          if (W*H*D*C>0) { \\\n            CImg<Tss> raw; \\\n            CImg<T> &img = res._data[l]; \\\n            if (err==5) _cimgz_unserialize_case(Tss) \\\n            else { \\\n              raw.assign(W,H,D,C); \\\n              CImg<ucharT> _raw((unsigned char*)raw._data,W*sizeof(Tss),H,D,C,true); \\\n              if (sizeof(t)==1) { std::memcpy(_raw,stream,_raw.size()); stream+=_raw.size(); } \\\n              else cimg_for(_raw,p,unsigned char) *p = (unsigned char)*(stream++); \\\n            } \\\n            if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \\\n            raw.move_to(img); \\\n          } \\\n        } \\\n        loaded = true; \\\n      }","sourceCodeStart":68807,"sourceCodeEnd":68843,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L68807-L68843","documentation":"While parsing a per-image header line ('W H D C #csiz') inside CImgList::unserialize(), cimg_sscanf extracted fewer than 4 unsigned dimensions, meaning the header line does not contain a valid W/H/D/C tuple. CImg throws CImgArgumentException naming the offending image index and the parsed (garbage) dimensions.","triggerScenarios":"Passing a corrupted, truncated, or wrongly-formatted buffer to CImgList::load()/unserialize() where the header line for image #l is missing dimensions or contains non-numeric text (e.g. an HTML error page, a text file, or a partially written save).","commonSituations":"File truncated by an interrupted save; loading a plain-text or non-CImg file into unserialize(); version mismatch where the .cimg header layout changed; transferring binary files in text mode corrupting newline/dimension lines.","solutions":["Regenerate the serialized file with a matching CImg version; verify it round-trips (save then load).","Check the file for truncation/corruption — compare byte size or checksum against the producer.","Ensure binary-safe transfer (ftp binary mode, no text-mode conversion).","Confirm you are calling unserialize() on a CImg-format buffer, not an arbitrary file."],"exampleFix":"// before\nCImgList<float> imgs;\nimgs.load(\"possibly_truncated.cimg\");\n// after\nstd::ifstream f(\"possibly_truncated.cimg\", std::ios::binary|std::ios::ate);\nif (f.tellg() < 16) throw std::runtime_error(\"file too small to be valid .cimg\");\nCImgList<float> imgs;\nimgs.load(\"possibly_truncated.cimg\");","handlingStrategy":"validation","validationCode":"// Validate the buffer looks like .cimg before loading\nstd::ifstream f(path, std::ios::binary);\nchar line[256]; f.getline(line, 256);\nunsigned N, W, H, D, C;\nif (std::sscanf(line, \"%u %u %u %u\", &W, &H, &D, &C) < 1)\n  throw std::runtime_error(\"not a valid CImg serialized buffer\");","typeGuard":null,"tryCatchPattern":"try {\n  imgs.unserialize(raw);\n} catch (CImgArgumentException& e) {\n  cimg::warn(\"corrupt serialized buffer: %s\", e.what());\n  // discard / re-request the data\n}","preventionTips":["Verify checksums/size of serialized files after transfer.","Use binary-safe transfer channels.","Round-trip test (save then load) in CI.","Never feed non-CImg files to unserialize()."],"tags":["serialization","corrupt-data","parsing","native"],"backgroundTag":"schema-validation-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}