[omniORB] CORBA::string_alloc(len) problem

Stephen Coy coys@whitesmiths.com.au
Thu, 15 Jul 1999 18:27:05 +1000


G'Day,

You said:

> 
> I want to describe a problem with OmniORB's string_alloc() when used in
> combination with the String_var's index operator (i.e operator[] ) and a
> simple fix for the string_alloc() implementation which will reduce the
> number of problems.
>
> Say we have the following code:
>
>   String_var myStr = CORBA::string_alloc(100);
>
>   myStr[0] = 'a';
>   myStr[1] = 'b';   // String_var operator[] broken from here
>   myStr[2] = '\0';
>
> The operator[] is potentially broken from the second assignment because
> in its implementation, it tries to do a strlen on a temporarily non-null
> terminated string that contains uninitialised characters ...
>
> Anyway, the fix I suggest for the CORBA::string_alloc(len)
> implementation is instead of zeroing just the first char, zero the
> entire allocated block, e.g.:
>     memset(s, 0, (len + 1) * sizeof(char))
>

I must disagree with you here. I don't believe that CORBA::string_alloc
should perform any more initialisation than, say, malloc or operator new.
The omniORB implementation of CORBA::string_alloc kindly installs a NUL in
the first byte of the returned string, but I'm not sure that it's required
to do even this much.

The CORBA 2.2 spec seems to be a bit vague about allowed uses of
String_var::operator[], but the Henning/Vinoski book* states that "Attempts
to index beyond the NUL terminator result in undefined behaviour" on p160.

Relying on the behaviour provided by adding the memset call above will:
a) result in non-portable code;
b) force other users of omniORB to pay the cost of the memset.

You probably need to do something like:

    char * myStr = CORBA::string_alloc(100);
    String_var myStrVar = myStr;
    myStr[0] = 'a';
    myStr[1] = 'b';
    myStr[2] = '\0';

Regards,

Steve Coy
Whitesmiths

* Advanced CORBA Programming with C++