stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anton Pevtsov" <>
Subject Problems with 0-lengths strings after an exception thrown in 11s configuration
Date Fri, 31 Mar 2006 16:19:05 GMT
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

#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
                   sz, str, str_len);

    charT* out = new charT [str_len + 1];

    try {
        if (bthrow)
            s.insert (10, wstr, str_len);
            s.insert (0, wstr, str_len);
    catch (std::out_of_range& e) {
        rw_printf ("out_of_range cougth\n");
    catch (bad_alloc& 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?

Anton Pevtsov

View raw message