diff --git a/SystemTools.cxx b/SystemTools.cxx
index 3452259400d3ecd467348fc22b7ad39e9a5bff24..a2c24a7d41275c3895333d191534ba07e9e18c4c 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -4243,6 +4243,7 @@ SystemTools::DetectFileType(const char *filename,
   fclose(fp);
   if (read_length == 0)
     {
+    delete [] buffer;
     return SystemTools::FileTypeUnknown;
     }
 
diff --git a/testSystemTools.cxx b/testSystemTools.cxx
index 15d8eab1b10750b091388dd450f2fb3ecba9c126..67a90fa2b6134e1def7ada789f30f80b82333827 100644
--- a/testSystemTools.cxx
+++ b/testSystemTools.cxx
@@ -98,6 +98,8 @@ static bool CheckEscapeChars(kwsys_stl::string input,
 static bool CheckFileOperations()
 {
   bool res = true;
+  const kwsys_stl::string testNonExistingFile(TEST_SYSTEMTOOLS_SOURCE_DIR
+    "/testSystemToolsNonExistingFile");
   const kwsys_stl::string testBinFile(TEST_SYSTEMTOOLS_SOURCE_DIR
     "/testSystemTools.bin");
   const kwsys_stl::string testTxtFile(TEST_SYSTEMTOOLS_SOURCE_DIR
@@ -106,6 +108,15 @@ static bool CheckFileOperations()
     "/testSystemToolsNewDir");
   const kwsys_stl::string testNewFile(testNewDir + "/testNewFile.txt");
 
+  if (kwsys::SystemTools::DetectFileType(testNonExistingFile.c_str()) !=
+      kwsys::SystemTools::FileTypeUnknown)
+    {
+    kwsys_ios::cerr
+      << "Problem with DetectFileType - failed to detect type of: "
+      << testNonExistingFile << kwsys_ios::endl;
+    res = false;
+    }
+
   if (kwsys::SystemTools::DetectFileType(testBinFile.c_str()) !=
       kwsys::SystemTools::FileTypeBinary)
     {