Table of Contents

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

long

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

width int

First dimension.

height int

Second dimension.

maxDimension int

Maximum value for each individual dimension.

maxElements long

Maximum 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

width int

First dimension.

height int

Second dimension.

depth int

Third dimension (e.g., bytes per pixel).

maxElements long

Maximum 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

width int

First dimension.

height int

Second dimension.

maxElements long

Maximum allowed product.

Returns

long

The validated product (width * height).

Exceptions

ArgumentOutOfRangeException

Thrown if either dimension is non-positive or the product exceeds the limit.