check that the pointer given by the user is actually correct before copying data

It seems the user can trigger NULL pointer dereference by passing wrong pointers.