diff --git a/Testing/Rendering/vtkTesting.cxx b/Testing/Rendering/vtkTesting.cxx
index 1d26ae0ec46169386666c5e8d0bc16e43a00a264..fe83e03531712be39ab4afd6ecc3252e2a5fc7a7 100644
--- a/Testing/Rendering/vtkTesting.cxx
+++ b/Testing/Rendering/vtkTesting.cxx
@@ -845,7 +845,8 @@ int vtkTesting::RegressionTest(vtkAlgorithm* imageSource, double thresh, ostream
     vtkEmscriptenTestUtilities::PreloadDataFile(hostFileName.c_str(), sandboxedFileName);
     // so that subsequent code uses the sandboxed file name instead of host file name.
     delete[] newFileName;
-    newFileName = strdup(sandboxedFileName.c_str());
+    newFileName = new char[sandboxedFileName.size() + 1];
+    strcpy(newFileName, sandboxedFileName.c_str());
 #endif
     if (!LookForFile(newFileName))
     {
@@ -972,7 +973,8 @@ int vtkTesting::RegressionTest(vtkAlgorithm* imageSource, double thresh, ostream
 #ifdef __EMSCRIPTEN__
     std::string sandboxedFileName = vtkEmscriptenTestUtilities::PreloadDataFile(newFileName);
     delete[] newFileName;
-    newFileName = strdup(sandboxedFileName.c_str());
+    newFileName = new char[sandboxedFileName.size() + 1];
+    strcpy(newFileName, sandboxedFileName.c_str());
 #endif
     rtPng->SetFileName(newFileName);
     delete[] newFileName;