{"record":{"id":"5b959679e9fd3553","repo":"pinpoint-apm/pinpoint","slug":"negative-buffersize","errorCode":null,"errorMessage":"negative bufferSize","messagePattern":"negative bufferSize","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"commons/src/main/java/com/navercorp/pinpoint/common/util/IOUtils.java","lineNumber":52,"sourceCode":"    private static final int MAX_BUFFER_SIZE = 1024 * 1024;\n\n    public static final int EOF = -1;\n\n    private IOUtils() {\n    }\n\n    public static byte[] toByteArray(final InputStream inputStream) throws IOException {\n        return toByteArray(inputStream, DEFAULT_BUFFER_SIZE, true);\n    }\n\n    public static byte[] toByteArray(final InputStream inputStream, boolean close) throws IOException {\n        return toByteArray(inputStream, DEFAULT_BUFFER_SIZE, close);\n    }\n\n    public static byte[] toByteArray(final InputStream inputStream, int bufferSize, boolean close) throws IOException {\n        Objects.requireNonNull(inputStream, \"inputStream\");\n        if (bufferSize < 0) {\n            throw new IllegalArgumentException(\"negative bufferSize\");\n        }\n\n        bufferSize = calculateBufferSize(inputStream, bufferSize);\n        final byte[] buffer = new byte[bufferSize];\n\n        final byte[] readBuffer = bufferRead(inputStream, buffer);\n        if (readBuffer != null) {\n            return readBuffer;\n        }\n\n        ByteArrayOutputStream outputStream;\n        try {\n            outputStream = new ByteArrayOutputStream(buffer.length * 2);\n            outputStream.write(buffer, 0, buffer.length);\n\n            copy(inputStream, outputStream, buffer);\n\n            outputStream.flush();","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/pinpoint-apm/pinpoint/blob/744c3d3075e595656abb1ae331ad2c0e4c9eb996/commons/src/main/java/com/navercorp/pinpoint/common/util/IOUtils.java#L34-L70","documentation":"IOUtils.toByteArray copies an InputStream into a byte array using a caller-supplied buffer size. A negative bufferSize would make 'new byte[bufferSize]' throw NegativeArraySizeException with a confusing stack trace, so the method throws a clear IllegalArgumentException instead.","triggerScenarios":"Calling IOUtils.toByteArray(inputStream, bufferSize, close) with bufferSize < 0, e.g. toByteArray(in, -1, true) or a bufferSize computed from a config/constant that is negative.","commonSituations":"Misconfigured buffer-size settings (negative value in properties/env), subtraction-based size computations that underflow, or copy-paste of the 3-arg overload expecting 0 to mean default.","solutions":["Pass a positive buffer size (or use the 1/2-arg overloads that apply DEFAULT_BUFFER_SIZE).","Clamp the computed size: Math.max(bufferSize, DEFAULT_BUFFER_SIZE) or validate config >= 0 at load time.","Fix the source of the negative value (config parsing or arithmetic underflow)."],"exampleFix":"// before\nbyte[] data = IOUtils.toByteArray(in, -1, true);\n// after\nint size = Math.max(configuredSize, IOUtils.DEFAULT_BUFFER_SIZE);\nbyte[] data = IOUtils.toByteArray(in, size, true);","handlingStrategy":"validation","validationCode":"if (bufferSize < 0) {\n    throw new IllegalArgumentException(\"bufferSize must be >= 0: \" + bufferSize);\n}","typeGuard":"boolean isValidBufferSize(int n) { return n >= 0; }","tryCatchPattern":"try {\n    return IOUtils.toByteArray(in, bufferSize, true);\n} catch (IllegalArgumentException e) {\n    log.warn(\"Bad buffer size {}, falling back to default\", bufferSize);\n    return IOUtils.toByteArray(in);\n}","preventionTips":["Clamp configured buffer sizes to a positive minimum at load time","Prefer the default overloads unless tuning is measured","Guard arithmetic that derives buffer sizes from other values (underflow risk)"],"tags":["io","buffer","argument-validation","stream"],"backgroundTag":"invalid-argument-value","analyzedSha":"744c3d3075e595656abb1ae331ad2c0e4c9eb996","analyzedAt":"2026-09-07T18:48:45.289Z","contentChangedAt":"2026-09-07T18:48:45.289Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}