Repository navigation
Keep only ASCII from the hostname in Posit Assistant staging directory names - #19626
Conversation
The staging directory name sanitized the hostname with std::isalnum(), which accepts some bytes above 0x7F in macOS UTF-8 and Windows code-page locales. A hostname like "jose-MacBook" with an accented e became "jos\xC3_-MacBook": invalid UTF-8, which APFS refuses with EILSEQ and R < 4.2 on Windows misreads as a different directory. The sanitizer now keeps only ASCII letters, digits, '-' and '.', whatever the locale. prepareStagingDir() takes the hostname as a parameter so tests can pass a non-ASCII one; getHostname() caches its first result. Addresses #19624.
| for (const char* name : { ".1252", "en_US.UTF-8" }) | ||
| { | ||
| if (std::setlocale(LC_CTYPE, name) != nullptr) | ||
| break; | ||
| } |
There was a problem hiding this comment.
If neither .1252 nor en_US.UTF-8 can be set, this falls through silently and the two tests that use it run in the current locale. There the old isalnum() check also rejects high bytes, so the tests pass with or without the fix and stop guarding the regression.
Unlikely on stock images (macOS has en_US.UTF-8, where isalnum(0xC3) and isalnum(0xE9) are true; Windows always has .1252), but it fails quietly when it does. Consider recording whether a locale was set and having the tests GTEST_SKIP() (or assert, as StringUtilsTests.cpp does with ASSERT_NE(nullptr, _wsetlocale(...))) when none was, except on Linux, where the comment already says they pass either way.
There was a problem hiding this comment.
Fixed in b52501c. The helper now records whether it set .1252 or en_US.UTF-8, and outside Linux reports a failure (ADD_FAILURE()) when neither could be set, so the two tests fail instead of passing vacuously. Checked by temporarily substituting nonexistent locale names: both tests failed with "Could not set LC_CTYPE to .1252 or en_US.UTF-8".
Without .1252 or en_US.UTF-8, the locale helper fell through to a locale where the old isalnum() check was already safe, so the two staging-name tests passed with or without the fix. Outside Linux, where glibc makes the locale irrelevant, the helper now fails the test instead. Addresses #19624.
| const char* current = std::setlocale(LC_CTYPE, nullptr); | ||
| previous_ = current != nullptr ? current : "C"; | ||
|
|
||
| bool set = false; |
There was a problem hiding this comment.
On Linux the only read of set (inside #ifndef __linux__) is preprocessed out, so set is written but never read and the -Wall build (src/cpp/CMakeLists.txt:65) warns -Wunused-but-set-variable. Reproduced with clang -Wall on a copy with that block removed: "variable 'set' set but not used"; GCC's -Wall includes the same warning.
Either [[maybe_unused]] bool set = false; or moving the set bookkeeping inside the #ifndef avoids it.
There was a problem hiding this comment.
Fixed in 64270b3. The helper no longer has the flag: it returns as soon as a locale is set, and reports the failure (outside Linux) after the loop. Compiled the helper with clang -Wall -Wextra -Werror with and without -D__linux__: no warnings either way. The two tests still fail with "Could not set LC_CTYPE to .1252 or en_US.UTF-8" when given nonexistent locale names.
On Linux the only read of the locale helper's "set" flag is preprocessed out, so -Wall warned "variable 'set' set but not used". The helper now returns as soon as a locale is set and reports the failure after the loop, with no flag. Addresses #19624.
RStudio E2E - all platformsAll tests passed!
All platforms - commit |
kevinushey
left a comment
There was a problem hiding this comment.
Looks good. I checked the premise locally on macOS: under en_US.UTF-8, isalnum() accepts 0xC3 and 0xE9 but not 0xA9, so the old sanitizer turns jos\xC3\xA9-MacBook into jos\xC3_-MacBook and mkdir rejects it with "Illegal byte sequence". The explicit ASCII ranges give jos__-MacBook and jos_-PC, as the tests expect. Nothing else calls prepareStagingDir() or reads the hostname back out of a staging directory name, so the change in sanitizing has no other consumers.
One non-blocking note: PR CI runs the C++ unit tests on Linux only (jenkins/Jenkinsfile.pull-request-build), where the two new tests pass with or without the fix. The .1252 branch of ScopedHighByteCType will first run in the Windows daily build (jenkins/Jenkinsfile.windows), so that is where a problem with it would show up.
Fixes #19624.
Installing or updating Posit Assistant extracts into a staging directory named after the hostname. The hostname was sanitized byte by byte with
std::isalnum(), which accepts some bytes above 0x7F in macOS UTF-8 locales and Windows code-page locales. A hostname with an accented letter then produced an invalid UTF-8 directory name: macOS refused to create it ("Illegal byte sequence"), and on Windows with R < 4.2, R'sunzip()misread it and extracted elsewhere.stagingDirName()now keeps only ASCII letters, digits,-and., checked explicitly, independent of the locale; every other byte becomes_.prepareStagingDir()takes the hostname as a parameter so tests can pass a non-ASCII one;installPackage()passescore::system::getHostname().Tests
Two new
ChatSlotstests stage an install with a UTF-8 hostname (jos\xC3\xA9-MacBook) and a code-page-1252 one (jos\xE9-PC), in a locale where the old check let high bytes through (.1252on Windows,en_US.UTF-8on macOS), and check that the directory is created with an ASCII name. On macOS both failed before the change and pass after it. On Linux they pass either way, because glibc'sisalnum()rejects all bytes above 0x7F.The paths to R's
unzip()and the download are still passed in the native encoding, which breaks on Windows with R < 4.2 whenever the data directory path is non-ASCII; that is tracked separately in #19625.