Allocating memory with a size based on user input may allow arbitrary amounts of memory to be +allocated, leading to a crash or denial of service incident.
+ +If the user input is multiplied by a constant, such as the size of a type, the result may
+overflow. In a build with the --release flag Rust performs two's complement wrapping,
+with the result that less memory may be allocated than expected. This can lead to buffer overflow
+incidents.
Implement a guard to limit the amount of memory that is allocated, and reject the request if
+the guard is not met. Ensure that any multiplications in the calculation cannot overflow, either
+by guarding their inputs, or using a multiplication routine such as checked_mul that
+does not wrap around.
In the following example, an arbitrary amount of memory is allocated based on user input. In +addition, due to the multiplication operation the result may overflow if a very large value is +provided, leading to less memory being allocated than other parts of the program expect.
+In the fixed example, the user input is checked against a maximum value. If the check fails an +error is returned, and both the multiplication and alloaction do not take place.
+