fix allocate memory out of range bug