{"record":{"id":"4dd486c5c15ba89d","repo":"ruby-concurrency/concurrent-ruby","slug":"value-is-greater-than-the-maximum-value-of-ma","errorCode":null,"errorMessage":"#{value} is greater than the maximum value of #{MAX_VALUE}","messagePattern":"#(.+?) is greater than the maximum value of #(.+?)","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"lib/concurrent-ruby/concurrent/utility/native_integer.rb","lineNumber":12,"sourceCode":"module Concurrent\n  # @!visibility private\n  module Utility\n    # @private\n    module NativeInteger\n      # http://stackoverflow.com/questions/535721/ruby-max-integer\n      MIN_VALUE = -(2**(0.size * 8 - 2))\n      MAX_VALUE = (2**(0.size * 8 - 2) - 1)\n\n      def ensure_upper_bound(value)\n        if value > MAX_VALUE\n          raise RangeError.new(\"#{value} is greater than the maximum value of #{MAX_VALUE}\")\n        end\n        value\n      end\n\n      def ensure_lower_bound(value)\n        if value < MIN_VALUE\n          raise RangeError.new(\"#{value} is less than the maximum value of #{MIN_VALUE}\")\n        end\n        value\n      end\n\n      def ensure_integer(value)\n        unless value.is_a?(Integer)\n          raise ArgumentError.new(\"#{value} is not an Integer\")\n        end\n        value\n      end\n","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/ruby-concurrency/concurrent-ruby/blob/0b88d5ff75f69b3740c8f0868e76f833cb2fd45d/lib/concurrent-ruby/concurrent/utility/native_integer.rb#L1-L30","documentation":"Concurrent::Utility::NativeInteger guards native-backed primitives (AtomicFixnum, Semaphore, CountDownLatch, CyclicBarrier) against values that exceed what a native fixnum slot holds: 2**62 - 1 on 64-bit MRI. Ruby Integers are arbitrary precision, so large results compute silently and only blow up when stored into one of these primitives as RangeError. AtomicFixnum.new, AtomicFixnum#value=, and #update all pass through this check.","triggerScenarios":"Concurrent::AtomicFixnum.new(2**63); counter.update { |v| v * 2 } crossing the bound; bit masks like (1 << 63) stored into an AtomicFixnum; IDs/counters aggregated over a long-lived process.","commonSituations":"Shard/snowflake-style IDs or epoch-millisecond counters pushed into AtomicFixnum; multiplying large aggregates in update blocks; porting code from JRuby (where native limits differ) to MRI.","solutions":["Validate before storing: raise RangeError, 'counter overflow' if value > Concurrent::Utility::NativeInteger::MAX_VALUE","Keep oversized counters in a plain Integer guarded by a Mutex or use two counters (high/low words) instead of AtomicFixnum","Clamp if saturation is acceptable: value = [value, MAX_VALUE].min (document the saturation)"],"exampleFix":"# before\ncounter = Concurrent::AtomicFixnum.new(2**63) # RangeError\n\n# after\nmax = Concurrent::Utility::NativeInteger::MAX_VALUE\nraise RangeError, 'id too large for atomic counter' if id > max\ncounter = Concurrent::AtomicFixnum.new(id)","handlingStrategy":"validation","validationCode":"NI = Concurrent::Utility::NativeInteger\nraise RangeError, \"#{value} exceeds atomic counter range\" if value.is_a?(Integer) && value > NI::MAX_VALUE\nConcurrent::AtomicFixnum.new(value)","typeGuard":"def native_int?(v)\n  v.is_a?(Integer) &&\n    v.between?(Concurrent::Utility::NativeInteger::MIN_VALUE,\n               Concurrent::Utility::NativeInteger::MAX_VALUE)\nend","tryCatchPattern":"begin\n  counter.value = next_value\nrescue RangeError\n  counter.value = Concurrent::Utility::NativeInteger::MAX_VALUE # explicit saturation + alert\nend","preventionTips":["Check against NativeInteger::MAX_VALUE before storing big computed values","Keep unbounded counters in plain Integer + Mutex; use AtomicFixnum only for provably bounded values","Add overflow asserts in update blocks that multiply or shift"],"tags":["ruby","concurrency","atomic","range-error","integer-overflow"],"backgroundTag":"integer-overflow","analyzedSha":"0b88d5ff75f69b3740c8f0868e76f833cb2fd45d","analyzedAt":"2026-08-21T20:12:56.291Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}