Fixed off-by-one error when max_pos is larger than size.