{"record":{"id":"e86bfc38255b7463","repo":"aeron-io/aeron","slug":"einval-counter-id-id-out-of-range-maxcounterid-maxcounterid","errorCode":"EINVAL","errorMessage":"counter id {id} out of range: maxCounterId={maxCounterId}","messagePattern":"counter id (.+?) out of range: maxCounterId=(.+?)","errorType":"exception","errorClass":"util::IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"aeron-client/src/main/cpp_wrapper/concurrent/CountersReader.h","lineNumber":191,"sourceCode":"    inline std::int64_t getCounterOwnerId(std::int32_t id) const\n    {\n        validateCounterId(id);\n\n        std::int64_t ownerId;\n        if (aeron_counters_reader_counter_owner_id(m_countersReader, id, &ownerId) < 0)\n        {\n            AERON_MAP_ERRNO_TO_SOURCED_EXCEPTION_AND_THROW;\n        }\n\n        return ownerId;\n    }\n\n    inline std::int32_t getCounterState(std::int32_t id) const\n    {\n        std::int32_t state;\n        if (aeron_counters_reader_counter_state(m_countersReader, id, &state) < 0)\n        {\n            throw util::IllegalArgumentException(\n                \"counter id \" + std::to_string(id) +\n                \" out of range: maxCounterId=\" + std::to_string(maxCounterId()),\n                SOURCEINFO, EINVAL);\n        }\n\n        return state;\n    }\n\n    inline std::int32_t getCounterTypeId(std::int32_t id) const\n    {\n        std::int32_t typeId;\n        if (aeron_counters_reader_counter_type_id(m_countersReader, id, &typeId) < 0)\n        {\n            throw util::IllegalArgumentException(\n                \"counter id \" + std::to_string(id) +\n                \" out of range: maxCounterId=\" + std::to_string(maxCounterId()),\n                SOURCEINFO, EINVAL);\n        }","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/aeron-io/aeron/blob/6d60124e15e35c11b49ba2e3c2c2858a09a18803/aeron-client/src/main/cpp_wrapper/concurrent/CountersReader.h#L173-L209","documentation":"CountersReader::getCounterState() validates the counter id via the C API and throws IllegalArgumentException (EINVAL) when the id is outside the valid range (0 .. maxCounterId()-1, derived from the counter metadata buffer size). The underlying aeron_counters_reader_counter_state returns < 0 for any id that has no metadata slot.","triggerScenarios":"Calling getCounterState(), getRecordingId(), getSourceIdentity(), isActive(), or byCounterId-style iteration with id < 0 or id >= maxCounterId(); using a counter id from a different, stale Aeron client or a different media driver run.","commonSituations":"Persisting counter ids across driver restarts; iterating with an upper bound taken from a different CountersReader; sign errors making id negative; hard-coded counter ids that depend on allocation order.","solutions":["Validate 0 <= id < countersReader.maxCounterId() before any counter lookup.","Look up counters by label/typeId via forEach/findByLabel instead of persisted raw ids.","Re-acquire counter ids from the current driver session after a restart rather than reusing cached values.","Check for negative ids from signed arithmetic before calling the API."],"exampleFix":"// before\nauto state = countersReader.getCounterState(cachedId); // cachedId may be stale\n// after\nif (cachedId < 0 || cachedId >= countersReader.maxCounterId())\n    throw std::runtime_error(\"counter id out of range\");\nauto state = countersReader.getCounterState(cachedId);","handlingStrategy":"validation","validationCode":"bool counterIdValid(const aeron::concurrent::CountersReader& r, std::int32_t id) {\n    return id >= 0 && id < r.maxCounterId();\n}","typeGuard":null,"tryCatchPattern":"try {\n    auto state = countersReader.getCounterState(id);\n} catch (const aeron::util::IllegalArgumentException& e) {\n    // id stale/out of range: re-enumerate counters via for-each\n}","preventionTips":["Validate counter ids against maxCounterId() before any lookup.","Never persist counter ids across driver restarts; look them up by label/typeId.","Re-acquire ids from the live CountersReader after reconnecting."],"tags":["aeron","cpp","counters","range-check"],"backgroundTag":"value-out-of-range","analyzedSha":"6d60124e15e35c11b49ba2e3c2c2858a09a18803","analyzedAt":"2026-09-12T11:17:07.683Z","contentChangedAt":"2026-09-12T11:17:07.683Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}