* mem_t: Throw an error if zero-sized memory is requested
If for some reason the user requests a memory size of 0 megabytes, print
a useful error message.
* Check for overflow in memory size
If the user passes in a large enough memory size (-m) that the size in
bytes doesn't fit into size_t, catch this error in the make_mems function.
class mem_t : public abstract_device_t {
public:
mem_t(size_t size) : len(size) {
+ if (!size)
+ throw std::runtime_error("zero bytes of target memory requested");
data = (char*)calloc(1, size);
if (!data)
throw std::runtime_error("couldn't allocate " + std::to_string(size) + " bytes of target memory");
auto mb = strtoull(arg, &p, 0);
if (*p == 0) {
reg_t size = reg_t(mb) << 20;
+ if (size != (size_t)size)
+ throw std::runtime_error("Size would overflow size_t");
return std::vector<std::pair<reg_t, mem_t*>>(1, std::make_pair(reg_t(DRAM_BASE), new mem_t(size)));
}