]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Make InspIRCd::Format return std::string instead of const char*.
authorPeter Powell <petpow@saberuk.com>
Mon, 1 Jan 2018 23:56:35 +0000 (23:56 +0000)
committerPeter Powell <petpow@saberuk.com>
Wed, 3 Jan 2018 12:38:40 +0000 (12:38 +0000)
Using the latter is problematic as if you don't copy the return
value before calling Format again your formatted message will be
overwritten by something else. This bug was observed in m_callerid
where InspIRCd::Format was being used for formatting two arguments
the latter of which was being overwritten with the former.

We could have preserved the return type and just copied the string
but then callers would have had to deallocate the string once they
have finished with it which is an undesirabable burden to put on
callers.

include/inspircd.h
include/inspstring.h
include/modules/sql.h
src/helperfuncs.cpp

index 40c368106260f92dffddeec2113e6cbf610ac418..bdbcdb20df002b356d6c3f55cca0bb21d6845001 100644 (file)
@@ -407,8 +407,8 @@ class CoreExport InspIRCd
        * @param ... A variable number of format arguments.
        * @return The formatted string
        */
-       static const char* Format(const char* formatString, ...) CUSTOM_PRINTF(1, 2);
-       static const char* Format(va_list &vaList, const char* formatString) CUSTOM_PRINTF(2, 0);
+       static std::string Format(const char* formatString, ...) CUSTOM_PRINTF(1, 2);
+       static std::string Format(va_list& vaList, const char* formatString) CUSTOM_PRINTF(2, 0);
 
        /** Determines whether a nickname is valid. */
        TR1NS::function<bool(const std::string&)> IsNick;
index ccc77da663156644b8723c34d6b998408ab07987..2501b76ce85baf339f0ce70fab71626f65719af8 100644 (file)
@@ -29,7 +29,7 @@
        do { \
        va_list _vaList; \
        va_start(_vaList, last); \
-       ret = InspIRCd::Format(_vaList, format); \
+       ret.assign(InspIRCd::Format(_vaList, format)); \
        va_end(_vaList); \
        } while (false);
 
index dd33fc6761201bf84c93837fd075e2048a2b3096..01adbb42e587e1d540f4c6309c28a002d46aa6ab 100644 (file)
@@ -132,7 +132,7 @@ class SQL::Error
 {
  private:
        /** The custom error message if one has been specified. */
-       const char* message;
+       const std::string message;
 
  public:
        /** The code which represents this error. */
@@ -142,8 +142,7 @@ class SQL::Error
         * @param c A code which represents this error.
         */
        Error(ErrorCode c)
-               : message(NULL)
-               , code(c)
+               : code(c)
        {
        }
 
@@ -151,7 +150,7 @@ class SQL::Error
         * @param c A code which represents this error.
         * @param m A custom error message.
         */
-       Error(ErrorCode c, const char* m)
+       Error(ErrorCode c, const std::string m)
                : message(m)
                , code(c)
        {
@@ -160,8 +159,8 @@ class SQL::Error
        /** Retrieves the error message. */
        const char* ToString() const
        {
-               if (message)
-                       return message;
+               if (!message.empty())
+                       return message.c_str();
 
                switch (code)
                {
index c29e7d2cc65e6d7bfd28897a2be68b2f4a23640c..e331ba9dc6c9b2bf980df1ba1607dd94602c5b01 100644 (file)
@@ -331,7 +331,7 @@ unsigned long InspIRCd::Duration(const std::string &str)
        return total + subtotal;
 }
 
-const char* InspIRCd::Format(va_list &vaList, const char* formatString)
+std::string InspIRCd::Format(va_list& vaList, const char* formatString)
 {
        static std::vector<char> formatBuffer(1024);
 
@@ -351,12 +351,12 @@ const char* InspIRCd::Format(va_list &vaList, const char* formatString)
                formatBuffer.resize(formatBuffer.size() * 2);
        }
 
-       return &formatBuffer[0];
+       return std::string(&formatBuffer[0]);
 }
 
-const char* InspIRCd::Format(const char* formatString, ...)
+std::string InspIRCd::Format(const char* formatString, ...)
 {
-       const char* ret;
+       std::string ret;
        VAFORMAT(ret, formatString, formatString);
        return ret;
 }