wesnoth-cvs-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Wesnoth-cvs-commits] wesnoth/src/serialization preprocessor.cpp


From: Guillaume Melquiond
Subject: [Wesnoth-cvs-commits] wesnoth/src/serialization preprocessor.cpp
Date: Tue, 12 Jul 2005 16:33:19 -0400

CVSROOT:        /cvsroot/wesnoth
Module name:    wesnoth
Branch:         
Changes by:     Guillaume Melquiond <address@hidden>    05/07/12 20:33:19

Modified files:
        src/serialization: preprocessor.cpp 

Log message:
        Fix GCC warning about dubious copy constructor by explicitly specifying 
that the base class is not copied. Speaking about the copy constructor, because 
the default_defines is not copied and the pointer is not updated, it would lead 
to a use-after-free memory access if the buffer was to be destroyed. I can't 
think of any normal situation where this would happen, but better safe than 
sorry. So let's push the ownership one level upward. As a nice side-effect, the 
preproc_map is no more constructed/destructed when useless.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/serialization/preprocessor.cpp.diff?tr1=1.23&tr2=1.24&r1=text&r2=text

Patches:
Index: wesnoth/src/serialization/preprocessor.cpp
diff -u wesnoth/src/serialization/preprocessor.cpp:1.23 
wesnoth/src/serialization/preprocessor.cpp:1.24
--- wesnoth/src/serialization/preprocessor.cpp:1.23     Sun Jul  3 20:14:42 2005
+++ wesnoth/src/serialization/preprocessor.cpp  Tue Jul 12 20:33:19 2005
@@ -1,4 +1,4 @@
-/* $Id: preprocessor.cpp,v 1.23 2005/07/03 20:14:42 ott Exp $ */
+/* $Id: preprocessor.cpp,v 1.24 2005/07/12 20:33:19 silene Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Copyright (C) 2005 by Guillaume Melquiond <address@hidden>
@@ -87,13 +87,10 @@
        : current_(NULL), defines_(def), textdomain_(PACKAGE),
          depth_(0), quoted_(false)
 {
-       if(defines_ == NULL) {
-               defines_ = &default_defines_;
-       }
 }
 
 preprocessor_streambuf::preprocessor_streambuf(preprocessor_streambuf const &t)
-       : current_(NULL), defines_(t.defines_),
+       : std::streambuf(), current_(NULL), defines_(t.defines_),
          textdomain_(PACKAGE), depth_(t.depth_), quoted_(t.quoted_)
 {
 }
@@ -716,15 +713,36 @@
 struct preprocessor_deleter: std::basic_istream<char>
 {
        preprocessor_streambuf *buf_;
-       preprocessor_deleter(preprocessor_streambuf *buf) : 
std::basic_istream<char>(buf), buf_(buf) {}
-       ~preprocessor_deleter() { rdbuf(NULL); delete buf_; }
+       preproc_map *defines_;
+       preprocessor_deleter(preprocessor_streambuf *buf, preproc_map *defines);
+       ~preprocessor_deleter();
 };
 
+preprocessor_deleter::preprocessor_deleter(preprocessor_streambuf *buf, 
preproc_map *defines)
+       : std::basic_istream<char>(buf), buf_(buf), defines_(defines)
+{
+}
+
+preprocessor_deleter::~preprocessor_deleter()
+{
+       rdbuf(NULL);
+       delete buf_;
+       delete defines_;
+}
+
+
 std::istream *preprocess_file(std::string const &fname,
                               preproc_map *defines)
 {
        log_scope("preprocessing file...");
+       preproc_map *owned_defines = NULL;
+       if (!defines) {
+               // if no preproc_map has been given, create a new one, and 
ensure
+               // it is destroyed when the stream is by giving it to the 
deleter
+               owned_defines = new preproc_map;
+               defines = owned_defines;
+       }
        preprocessor_streambuf *buf = new preprocessor_streambuf(defines);
        new preprocessor_file(*buf, fname);
-       return new preprocessor_deleter(buf);
+       return new preprocessor_deleter(buf, owned_defines);
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]