{"record":{"id":"65e9d0287339b21a","repo":"Tencent/matrix","slug":"sizes-should-not-be-negative-and-maxsize-should-be","errorCode":null,"errorMessage":"sizes should not be negative and maxSize should be 0 or greater than minSize: min = <mMinTraceSize>, max = <mMaxTraceSize>","messagePattern":"sizes should not be negative and maxSize should be 0 or greater than minSize: min = <mMinTraceSize>, max = <mMaxTraceSize>","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-hooks/src/main/java/com/tencent/matrix/hook/memory/MemoryHook.java","lineNumber":140,"sourceCode":"                .addHook(this)\n                .commitHooks();\n    }\n\n    @NonNull\n    @Override\n    protected String getNativeLibraryName() {\n        return \"matrix-memoryhook\";\n    }\n\n    @Override\n    public boolean onConfigure() {\n        if (mMemGuardInstalled) {\n            MatrixLog.w(TAG, \"MemGuard has been installed, skip MemoryHook install logic.\");\n            return false;\n        }\n\n        if (mMinTraceSize < 0 || (mMaxTraceSize != 0 && mMaxTraceSize < mMinTraceSize)) {\n            throw new IllegalArgumentException(\"sizes should not be negative and maxSize should be \" +\n                    \"0 or greater than minSize: min = \" + mMinTraceSize + \", max = \" + mMaxTraceSize);\n        }\n\n        MatrixLog.d(TAG, \"enable mmap? \" + mEnableMmap);\n        enableMmapHookNative(mEnableMmap);\n\n        setTracingAllocSizeRangeNative(mMinTraceSize, mMaxTraceSize);\n        setStacktraceLogThresholdNative(mStacktraceLogThreshold);\n        enableStacktraceNative(mEnableStacktrace);\n\n        return true;\n    }\n\n    @Override\n    protected boolean onHook(boolean enableDebug) {\n        if (!mHookInstalled) {\n            installHooksNative(mHookSoSet.toArray(new String[0]), mIgnoreSoSet.toArray(new String[0]), enableDebug);\n            mHookInstalled = true;","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-hooks/src/main/java/com/tencent/matrix/hook/memory/MemoryHook.java#L122-L158","documentation":"MemoryHook.onConfigure() validates the trace-size configuration before installing the memory hook. It throws IllegalArgumentException when mMinTraceSize is negative, or when mMaxTraceSize is non-zero but smaller than mMinTraceSize. A max of 0 is treated as 'unlimited', so only a positive max below min is rejected.","triggerScenarios":"Building/configuring a MemoryHook (or the corresponding MatrixBuilder parameter) with setTraceSize / min/max trace size values where min < 0, or max != 0 && max < min, before calling install/commit.","commonSituations":"Passing a size in KB where the API expects bytes (yielding a tiny or negative value after unit conversion); copying sample config with max left smaller than a customized min; computing sizes dynamically from device memory and producing a negative number on failure; typo swapping min and max arguments.","solutions":["Read the message values: it prints the actual mMinTraceSize and mMaxTraceSize that failed validation — fix those inputs.","Ensure min trace size is >= 0 and, if a max is set, max >= min (max = 0 means unlimited).","Check for unit conversion bugs (KB vs bytes) or swapped min/max arguments at the configuration call site.","Clamp dynamic/derived sizes defensively before passing them to the hook builder."],"exampleFix":"// before\nmemoryHook.setTraceSize(-1024, 512); // min negative\n// after\nint min = Math.max(0, computedMin);\nint max = (computedMax == 0) ? 0 : Math.max(min, computedMax);\nmemoryHook.setTraceSize(min, max);","handlingStrategy":"validation","validationCode":"public static int[] safeTraceSizes(int min, int max) {\n    if (min < 0) min = 0;\n    if (max != 0 && max < min) max = (min == 0) ? 0 : min;\n    return new int[]{min, max};\n}","typeGuard":null,"tryCatchPattern":"try {\n    memoryHook.onConfigure();\n} catch (IllegalArgumentException e) {\n    MatrixLog.e(TAG, \"invalid trace sizes, using defaults\", e);\n    memoryHook.setTraceSize(DEFAULT_MIN, DEFAULT_MAX);\n}","preventionTips":["Validate min >= 0 and (max == 0 || max >= min) at your config layer.","Watch unit conversions (KB vs bytes) when computing sizes.","Clamp dynamically derived sizes before passing them to the builder.","Double-check argument order when passing (min, max)."],"tags":["configuration","validation","memory-hook","illegal-argument"],"backgroundTag":"argument-out-of-range","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}