Proposed fix for coverity #29372
authorTobias Markus <tobbi@mozilla-uk.org>
Sat, 28 Feb 2015 12:53:47 +0000 (13:53 +0100)
committerTobias Markus <tobbi@mozilla-uk.org>
Sat, 28 Feb 2015 12:53:47 +0000 (13:53 +0100)
src/physfs/buffered_ifile_stream.cpp [new file with mode: 0644]
src/physfs/buffered_ifile_stream.hpp [new file with mode: 0644]
src/physfs/ifile_stream.cpp
src/physfs/ifile_stream.hpp
src/physfs/physfs_file_system.cpp
src/scripting/functions.cpp
src/scripting/scripting.cpp
src/supertux/console.cpp

diff --git a/src/physfs/buffered_ifile_stream.cpp b/src/physfs/buffered_ifile_stream.cpp
new file mode 100644 (file)
index 0000000..85eee6c
--- /dev/null
@@ -0,0 +1,43 @@
+//  SuperTux
+//  Copyright (C) 2015 Tobias Markus <tobbi@mozilla-uk.org>
+//
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_SUPERTUX_PHYSFS_BUFFERED_IFILE_STREAM_CPP
+#define HEADER_SUPERTUX_PHYSFS_BUFFERED_IFILE_STREAM_CPP
+
+#include "physfs/buffered_ifile_stream.hpp"
+
+BufferedIFileStream::BufferedIFileStream(const std::string& filename)
+{
+  buffer = new IFileStreambuf(filename);
+  stream = new IFileStream(buffer);
+}
+
+BufferedIFileStream::~BufferedIFileStream()
+{
+  delete buffer;
+  delete stream;
+  buffer = NULL;
+  stream = NULL;
+}
+
+IFileStream* BufferedIFileStream::get_stream()
+{
+  return stream;
+}
+
+#endif
+
+/* EOF */
diff --git a/src/physfs/buffered_ifile_stream.hpp b/src/physfs/buffered_ifile_stream.hpp
new file mode 100644 (file)
index 0000000..578dd48
--- /dev/null
@@ -0,0 +1,39 @@
+//  SuperTux
+//  Copyright (C) 2015 Tobias Markus <tobbi@mozilla-uk.org>
+//
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_SUPERTUX_PHYSFS_BUFFERED_IFILE_STREAM_HPP
+#define HEADER_SUPERTUX_PHYSFS_BUFFERED_IFILE_STREAM_HPP
+
+#include <ostream>
+#include <physfs.h>
+#include "physfs/ifile_stream.hpp"
+#include "physfs/ifile_streambuf.hpp"
+
+class BufferedIFileStream {
+
+private:
+  IFileStream* stream;
+  IFileStreambuf* buffer;
+
+public:
+  BufferedIFileStream(const std::string& filename);
+  ~BufferedIFileStream();
+  
+  IFileStream* get_stream();
+};
+#endif
+
+/* EOF */
index 4a09394..f4cb889 100644 (file)
 
 #include "physfs/ifile_stream.hpp"
 
-#include "physfs/ifile_streambuf.hpp"
-
-IFileStream::IFileStream(const std::string& filename) :
-  std::istream(new IFileStreambuf(filename))
+IFileStream::IFileStream(IFileStreambuf* buf) :
+  std::istream(buf)
 {
 }
 
index 263e44d..f160d1c 100644 (file)
 #include <istream>
 #include <physfs.h>
 
+#include "physfs/ifile_streambuf.hpp"
+
 class IFileStream : public std::istream
 {
 public:
-  IFileStream(const std::string& filename);
+  IFileStream(IFileStreambuf* buf);
   ~IFileStream();
 };
 
index 15d9f76..65ac067 100644 (file)
@@ -16,7 +16,7 @@
 
 #include "physfs/physfs_file_system.hpp"
 
-#include "physfs/ifile_stream.hpp"
+#include "physfs/buffered_ifile_stream.hpp"
 
 PhysFSFileSystem::PhysFSFileSystem()
 {
@@ -40,7 +40,8 @@ PhysFSFileSystem::open_directory(const std::string& pathname)
 std::unique_ptr<std::istream>
 PhysFSFileSystem::open_file(const std::string& filename)
 {
-  return std::unique_ptr<std::istream>(new IFileStream(filename));
+  BufferedIFileStream* stream = new BufferedIFileStream(filename);
+  return std::unique_ptr<std::istream>(stream->get_stream());
 }
 
 /* EOF */
index 893191b..2c906ff 100644 (file)
@@ -20,7 +20,7 @@
 #include "math/random_generator.hpp"
 #include "object/camera.hpp"
 #include "object/player.hpp"
-#include "physfs/ifile_stream.hpp"
+#include "physfs/buffered_ifile_stream.hpp"
 #include "supertux/fadeout.hpp"
 #include "supertux/game_session.hpp"
 #include "supertux/gameconfig.hpp"
@@ -127,7 +127,8 @@ void load_level(const std::string& filename)
 
 void import(HSQUIRRELVM vm, const std::string& filename)
 {
-  IFileStream in(filename);
+  BufferedIFileStream* stream = new BufferedIFileStream(filename);
+  IFileStream* in = stream->get_stream();
 
   if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
                           filename.c_str(), SQTrue)))
index 1e27640..b19c13b 100644 (file)
@@ -23,7 +23,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 
-#include "physfs/ifile_stream.hpp"
+#include "physfs/buffered_ifile_stream.hpp"
 #include "scripting/squirrel_error.hpp"
 #include "scripting/wrapper.hpp"
 #include "squirrel_util.hpp"
@@ -106,8 +106,9 @@ Scripting::Scripting(bool enable_debugger)
   // try to load default script
   try {
     std::string filename = "scripts/default.nut";
-    IFileStream stream(filename);
-    scripting::compile_and_run(global_vm, stream, filename);
+    BufferedIFileStream* buffered_stream = new BufferedIFileStream(filename);
+    IFileStream* stream = buffered_stream->get_stream();
+    scripting::compile_and_run(global_vm, *stream, filename);
   } catch(std::exception& e) {
     log_warning << "Couldn't load default.nut: " << e.what() << std::endl;
   }
index 1d27bc8..7244191 100644 (file)
@@ -19,7 +19,7 @@
 #include <math.h>
 #include <iostream>
 
-#include "physfs/ifile_stream.hpp"
+#include "physfs/buffered_ifile_stream.hpp"
 #include "scripting/scripting.hpp"
 #include "scripting/squirrel_util.hpp"
 #include "supertux/gameconfig.hpp"
@@ -175,8 +175,9 @@ Console::ready_vm()
 
     try {
       std::string filename = "scripts/console.nut";
-      IFileStream stream(filename);
-      scripting::compile_and_run(m_vm, stream, filename);
+      BufferedIFileStream* buffered_stream = new BufferedIFileStream(filename);
+      IFileStream* stream = buffered_stream->get_stream();
+      scripting::compile_and_run(m_vm, *stream, filename);
     } catch(std::exception& e) {
       log_warning << "Couldn't load console.nut: " << e.what() << std::endl;
     }