Common/FileSystem: Fix canonicalization of paths beginning with `./`

If we don't skip past the following separator when the destination
is empty, then `./file` gets canonicalized as `/file`.

Also, consider the case where we end up with an empty string (for
example, from `foo/..`). Canonicalize this as `.`.
This commit is contained in:
Michael Forney 2020-12-24 23:24:41 -08:00 committed by Connor McLaughlin
parent 64c0ca14a3
commit d0398c8a83
1 changed files with 7 additions and 0 deletions

View File

@ -75,6 +75,9 @@ void CanonicalizePath(char* Destination, u32 cbDestination, const char* Path, bo
// remove the previous \, if we have one trailing the dot it'll append it anyway // remove the previous \, if we have one trailing the dot it'll append it anyway
if (destinationLength > 0) if (destinationLength > 0)
Destination[--destinationLength] = '\0'; Destination[--destinationLength] = '\0';
// if there was no previous \, skip past the next one
else if (nextCh != '\0')
i++;
continue; continue;
} }
@ -126,6 +129,10 @@ void CanonicalizePath(char* Destination, u32 cbDestination, const char* Path, bo
i++; i++;
} }
// if we end up with the empty string, return '.'
if (destinationLength == 0)
Destination[destinationLength++] = '.';
// ensure nullptr termination // ensure nullptr termination
if (destinationLength < cbDestination) if (destinationLength < cbDestination)
Destination[destinationLength] = '\0'; Destination[destinationLength] = '\0';