Fix potential out-of-bounds read