fix a bug that index may out of range