mem: fix possible integer overflow