I just added a check to avoid integer overflow
 when allocating the array to be returned.