The following code works incorrectly on Windows, compiler MSVC-7.1. But
the problem is unstable, so I am not sure that this test will fail
everywhere.
#include <rw_printf.h>
#include <rw_char.h>
#include <string>
#include <cstddef>
#include <stdexcept>
#include <new>
template <class charT, class Traits>
void test_insert (char* str, std::size_t str_len, bool bthrow)
{
typedef std::basic_string <charT, Traits,
std::allocator<charT> > String;
static charT wstr [100];
rw_widen (wstr, str, str_len);
String s (wstr, str_len);
std::size_t sz = s. size ();
if (sz != str_len)
rw_printf ("string size is %zu for string %{#*s}, expected
%zu\n",
sz, str, str_len);
charT* out = new charT [str_len + 1];
try {
if (bthrow)
s.insert (10, wstr, str_len);
else
s.insert (0, wstr, str_len);
}
catch (std::out_of_range& e) {
_RWSTD_UNUSED(e);
rw_printf ("out_of_range cougth\n");
}
catch (bad_alloc& e) {
_RWSTD_UNUSED(e);
rw_printf ("bad_alloc cougth\n");
}
catch (...) {
rw_printf ("unknown exception cougth\n");
}
delete[] out;
}
void main (int argc, char* argv[])
{
char empty[] = "";
char test[] = "ab\0cd";
test_insert<wchar_t, UserTraits<wchar_t> > (test, 5, true);
test_insert<wchar_t, UserTraits<wchar_t> > (empty, 0, false);
}
I receive the following output:
out_of_range cougth
string size is 677733217 for string (null), expected 0
bad_alloc cougth
The problem seems to be in the _RW::__nullref dereferencing - its fields
contains garbage.
I found that __nullref is declared as
extern _RWSTD_EXPORT unsigned long __nullref [];
And intialized as:
_RWSTD_EXPORT unsigned long __nullref [sizeof (StringRef) / 4 + 2];
Is it correct to cast it to _RW::__null_ref<_CharT, _Traits, _Allocator>
and use fileds of this class after the cast?
Also the sample works OK if I just throw out_of_range exception instead
of the s.insert (10, wstr, str_len); call...
Martin, could you look into this when you have a chance, please?
Thanks,
Anton Pevtsov