(Pre-existing) You need to revise path_len after this.  Otherwise, all the uses of path_len below are referring to the length path had before it was preprocessed - e.g. the check for whether path ends in a slash (when it's a directory) actually checks for whether path has a slash at the position in which it used to end before preprocessing.  After preprocessing, this is not guaranteed to be the end of path !