From c0b5cfa3eadebef8101f87cd593eb221bdef9280 Mon Sep 17 00:00:00 2001 From: Tobias Markus Date: Sat, 28 Feb 2015 13:53:47 +0100 Subject: [PATCH] Proposed fix for coverity #29372 --- src/physfs/buffered_ifile_stream.cpp | 43 ++++++++++++++++++++++++++++++++++++ src/physfs/buffered_ifile_stream.hpp | 39 ++++++++++++++++++++++++++++++++ src/physfs/ifile_stream.cpp | 6 ++--- src/physfs/ifile_stream.hpp | 4 +++- src/physfs/physfs_file_system.cpp | 5 +++-- src/scripting/functions.cpp | 5 +++-- src/scripting/scripting.cpp | 7 +++--- src/supertux/console.cpp | 7 +++--- 8 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 src/physfs/buffered_ifile_stream.cpp create mode 100644 src/physfs/buffered_ifile_stream.hpp diff --git a/src/physfs/buffered_ifile_stream.cpp b/src/physfs/buffered_ifile_stream.cpp new file mode 100644 index 000000000..85eee6c4f --- /dev/null +++ b/src/physfs/buffered_ifile_stream.cpp @@ -0,0 +1,43 @@ +// SuperTux +// Copyright (C) 2015 Tobias Markus +// +// 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 . + +#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 index 000000000..578dd48dd --- /dev/null +++ b/src/physfs/buffered_ifile_stream.hpp @@ -0,0 +1,39 @@ +// SuperTux +// Copyright (C) 2015 Tobias Markus +// +// 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 . + +#ifndef HEADER_SUPERTUX_PHYSFS_BUFFERED_IFILE_STREAM_HPP +#define HEADER_SUPERTUX_PHYSFS_BUFFERED_IFILE_STREAM_HPP + +#include +#include +#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 */ diff --git a/src/physfs/ifile_stream.cpp b/src/physfs/ifile_stream.cpp index 4a09394dc..f4cb889e9 100644 --- a/src/physfs/ifile_stream.cpp +++ b/src/physfs/ifile_stream.cpp @@ -16,10 +16,8 @@ #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) { } diff --git a/src/physfs/ifile_stream.hpp b/src/physfs/ifile_stream.hpp index 263e44dbe..f160d1ca8 100644 --- a/src/physfs/ifile_stream.hpp +++ b/src/physfs/ifile_stream.hpp @@ -20,10 +20,12 @@ #include #include +#include "physfs/ifile_streambuf.hpp" + class IFileStream : public std::istream { public: - IFileStream(const std::string& filename); + IFileStream(IFileStreambuf* buf); ~IFileStream(); }; diff --git a/src/physfs/physfs_file_system.cpp b/src/physfs/physfs_file_system.cpp index 15d9f768c..65ac0677b 100644 --- a/src/physfs/physfs_file_system.cpp +++ b/src/physfs/physfs_file_system.cpp @@ -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 PhysFSFileSystem::open_file(const std::string& filename) { - return std::unique_ptr(new IFileStream(filename)); + BufferedIFileStream* stream = new BufferedIFileStream(filename); + return std::unique_ptr(stream->get_stream()); } /* EOF */ diff --git a/src/scripting/functions.cpp b/src/scripting/functions.cpp index 893191bb2..2c906ffb4 100644 --- a/src/scripting/functions.cpp +++ b/src/scripting/functions.cpp @@ -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))) diff --git a/src/scripting/scripting.cpp b/src/scripting/scripting.cpp index 1e27640f9..b19c13b6b 100644 --- a/src/scripting/scripting.cpp +++ b/src/scripting/scripting.cpp @@ -23,7 +23,7 @@ #include #include -#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; } diff --git a/src/supertux/console.cpp b/src/supertux/console.cpp index 1d27bc87b..7244191f4 100644 --- a/src/supertux/console.cpp +++ b/src/supertux/console.cpp @@ -19,7 +19,7 @@ #include #include -#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; } -- 2.11.0