`ContentPath` class maintains a variable `index` that has the next available index to write path to. In fact to add a path we did `path[index++] = name;` so we write at index position and increment it afterwards. To remove last path we are doing `path[index--] = null;` this would set to null the next available index position (that is already null) and later subtract 1 from index. This was masked by the fact that when retrieving a path we loop from 0 up until `i < index`, therefore masking the fact that there could have being 'removed' but not null path existing at `index`.

_Originally posted by @piergm in https://github.com/elastic/elasticsearch/pull/97972#discussion_r1287055169_
            
server/src/main/java/org/elasticsearch/index/mapper/ContentPath.java
expand();
        }
    }
    private void expand() {
        String[] newPath = new String[path.length + 10];
        System.arraycopy(path, 0, newPath, 0, path.length);
        path = newPath;
    }
    public void remove() {
        path[index--] = null;
    }
    public void setWithinLeafObject(boolean withinLeafObject) {
        this.withinLeafObject = withinLeafObject;
    }
    public boolean isWithinLeafObject() {
        return withinLeafObject;
    }
    public String pathAsText(String name) {
        sb.setLength(0);
        for (int i = 0; i < index; i++) {
            sb.append(path[i]).append(DELIMITER);
        }
        sb.append(name);
        return sb.toString();
    }
    public int length() {
        return index;
    }
}