[odb-users] libodb-mssql wstring possible memory issue

Candy Chiu candy.chiu.ad at gmail.com
Fri Oct 26 13:48:05 EDT 2012


Boris,

I am going through how wchar_t strings are handled.  In
odb/mssql/traits.hxx, line 803,

    template <>
    struct LIBODB_MSSQL_EXPORT default_value_traits<std::wstring,
id_nstring>
    {
      static void
      set_image (ucs2_char* b,
                 std::size_t c,
                 std::size_t& n,
                 bool& is_null,
                 const std::wstring& v)
      {
        is_null = false;
        n = v.size ();

        if (n > c)  // <------------- if (n * sizeof(ucs2_char) > c) ...
          n = c;

        functions::assign (b, v.c_str (), n);
      }
    };

Suppose the capacity of the buffer is the same as the length of the string
(n=c>0).  In traits.hxx, line 775,

      static void
      assign (ucs2_char* b, const wchar_t* s, std::size_t n)
      {
        if (n != 0)
          std::memcpy (b, s, n * sizeof (ucs2_char));
      }

memcpy copys 2n bytes of data into a n byte buffer.


More information about the odb-users mailing list