@@ -406,6 +406,10 @@ std::unique_ptr<wchar_t[]> ToWinAPIPath(const char* utf8_path) {
406406
407407File* File::Open (Namespace* namespc, const char * name, FileOpenMode mode) {
408408 const auto path = ToWinAPIPath (name);
409+ if (path.get () == nullptr ) {
410+ SetLastError (ERROR_INVALID_NAME);
411+ return nullptr ;
412+ }
409413 File* file = FileOpenW (path.get (), mode);
410414 return file;
411415}
@@ -476,6 +480,9 @@ static bool FileExists(const wchar_t* path) {
476480
477481bool File::Exists (Namespace* namespc, const char * name) {
478482 const auto path = ToWinAPIPath (name);
483+ if (path.get () == nullptr ) {
484+ return false ;
485+ }
479486 return FileExists (path.get ());
480487}
481488
@@ -490,6 +497,9 @@ bool File::ExistsUri(Namespace* namespc, const char* uri) {
490497
491498bool File::Create (Namespace* namespc, const char * name, bool exclusive) {
492499 const auto path = ToWinAPIPath (name);
500+ if (path.get () == nullptr ) {
501+ return false ;
502+ }
493503 int flags = O_RDONLY | O_CREAT;
494504 if (exclusive) {
495505 flags |= O_EXCL;
@@ -535,11 +545,19 @@ bool File::CreateLink(Namespace* namespc,
535545 const char * utf8_name,
536546 const char * utf8_target) {
537547 const auto name = ToWinAPIPath (utf8_name);
548+ if (name.get () == nullptr ) {
549+ SetLastError (ERROR_INVALID_NAME);
550+ return false ;
551+ }
538552
539553 std::unique_ptr<wchar_t []> target;
540554 bool target_is_directory;
541555 if (File::IsAbsolutePath (utf8_target)) {
542556 target = ToWinAPIPath (utf8_target);
557+ if (target.get () == nullptr ) {
558+ SetLastError (ERROR_INVALID_NAME);
559+ return false ;
560+ }
543561 target_is_directory =
544562 File::GetType (target.get (), /* follow_links=*/ true ) == kIsDirectory ;
545563 } else {
@@ -625,6 +643,9 @@ bool File::CreatePipe(Namespace* namespc, File** readPipe, File** writePipe) {
625643
626644bool File::Delete (Namespace* namespc, const char * name) {
627645 const auto path = ToWinAPIPath (name);
646+ if (path.get () == nullptr ) {
647+ return false ;
648+ }
628649 int status = _wremove (path.get ());
629650 return status != -1 ;
630651}
@@ -650,19 +671,30 @@ static bool DeleteLinkHelper(const wchar_t* path) {
650671
651672bool File::DeleteLink (Namespace* namespc, const char * name) {
652673 const auto path = ToWinAPIPath (name);
674+ if (path.get () == nullptr ) {
675+ return false ;
676+ }
653677 return DeleteLinkHelper (path.get ());
654678}
655679
656680static bool RenameHelper (File::Type expected,
657681 const char * old_name,
658682 const char * new_name) {
659683 const auto old_path = ToWinAPIPath (old_name);
684+ if (old_path.get () == nullptr ) {
685+ SetLastError (ERROR_INVALID_NAME);
686+ return false ;
687+ }
660688 File::Type type = File::GetType (old_path.get (), /* follow_links=*/ false );
661689 if (type != expected) {
662690 SetLastError (ERROR_FILE_NOT_FOUND);
663691 return false ;
664692 }
665693 const auto new_path = ToWinAPIPath (new_name);
694+ if (new_path.get () == nullptr ) {
695+ SetLastError (ERROR_INVALID_NAME);
696+ return false ;
697+ }
666698 DWORD flags = MOVEFILE_WRITE_THROUGH | MOVEFILE_REPLACE_EXISTING;
667699
668700 // Symbolic links (e.g. produced by Link.create) to directories on Windows
@@ -780,6 +812,10 @@ bool File::Copy(Namespace* namespc,
780812 // CopyIntoTempFile so we force long prefix no matter what.
781813 const auto old_path = ToWinAPIPath (old_name);
782814 const auto new_path = ToWinAPIPath (new_name);
815+ if (new_path.get () == nullptr || old_path.get () == nullptr ) {
816+ SetLastError (ERROR_INVALID_NAME);
817+ return false ;
818+ }
783819
784820 File::Type type = GetType (old_path.get (), /* follow_links=*/ false );
785821 if (type != kIsFile ) {
@@ -812,7 +848,7 @@ bool File::Copy(Namespace* namespc,
812848int64_t File::LengthFromPath (Namespace* namespc, const char * name) {
813849 struct __stat64 st;
814850 const auto path = ToWinAPIPath (name);
815- if (!StatHelper (path.get (), &st)) {
851+ if (path. get () == nullptr || !StatHelper (path.get (), &st)) {
816852 return -1 ;
817853 }
818854 return st.st_size ;
@@ -823,6 +859,9 @@ const char* File::LinkTarget(Namespace* namespc,
823859 char * dest,
824860 int dest_size) {
825861 const auto path = ToWinAPIPath (pathname);
862+ if (path.get () == nullptr ) {
863+ return nullptr ;
864+ }
826865 HANDLE dir_handle = CreateFileW (
827866 path.get (), GENERIC_READ,
828867 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr ,
@@ -910,6 +949,10 @@ const char* File::LinkTarget(Namespace* namespc,
910949
911950void File::Stat (Namespace* namespc, const char * name, int64_t * data) {
912951 const auto path = ToWinAPIPath (name);
952+ if (path.get () == nullptr ) {
953+ data[kType ] = File::kDoesNotExist ;
954+ return ;
955+ }
913956 File::Type type = GetType (path.get (), /* follow_links=*/ true );
914957 data[kType ] = type;
915958 if (type != kDoesNotExist ) {
@@ -930,7 +973,7 @@ void File::Stat(Namespace* namespc, const char* name, int64_t* data) {
930973time_t File::LastAccessed (Namespace* namespc, const char * name) {
931974 struct __stat64 st;
932975 const auto path = ToWinAPIPath (name);
933- if (!StatHelper (path.get (), &st)) {
976+ if (path. get () == nullptr || !StatHelper (path.get (), &st)) {
934977 return -1 ;
935978 }
936979 return st.st_atime ;
@@ -939,7 +982,7 @@ time_t File::LastAccessed(Namespace* namespc, const char* name) {
939982time_t File::LastModified (Namespace* namespc, const char * name) {
940983 struct __stat64 st;
941984 const auto path = ToWinAPIPath (name);
942- if (!StatHelper (path.get (), &st)) {
985+ if (path. get () == nullptr || !StatHelper (path.get (), &st)) {
943986 return -1 ;
944987 }
945988 return st.st_mtime ;
@@ -950,7 +993,7 @@ bool File::SetLastAccessed(Namespace* namespc,
950993 int64_t millis) {
951994 struct __stat64 st;
952995 const auto path = ToWinAPIPath (name);
953- if (!StatHelper (path.get (), &st)) { // Checks that it is a file.
996+ if (path. get () == nullptr || !StatHelper (path.get (), &st)) {
954997 return false ;
955998 }
956999
@@ -979,7 +1022,7 @@ bool File::SetLastModified(Namespace* namespc,
9791022 // First get the current times.
9801023 struct __stat64 st;
9811024 const auto path = ToWinAPIPath (name);
982- if (!StatHelper (path.get (), &st)) {
1025+ if (path. get () == nullptr || !StatHelper (path.get (), &st)) {
9831026 return false ;
9841027 }
9851028
@@ -1008,6 +1051,10 @@ const char* File::GetCanonicalPath(Namespace* namespc,
10081051 char * dest,
10091052 int dest_size) {
10101053 const auto path = ToWinAPIPath (pathname);
1054+ if (path.get () == nullptr ) {
1055+ SetLastError (ERROR_INVALID_NAME);
1056+ return nullptr ;
1057+ }
10111058 HANDLE file_handle =
10121059 CreateFileW (path.get (), 0 , FILE_SHARE_READ, nullptr , OPEN_EXISTING,
10131060 FILE_FLAG_BACKUP_SEMANTICS, nullptr );
@@ -1110,6 +1157,9 @@ File::Type File::GetType(Namespace* namespc,
11101157 const char * name,
11111158 bool follow_links) {
11121159 const auto path = ToWinAPIPath (name);
1160+ if (path.get () == nullptr ) {
1161+ return File::kDoesNotExist ;
1162+ }
11131163 return GetType (path.get (), follow_links);
11141164}
11151165
@@ -1122,6 +1172,9 @@ File::Identical File::AreIdentical(Namespace* namespc_1,
11221172 BY_HANDLE_FILE_INFORMATION file_info[2 ];
11231173 const std::unique_ptr<wchar_t []> file_names[2 ] = {ToWinAPIPath (file_1),
11241174 ToWinAPIPath (file_2)};
1175+ if (file_names[0 ].get () == nullptr || file_names[1 ].get () == nullptr ) {
1176+ return File::kError ;
1177+ }
11251178 for (int i = 0 ; i < 2 ; ++i) {
11261179 HANDLE file_handle = CreateFileW (
11271180 file_names[i].get (), 0 ,
0 commit comments