From c2c35e8fb367b9aa6db7bd0304e3352a6dbf87f4 Mon Sep 17 00:00:00 2001 From: Philippe Canal Date: Mon, 7 Nov 2016 08:38:50 -0600 Subject: [PATCH] Properly handle I/O of pointer to std::string. Like the STL collection a nullptr std::string is stored the same as a pointer to an 'empty' std::string. --- core/base/inc/TBuffer.h | 4 ++-- core/base/src/String.cxx | 4 ++-- io/io/inc/TBufferFile.h | 4 ++-- io/io/inc/TBufferJSON.h | 4 ++-- io/io/src/TBufferFile.cxx | 16 +++++++++++----- io/io/src/TBufferJSON.cxx | 8 +++++--- io/sql/inc/TBufferSQL2.h | 4 ++-- io/sql/src/TBufferSQL2.cxx | 4 ++-- io/xml/inc/TBufferXML.h | 4 ++-- io/xml/src/TBufferXML.cxx | 9 +++++---- tree/tree/inc/TBufferSQL.h | 4 ++-- tree/tree/src/TBufferSQL.cxx | 4 ++-- 12 files changed, 39 insertions(+), 30 deletions(-) diff --git a/core/base/inc/TBuffer.h b/core/base/inc/TBuffer.h index 1a1ec15dba6b4..1319662a6cb79 100644 --- a/core/base/inc/TBuffer.h +++ b/core/base/inc/TBuffer.h @@ -279,7 +279,7 @@ class TBuffer : public TObject { virtual void ReadDouble(Double_t &d) = 0; virtual void ReadCharP(Char_t *c) = 0; virtual void ReadTString(TString &s) = 0; - virtual void ReadStdString(std::string &s) = 0; + virtual void ReadStdString(std::string *s) = 0; virtual void WriteBool(Bool_t b) = 0; virtual void WriteChar(Char_t c) = 0; @@ -296,7 +296,7 @@ class TBuffer : public TObject { virtual void WriteDouble(Double_t d) = 0; virtual void WriteCharP(const Char_t *c) = 0; virtual void WriteTString(const TString &s) = 0; - virtual void WriteStdString(const std::string &s) = 0; + virtual void WriteStdString(const std::string *s) = 0; // Special basic ROOT objects and collections virtual TProcessID *GetLastProcessID(TRefTable *reftable) const = 0; diff --git a/core/base/src/String.cxx b/core/base/src/String.cxx index d428e3e786473..0da80eb63a227 100644 --- a/core/base/src/String.cxx +++ b/core/base/src/String.cxx @@ -25,9 +25,9 @@ void std_string_streamer(TBuffer &b, void *objadd) { // Streamer function for std::string object. if (b.IsReading()) { - b.ReadStdString(*(std::string*)objadd); + b.ReadStdString((std::string*)objadd); } else { - b.WriteStdString(*(std::string*)objadd); + b.WriteStdString((std::string*)objadd); } } diff --git a/io/io/inc/TBufferFile.h b/io/io/inc/TBufferFile.h index c06bb42efc004..a9c5c84179141 100644 --- a/io/io/inc/TBufferFile.h +++ b/io/io/inc/TBufferFile.h @@ -272,7 +272,7 @@ class TBufferFile : public TBuffer { virtual void ReadDouble(Double_t &d); virtual void ReadCharP(Char_t *c); virtual void ReadTString(TString &s); - virtual void ReadStdString(std::string &s); + virtual void ReadStdString(std::string *s); virtual void WriteBool(Bool_t b); virtual void WriteChar(Char_t c); @@ -289,7 +289,7 @@ class TBufferFile : public TBuffer { virtual void WriteDouble(Double_t d); virtual void WriteCharP(const Char_t *c); virtual void WriteTString(const TString &s); - virtual void WriteStdString(const std::string &s); + virtual void WriteStdString(const std::string *s); // Special basic ROOT objects and collections virtual TProcessID *GetLastProcessID(TRefTable *reftable) const; diff --git a/io/io/inc/TBufferJSON.h b/io/io/inc/TBufferJSON.h index b08c84636c2a2..01f3526b5c534 100644 --- a/io/io/inc/TBufferJSON.h +++ b/io/io/inc/TBufferJSON.h @@ -201,7 +201,7 @@ class TBufferJSON : public TBuffer { virtual void ReadDouble(Double_t &d); virtual void ReadCharP(Char_t *c); virtual void ReadTString(TString &s); - virtual void ReadStdString(std::string &s); + virtual void ReadStdString(std::string *s); virtual void WriteBool(Bool_t b); virtual void WriteChar(Char_t c); @@ -218,7 +218,7 @@ class TBufferJSON : public TBuffer { virtual void WriteDouble(Double_t d); virtual void WriteCharP(const Char_t *c); virtual void WriteTString(const TString &s); - virtual void WriteStdString(const std::string &s); + virtual void WriteStdString(const std::string *s); virtual Int_t WriteClones(TClonesArray *a, Int_t nobjects); diff --git a/io/io/src/TBufferFile.cxx b/io/io/src/TBufferFile.cxx index 96d021ce0d384..05278e06882e9 100644 --- a/io/io/src/TBufferFile.cxx +++ b/io/io/src/TBufferFile.cxx @@ -295,9 +295,11 @@ void TBufferFile::WriteTString(const TString &s) //////////////////////////////////////////////////////////////////////////////// /// Read std::string from TBuffer. -void TBufferFile::ReadStdString(std::string &s) +void TBufferFile::ReadStdString(std::string *obj) { - std::string *obj = &s; + if (obj == 0) { + Error("TBufferFile::ReadStdString","The std::string address is nullptr but should not"); + } Int_t nbig; UChar_t nwh; *this >> nwh; @@ -323,10 +325,14 @@ void TBufferFile::ReadStdString(std::string &s) //////////////////////////////////////////////////////////////////////////////// /// Write std::string to TBuffer. -void TBufferFile::WriteStdString(const std::string &s) +void TBufferFile::WriteStdString(const std::string *obj) { - if (s==0) return; - const std::string *obj = &s; + if (obj==0) { + *this << (UChar_t)0; + WriteFastArray("",0); + return; + } + UChar_t nwh; Int_t nbig = obj->length(); if (nbig > 254) { diff --git a/io/io/src/TBufferJSON.cxx b/io/io/src/TBufferJSON.cxx index 49ab26ba20273..81753f8554c71 100644 --- a/io/io/src/TBufferJSON.cxx +++ b/io/io/src/TBufferJSON.cxx @@ -2658,7 +2658,7 @@ void TBufferJSON::ReadTString(TString & /*s*/) //////////////////////////////////////////////////////////////////////////////// /// Reads a std::string -void TBufferJSON::ReadStdString(std::string &/*s*/) +void TBufferJSON::ReadStdString(std::string */*s*/) { } @@ -2815,11 +2815,13 @@ void TBufferJSON::WriteTString(const TString &s) //////////////////////////////////////////////////////////////////////////////// /// Writes a std::string -void TBufferJSON::WriteStdString(const std::string &s) +void TBufferJSON::WriteStdString(const std::string *s) { TJSONPushValue(); - JsonWriteConstChar(s.c_str(), s.length()); + if (s) JsonWriteConstChar(s->c_str(), s->length()); + else JsonWriteConstChar("",0); + } //////////////////////////////////////////////////////////////////////////////// diff --git a/io/sql/inc/TBufferSQL2.h b/io/sql/inc/TBufferSQL2.h index 1a5a731145007..61acdaf013036 100644 --- a/io/sql/inc/TBufferSQL2.h +++ b/io/sql/inc/TBufferSQL2.h @@ -278,7 +278,7 @@ friend class TSQLStructure; virtual void ReadDouble(Double_t &d); virtual void ReadCharP(Char_t *c); virtual void ReadTString(TString &s); - virtual void ReadStdString(std::string &s); + virtual void ReadStdString(std::string *s); virtual void WriteBool(Bool_t b); virtual void WriteChar(Char_t c); @@ -295,7 +295,7 @@ friend class TSQLStructure; virtual void WriteDouble(Double_t d); virtual void WriteCharP(const Char_t *c); virtual void WriteTString(const TString &s); - virtual void WriteStdString(const std::string &s); + virtual void WriteStdString(const std::string *s); virtual Int_t ApplySequence(const TStreamerInfoActions::TActionSequence &sequence, void *object); virtual Int_t ApplySequenceVecPtr(const TStreamerInfoActions::TActionSequence &sequence, void *start_collection, void *end_collection); diff --git a/io/sql/src/TBufferSQL2.cxx b/io/sql/src/TBufferSQL2.cxx index c6065a54dc3ca..ab13760890511 100644 --- a/io/sql/src/TBufferSQL2.cxx +++ b/io/sql/src/TBufferSQL2.cxx @@ -2239,7 +2239,7 @@ void TBufferSQL2::WriteTString(const TString &s) //////////////////////////////////////////////////////////////////////////////// /// Read a std::string -void TBufferSQL2::ReadStdString(std::string &s) +void TBufferSQL2::ReadStdString(std::string *s) { TBufferFile::ReadStdString(s); } @@ -2247,7 +2247,7 @@ void TBufferSQL2::ReadStdString(std::string &s) //////////////////////////////////////////////////////////////////////////////// /// Write a std::string -void TBufferSQL2::WriteStdString(const std::string &s) +void TBufferSQL2::WriteStdString(const std::string *s) { TBufferFile::WriteStdString(s); } diff --git a/io/xml/inc/TBufferXML.h b/io/xml/inc/TBufferXML.h index 669262a90c5b2..7f741ed1f57c3 100644 --- a/io/xml/inc/TBufferXML.h +++ b/io/xml/inc/TBufferXML.h @@ -205,7 +205,7 @@ friend class TKeyXML; virtual void ReadDouble(Double_t &d); virtual void ReadCharP(Char_t *c); virtual void ReadTString(TString &s); - virtual void ReadStdString(std::string &s); + virtual void ReadStdString(std::string *s); virtual void WriteBool(Bool_t b); virtual void WriteChar(Char_t c); @@ -222,7 +222,7 @@ friend class TKeyXML; virtual void WriteDouble(Double_t d); virtual void WriteCharP(const Char_t *c); virtual void WriteTString(const TString &s); - virtual void WriteStdString(const std::string &s); + virtual void WriteStdString(const std::string *s); virtual Int_t ApplySequence(const TStreamerInfoActions::TActionSequence &sequence, void *object); virtual Int_t ApplySequenceVecPtr(const TStreamerInfoActions::TActionSequence &sequence, void *start_collection, void *end_collection); diff --git a/io/xml/src/TBufferXML.cxx b/io/xml/src/TBufferXML.cxx index e1c5cf03c9a37..1d51bce514a72 100644 --- a/io/xml/src/TBufferXML.cxx +++ b/io/xml/src/TBufferXML.cxx @@ -2724,7 +2724,7 @@ void TBufferXML::ReadTString(TString &s) //////////////////////////////////////////////////////////////////////////////// /// Reads a std::string -void TBufferXML::ReadStdString(std::string &s) +void TBufferXML::ReadStdString(std::string *s) { if (GetIOVersion()<3) { TBufferFile::ReadStdString(s); @@ -2732,7 +2732,7 @@ void TBufferXML::ReadStdString(std::string &s) BeforeIOoperation(); const char* buf; if ((buf = XmlReadValue(xmlio::String))) - s = buf; + if (s) *s = buf; } } @@ -2873,13 +2873,14 @@ void TBufferXML::WriteTString(const TString &s) //////////////////////////////////////////////////////////////////////////////// /// Writes a TString -void TBufferXML::WriteStdString(const std::string &s) +void TBufferXML::WriteStdString(const std::string *s) { if (GetIOVersion()<3) { TBufferFile::WriteStdString(s); } else { BeforeIOoperation(); - XmlWriteValue(s.c_str(), xmlio::String); + if (s) XmlWriteValue(s->c_str(), xmlio::String); + else XmlWriteValue("", xmlio::String); } } diff --git a/tree/tree/inc/TBufferSQL.h b/tree/tree/inc/TBufferSQL.h index 3cbd5c2f300fe..48d8161b0bba5 100644 --- a/tree/tree/inc/TBufferSQL.h +++ b/tree/tree/inc/TBufferSQL.h @@ -68,7 +68,7 @@ class TBufferSQL : public TBufferFile { virtual void ReadDouble(Double_t &d); virtual void ReadCharP(Char_t *c); virtual void ReadTString(TString &s); - virtual void ReadStdString(std::string &s); + virtual void ReadStdString(std::string *s); virtual void WriteBool(Bool_t b); virtual void WriteChar(Char_t c); @@ -85,7 +85,7 @@ class TBufferSQL : public TBufferFile { virtual void WriteDouble(Double_t d); virtual void WriteCharP(const Char_t *c); virtual void WriteTString(const TString &s); - virtual void WriteStdString(const std::string &s); + virtual void WriteStdString(const std::string *s); virtual void WriteFastArray(const Bool_t *b, Int_t n); diff --git a/tree/tree/src/TBufferSQL.cxx b/tree/tree/src/TBufferSQL.cxx index b5e344e05f91e..98da06e6dbf1a 100644 --- a/tree/tree/src/TBufferSQL.cxx +++ b/tree/tree/src/TBufferSQL.cxx @@ -311,7 +311,7 @@ void TBufferSQL::WriteTString(const TString &s) //////////////////////////////////////////////////////////////////////////////// /// Read a std::string -void TBufferSQL::ReadStdString(std::string &s) +void TBufferSQL::ReadStdString(std::string *s) { TBufferFile::ReadStdString(s); } @@ -319,7 +319,7 @@ void TBufferSQL::ReadStdString(std::string &s) //////////////////////////////////////////////////////////////////////////////// /// Write a std::string -void TBufferSQL::WriteStdString(const std::string &s) +void TBufferSQL::WriteStdString(const std::string *s) { TBufferFile::WriteStdString(s); }