[Fix #7772] Check attribute index before setting attribute to avoid crash