{"record":{"id":"deb30f71b9f30792","repo":"alibaba/Sentinel","slug":"bufsize-must-between-0-max-size-but-bufsi-deb30f","errorCode":null,"errorMessage":"bufSize must between (0, ${MAX_SIZE}], but ${bufSize} get","messagePattern":"bufSize must between \\(0, (.+?)\\], but (.+?) get","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileRefreshableDataSource.java","lineNumber":81,"sourceCode":"    public FileRefreshableDataSource(String fileName, Converter<String, T> configParser) throws FileNotFoundException {\n        this(new File(fileName), configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, DEFAULT_CHAR_SET);\n    }\n\n    public FileRefreshableDataSource(File file, Converter<String, T> configParser, int bufSize)\n        throws FileNotFoundException {\n        this(file, configParser, DEFAULT_REFRESH_MS, bufSize, DEFAULT_CHAR_SET);\n    }\n\n    public FileRefreshableDataSource(File file, Converter<String, T> configParser, Charset charset)\n        throws FileNotFoundException {\n        this(file, configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, charset);\n    }\n\n    public FileRefreshableDataSource(File file, Converter<String, T> configParser, long recommendRefreshMs, int bufSize,\n                                     Charset charset) throws FileNotFoundException {\n        super(configParser, recommendRefreshMs);\n        if (bufSize <= 0 || bufSize > MAX_SIZE) {\n            throw new IllegalArgumentException(\"bufSize must between (0, \" + MAX_SIZE + \"], but \" + bufSize + \" get\");\n        }\n        if (file == null || file.isDirectory()) {\n            throw new IllegalArgumentException(\"File can't be null or a directory\");\n        }\n        if (charset == null) {\n            throw new IllegalArgumentException(\"charset can't be null\");\n        }\n        this.buf = new byte[bufSize];\n        this.file = file;\n        this.charset = charset;\n        // If the file does not exist, the last modified will be 0.\n        this.lastModified = file.lastModified();\n        firstLoad();\n    }\n\n    private void firstLoad() {\n        try {\n            T newValue = loadConfig();","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileRefreshableDataSource.java#L63-L99","documentation":"FileRefreshableDataSource reads a whole config file into a fixed byte buffer (bufSize) on each refresh. Its constructor validates 0 < bufSize <= MAX_SIZE and throws IllegalArgumentException otherwise — same policy as FileInJarReadableDataSource, applied before the file/charset checks.","triggerScenarios":"new FileRefreshableDataSource(file, parser, recommendRefreshMs, bufSize, charset) with bufSize <= 0 or bufSize > MAX_SIZE; also reachable via the 5-arg overload from the 4-arg convenience constructor only if you pass an explicit bad size (defaults are safe).","commonSituations":"bufSize derived from a runtime computation (e.g. file length check order) or a config property defaulting to 0; migrating from a version where the argument order/semantics of the varargs constructor changed.","solutions":["Pass a bufSize in (0, MAX_SIZE]; if unsure, omit it and rely on DEFAULT_BUF_SIZE.","Audit the expression producing bufSize — ensure it cannot be 0 or negative before the constructor call.","Remember this datasource reads the file in one shot: bufSize must cover the full config file or later reads will fail a size check like the jar variant."],"exampleFix":"// before\nnew FileRefreshableDataSource<>(file, parser, 3000, Integer.parseInt(props.getProperty(\"buf\", \"0\")), charset);\n\n// after\nint bufSize = Integer.parseInt(props.getProperty(\"buf\", String.valueOf(FileRefreshableDataSource.DEFAULT_BUF_SIZE)));\nnew FileRefreshableDataSource<>(file, parser, 3000, bufSize, charset);","handlingStrategy":"validation","validationCode":"int bufSize = props.getProperty(\"bufSize\") == null\n    ? FileRefreshableDataSource.DEFAULT_BUF_SIZE\n    : Integer.parseInt(props.getProperty(\"bufSize\"));\nif (bufSize <= 0 || bufSize > FileRefreshableDataSource.DEFAULT_BUF_SIZE /* MAX_SIZE */) {\n    throw new ConfigurationException(\"bufSize out of range: \" + bufSize);\n}\nnew FileRefreshableDataSource<>(file, parser, refreshMs, bufSize, charset);","typeGuard":"boolean isValidBufSize(int bufSize) {\n    return bufSize > 0 && bufSize <= FileRefreshableDataSource.DEFAULT_BUF_SIZE;\n}","tryCatchPattern":null,"preventionTips":["Size bufSize to the maximum expected file size — the whole file is read in one buffer.","Omit the bufSize argument unless you have measured constraints; defaults are safe."],"tags":["sentinel","datasource","file","buffer","validation"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}