]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
test: Handle GetVolumePathName errors, prevent access violation (#1626)
authorautoantwort <41973254+autoantwort@users.noreply.github.com>
Wed, 10 Sep 2025 16:48:42 +0000 (18:48 +0200)
committerGitHub <noreply@github.com>
Wed, 10 Sep 2025 16:48:42 +0000 (18:48 +0200)
For example GetVolumePathName returns `F:/git_projects` on my computer which
does not fit in a char[4]. As a result GetVolumePathName returns an error and
a std::string is initialized from uninitialized memory which may not contain
a '\0' resulting in out of bounds memory reads.

Co-authored-by: Leander Schulten <Leander.Schulten@tetys.de>
unittest/test_argprocessing.cpp

index 4c2a97f45d326ad69c63d0c3dcf66bb4bcab5791..41c5d46b898fbd59f41d1b21bd931f0711080b10 100644 (file)
@@ -48,10 +48,11 @@ get_root()
 #ifndef _WIN32
   return "/";
 #else
-  char volume[4]; // "C:\"
-  GetVolumePathName(
-    util::pstr(*fs::current_path()).c_str(), volume, sizeof(volume));
-  return volume;
+  auto cwd = fs::current_path();
+  if (!cwd) {
+    FAIL("get_root failed: ", cwd.error());
+  }
+  return cwd->root_path().generic_u8string();
 #endif
 }