{"record":{"id":"3e80602c87f876ca","repo":"elastic/elasticsearch","slug":"input-cannot-be-null","errorCode":null,"errorMessage":"input cannot be null","messagePattern":"input cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libs/gpu-codec/src/main/java/org/elasticsearch/gpu/codec/DatasetUtilsImpl.java","lineNumber":47,"sourceCode":"        try {\n            return (CuVSMatrix) createDataset$mh.invokeExact(memorySegment, size, dimensions, dataType);\n        } catch (Throwable e) {\n            if (e instanceof Error err) {\n                throw err;\n            } else if (e instanceof RuntimeException re) {\n                throw re;\n            } else {\n                throw new RuntimeException(e);\n            }\n        }\n    }\n\n    private DatasetUtilsImpl() {}\n\n    @Override\n    public CuVSMatrix fromInput(MemorySegment input, int numVectors, int dims, CuVSMatrix.DataType dataType) {\n        if (input == null) {\n            throw new IllegalArgumentException(\"input cannot be null\");\n        }\n        if (numVectors < 0 || dims < 0) {\n            throwIllegalArgumentException(numVectors, dims);\n        }\n        final int byteSize = dataType == CuVSMatrix.DataType.FLOAT ? Float.BYTES : Byte.BYTES;\n        if (((long) numVectors * dims * byteSize) > input.byteSize()) {\n            throwIllegalArgumentException(input, numVectors, dims);\n        }\n        return fromMemorySegment(input, numVectors, dims, dataType);\n    }\n\n    static void throwIllegalArgumentException(MemorySegment ms, int numVectors, int dims) {\n        var s = \"segment of size [\" + ms.byteSize() + \"] too small for expected \" + numVectors + \" float vectors of \" + dims + \" dims\";\n        throw new IllegalArgumentException(s);\n    }\n\n    static void throwIllegalArgumentException(int numVectors, int dims) {\n        String s;","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/gpu-codec/src/main/java/org/elasticsearch/gpu/codec/DatasetUtilsImpl.java#L29-L65","documentation":"Thrown by DatasetUtilsImpl.fromInput when the input MemorySegment is null. fromInput wraps a native memory segment as a CuVSMatrix; a null segment has no backing memory to read vectors from, so it is rejected before any dimension or size check.","triggerScenarios":"Calling datasetUtils.fromInput(null, numVectors, dims, dataType) — the first guard in fromInput checks input == null and throws. Reachable when a caller passes a MemorySegment that was never allocated or whose allocation failed and returned null.","commonSituations":"A memory allocation that returned null on failure (out of native memory) being passed through unchecked; a code path that conditionally allocates the segment and passes it even when allocation was skipped; a test passing null inadvertently.","solutions":["Ensure the MemorySegment is allocated before calling fromInput; check for null after allocation and handle the failure.","If allocation can fail (native OOM), propagate a meaningful error rather than passing null downstream.","In tests, allocate a real MemorySegment of sufficient size (numVectors * dims * elementByteSize)."],"exampleFix":"// before: passing a possibly-null segment\nMemorySegment seg = maybeAllocate(numVectors * dims * Float.BYTES);\nCuVSMatrix m = datasetUtils.fromInput(seg, numVectors, dims, DataType.FLOAT);\n\n// after: null-check after allocation\nMemorySegment seg = maybeAllocate(numVectors * dims * Float.BYTES);\nif (seg == null) {\n    throw new OutOfMemoryError(\"failed to allocate GPU dataset segment\");\n}\nCuVSMatrix m = datasetUtils.fromInput(seg, numVectors, dims, DataType.FLOAT);","handlingStrategy":"validation","validationCode":"static CuVSMatrix safeFromInput(MemorySegment input, int numVectors, int dims, CuVSMatrix.DataType dt) {\n    if (input == null) {\n        throw new OutOfMemoryError(\"failed to allocate dataset segment (null)\");\n    }\n    return datasetUtils.fromInput(input, numVectors, dims, dt);\n}","typeGuard":"static boolean isAllocated(MemorySegment seg) {\n    return seg != null && seg.byteSize() > 0;\n}","tryCatchPattern":"try {\n    return datasetUtils.fromInput(input, numVectors, dims, dt);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().equals(\"input cannot be null\")) {\n        // allocation failed upstream; rethrow as OOM with context\n        throw new OutOfMemoryError(\"GPU dataset segment was null\");\n    }\n    throw e;\n}","preventionTips":["Always null-check a MemorySegment after allocation.","Propagate allocation failures as explicit errors, not null.","In tests, allocate a segment of size numVectors * dims * elementBytes."],"tags":["gpu","validation","elasticsearch","codec","vector-search","null-check"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}