{"record":{"id":"6254063b4a1e1819","repo":"Yalantis/uCrop","slug":"cimg-mod-specified-modulo-value-is-0","errorCode":null,"errorMessage":"cimg::mod(): Specified modulo value is 0.","messagePattern":"cimg::mod\\(\\): Specified modulo value is 0\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":6596,"sourceCode":"    //! Return the nearest power of 2 higher than given value.\n    template<typename T>\n    inline cimg_uint64 nearest_pow2(const T& x) {\n      cimg_uint64 i = 1;\n      while (x>i) i<<=1;\n      return i;\n    }\n\n    //! Return the modulo of a value.\n    /**\n       \\param x Input value.\n       \\param m Modulo value.\n       \\note This modulo function accepts negative and floating-points modulo numbers, as well as variables of any type.\n    **/\n    template<typename T>\n    inline T mod(const T& x, const T& m) {\n      if (!m) {\n        if (cimg::type<T>::is_float()) return cimg::type<T>::nan();\n        else throw CImgArgumentException(\"cimg::mod(): Specified modulo value is 0.\");\n      }\n      const double dx = (double)x, dm = (double)m;\n      if (!cimg::type<double>::is_finite(dm)) return x;\n      if (cimg::type<double>::is_finite(dx)) return (T)(dx - dm * std::floor(dx / dm));\n      return (T)0;\n    }\n    inline int mod(const bool x, const bool m) {\n      if (!m) throw CImgArgumentException(\"cimg::mod(): Specified modulo value is 0.\");\n      return x?1:0;\n    }\n    inline int mod(const unsigned char x, const unsigned char m) {\n      if (!m) throw CImgArgumentException(\"cimg::mod(): Specified modulo value is 0.\");\n      return x%m;\n    }\n    inline int mod(const char x, const char m) {\n      if (!m) throw CImgArgumentException(\"cimg::mod(): Specified modulo value is 0.\");\n#if defined(CHAR_MAX) && CHAR_MAX==255\n      return x%m;","sourceCodeStart":6578,"sourceCodeEnd":6614,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L6578-L6614","documentation":"CImg's cimg::mod(x, m) computes x modulo m; for non-floating-point types a zero modulus is invalid, so it throws CImgArgumentException('cimg::mod(): Specified modulo value is 0.'). For float/double it returns NaN instead of throwing. This guards against a degenerate modulo (division by zero) inside image arithmetic.","triggerScenarios":"Calling cimg::mod(x, 0) or mod(x, m) where m is a computed expression that evaluated to 0 (e.g. an image pixel value, a divisor from user data, or an uninitialized variable), with an integer/char/short/int type.","commonSituations":"Dividing/masking image coordinates by a value taken from image metadata or user input that is 0; template code where T is instantiated as an integer type while the caller assumed float NaN semantics; loop bounds computed as width/height of an empty image (0).","solutions":["Validate the modulus before calling: if (m == 0) handle/skip, otherwise call cimg::mod(x, m).","Use floating-point types (float/double) if NaN-on-zero is acceptable semantics for your code.","Ensure upstream values (image width/height, user input) are nonzero before they reach mod().","Replace the call with an explicit branch: m != 0 ? cimg::mod(x, m) : fallbackValue."],"exampleFix":"// before\nconst int r = cimg::mod(offset, step); // step may be 0\n// after\nif (step == 0) throw std::invalid_argument(\"step must be non-zero\");\nconst int r = cimg::mod(offset, step);","handlingStrategy":"validation","validationCode":"if (m == T(0)) {\n    throw std::invalid_argument(\"cimg::mod: modulo value must be non-zero\");\n}\nconst T r = cimg::mod(x, m);","typeGuard":"template<typename T>\nbool is_valid_modulus(const T& m) {\n    return m != T(0);\n}","tryCatchPattern":"try {\n    result = cimg::mod(x, m);\n} catch (cimg_library::CImgArgumentException& e) {\n    std::fprintf(stderr, \"Zero modulus: %s\\n\", e.what());\n    result = fallback_value;\n}","preventionTips":["Validate computed moduli (widths, steps, user input) for non-zero before use.","Remember the throwing behavior applies to integer types; floats/doubles return NaN instead.","Check dimensions of empty images — width/height of 0 can become a zero modulus downstream.","Centralize modulo usage in a helper that enforces the non-zero precondition."],"tags":["cimg","cpp","modulo","division-by-zero","illegal-argument"],"backgroundTag":"invalid-argument-value","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"}