malloc with a multiplied size can overflow and allocate too little memory.
Use calloc or check for multiplication overflow before allocating.