Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions cpp/src/arrow/io/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ namespace io {

// ----------------------------------------------------------------------
// Cross-platform file compatability layer
#if defined(_MSC_VER)
constexpr const char* kRangeExceptionError =
"Range exception during wide-char string conversion";
#endif

static inline Status CheckOpenResult(
int ret, int errno_actual, const char* filename, size_t filename_length) {
Expand All @@ -131,8 +135,10 @@ static inline Status CheckOpenResult(
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
std::wstring wide_string(
reinterpret_cast<const wchar_t*>(filename), filename_length / sizeof(wchar_t));
std::string byte_string = converter.to_bytes(wide_string);
ss << byte_string;
try {
std::string byte_string = converter.to_bytes(wide_string);
ss << byte_string;
} catch (const std::range_error&) { ss << kRangeExceptionError; }
#else
ss << filename;
#endif
Expand Down Expand Up @@ -162,7 +168,9 @@ static inline Status ConvertToUtf16(const std::string& input, std::wstring* resu
}

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utf16_converter;
*result = utf16_converter.from_bytes(input);
try {
*result = utf16_converter.from_bytes(input);
} catch (const std::range_error&) { return Status::Invalid(kRangeExceptionError); }
return Status::OK();
}
#endif
Expand Down
12 changes: 12 additions & 0 deletions cpp/src/arrow/io/io-file-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ class TestFileOutputStream : public FileTestFixture {
std::shared_ptr<FileOutputStream> file_;
};

#if defined(_MSC_VER)
TEST_F(TestFileOutputStream, FileNameWideCharConversionRangeException) {
std::shared_ptr<FileOutputStream> file;
// Form literal string with non-ASCII symbol(127 + 1)
std::string file_name = "\x80";
ASSERT_RAISES(Invalid, FileOutputStream::Open(file_name, &file));

std::shared_ptr<ReadableFile> rd_file;
ASSERT_RAISES(Invalid, ReadableFile::Open(file_name, &rd_file));
}
#endif

TEST_F(TestFileOutputStream, DestructorClosesFile) {
int fd;
{
Expand Down