diff options
author | Ryan Ofsky <ryan@ofsky.org> | 2022-03-03 14:40:18 -0500 |
---|---|---|
committer | Ryan Ofsky <ryan@ofsky.org> | 2022-04-21 12:01:00 -0500 |
commit | f64aa9c411ad78259756a28756ec1eb8069b5ab4 (patch) | |
tree | 5a7541df983a1321580080b5ff13684a78765c38 /src/fs.h | |
parent | 7a4ac713aad699435cc3175f1c3b6a5d985442a5 (diff) |
Disallow more unsafe string->path conversions allowed by path append operators
Add more fs::path operator/ and operator+ overloads to prevent unsafe
string->path conversions on Windows that would cause strings to be
decoded according to the current Windows locale & code page instead of
the correct string encoding.
Update application code to deal with loss of implicit string->path
conversions by calling fs::u8path or fs::PathFromString explicitly, or
by just changing variable types from std::string to fs::path to avoid
conversions altoghther, or make them happen earlier.
In all cases, there's no change in behavior either (1) because strings
only contained ASCII characters and would be decoded the same regardless
of what encoding was used, or (2) because of the 1:1 mapping between
paths and strings using the PathToString and PathFromString functions.
Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
Diffstat (limited to 'src/fs.h')
-rw-r--r-- | src/fs.h | 23 |
1 files changed, 21 insertions, 2 deletions
@@ -92,11 +92,30 @@ static inline auto quoted(const std::string& s) } // Allow safe path append operations. -static inline path operator+(path p1, path p2) +static inline path operator/(path p1, path p2) { - p1 += std::move(p2); + p1 /= std::move(p2); return p1; } +static inline path operator/(path p1, const char* p2) +{ + p1 /= p2; + return p1; +} +static inline path operator+(path p1, const char* p2) +{ + p1 += p2; + return p1; +} +static inline path operator+(path p1, path::value_type p2) +{ + p1 += p2; + return p1; +} + +// Disallow unsafe path append operations. +template<typename T> static inline path operator/(path p1, T p2) = delete; +template<typename T> static inline path operator+(path p1, T p2) = delete; // Disallow implicit std::string conversion for copy_file // to avoid locale-dependent encoding on Windows. |