add a protection on writing bytes exceeding memory