Skip to content

Commit 76d44e8

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24469: test: Correctly decode UTF-8 literal string paths
2f5fd3c test: Correctly decode UTF-8 literal string paths (Ryan Ofsky) Pull request description: Call `fs::u8path()` to convert some UTF-8 string literals to paths, instead of relying on the implicit conversion. Fake Macro pointed out in bitcoin/bitcoin#24306 (comment) that `fs_tests` are incorrectly decoding some literal UTF-8 paths using the current windows codepage, instead of treating them as UTF-8. This could cause test failures depending what environment windows tests are run under. The `fs::path` class exists to avoid problems like this, but because it is lenient with `const char*` conversions, under assumption that they are ["safe as long as the literals are ASCII"](https://github.com/bitcoin/bitcoin/blob/727b0cb59259ac63c627b09b503faada1a89bfb8/src/fs.h#L39), bugs like this are still possible. If we think this is a concern, followup options to try to prevent this bug in the future are: 0. Do nothing 1. Improve the "safe as long as the literals are ASCII" comment. Make it clear that non-ASCII strings are invalid. 2. Drop the implicit `const char*` conversion functions. This would be nice because it would simplifify the `fs::path` class a little, while making it safer. Drawback is that it would require some more verbosity from callers. For example, instead of `GetDataDirNet() / "mempool.dat"` they would have to write `GetDataDirNet() / fs::u8path("mempool.dat")` 3. Keep the implicit `const char*` conversion functions, but make them call `fs::u8path()` internally. Change the "safe as long as the literals are *ASCII*" comment to "safe as long as the literals are *UTF-8*". I'd be happy with 0, 1, or 2. I'd be a little resistant to 3 even though it was would add more safety, because it would slightly increase complexity, and because I think it would encourage representing paths as strings, when I think there are so many footguns associated with paths as strings, that it's best to convert strings to paths at the earliest point possible, and convert paths to strings at the latest point possible. ACKs for top commit: laanwj: Code review ACK 2f5fd3c w0xlt: crACK 2f5fd3c Tree-SHA512: 9c56714744592094d873b79843b526d20f31ed05eff957d698368d66025764eae8bfd5305d5f7b6cc38803f0d85fa5552003e5c6cacf1e076ea6d313bcbc960c
2 parents d1a940f + 2f5fd3c commit 76d44e8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/test/fs_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
4646
{
4747
fs::path tmpfolder = m_args.GetDataDirBase();
4848
// tmpfile1 should be the same as tmpfile2
49-
fs::path tmpfile1 = tmpfolder / "fs_tests_₿_🏃";
50-
fs::path tmpfile2 = tmpfolder / "fs_tests_₿_🏃";
49+
fs::path tmpfile1 = tmpfolder / fs::u8path("fs_tests_₿_🏃");
50+
fs::path tmpfile2 = tmpfolder / fs::u8path("fs_tests_₿_🏃");
5151
{
5252
std::ofstream file{tmpfile1};
5353
file << "bitcoin";
@@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
8686
}
8787
{
8888
// Join an absolute path and a relative path.
89-
fs::path p = fsbridge::AbsPathJoin(tmpfolder, "fs_tests_₿_🏃");
89+
fs::path p = fsbridge::AbsPathJoin(tmpfolder, fs::u8path("fs_tests_₿_🏃"));
9090
BOOST_CHECK(p.is_absolute());
9191
BOOST_CHECK_EQUAL(tmpfile1, p);
9292
}

0 commit comments

Comments
 (0)