Class AllocationGuard
- Namespace
- SharpConsoleUI.Helpers
- Assembly
- SharpConsoleUI.dll
Guards against integer overflow and unbounded allocations when computing buffer sizes from potentially untrusted dimensions. Use this whenever allocating arrays whose size derives from external input (image dimensions, video frame sizes, terminal coordinates from protocol responses).
public static class AllocationGuard
- Inheritance
-
AllocationGuard
- Inherited Members
Fields
DefaultMaxElements
Default maximum total elements allowed in a single allocation (64 million). Covers up to ~256 MB for byte arrays or ~512 MB for int arrays.
public const long DefaultMaxElements = 64000000
Field Value
Methods
AreDimensionsValid(int, int, int, long)
Returns true if the given dimensions are within the specified limits. Does not throw. Useful for early-return guards where throwing is not appropriate.
public static bool AreDimensionsValid(int width, int height, int maxDimension = 2147483647, long maxElements = 64000000)
Parameters
widthintFirst dimension.
heightintSecond dimension.
maxDimensionintMaximum value for each individual dimension.
maxElementslongMaximum allowed product.
Returns
- bool
True if dimensions are valid and within limits.
ValidateDimensions(int, int, int, long)
Validates that width * height * depth does not overflow and does not exceed the specified maximum. Used for RGB byte buffers (depth=3).
public static long ValidateDimensions(int width, int height, int depth, long maxElements = 64000000)
Parameters
widthintFirst dimension.
heightintSecond dimension.
depthintThird dimension (e.g., bytes per pixel).
maxElementslongMaximum allowed product.
Returns
- long
The validated product (width * height * depth).
Exceptions
- ArgumentOutOfRangeException
Thrown if any dimension is non-positive or the product exceeds the limit.
ValidateDimensions(int, int, long)
Validates that width * height does not overflow and does not exceed the specified maximum. Returns the product as a checked long.
public static long ValidateDimensions(int width, int height, long maxElements = 64000000)
Parameters
Returns
- long
The validated product (width * height).
Exceptions
- ArgumentOutOfRangeException
Thrown if either dimension is non-positive or the product exceeds the limit.