[EMAIL PROTECTED] wrote:
> // widen function
> // Converts ASCII std::string to XMLCh array.
> // NOTE: Caller must delete XMLCh array when finished with it.
> XMLCh* widen(const string& src)
> {
>     // Allocate buffer.
>     XMLCh* dest = new XMLCh[src.length() + 1];
>
>     // Copy from string to buffer.
>     string::size_type i;
>     for (i=0; i < src.length(); ++i)
>         dest[i]=(XMLCh)src[i];
>
>     // Add NULL byte.
>     dest[i+1]=0;
>
>     // Return const pointer to array.
>     return dest;
> }

Hi Matthew,

pay attention! This function casually will crash!

Variable dest points to an array of src.length()+1 elements. After the 
for-loop has finished, i has the value src.length(), and with the next 
statement you access dest[src.length()+1], which is behind the allocated 
array.

In order to explicitly add a NULL byte, you should say
        dest[i] = 0;

Cheers,
                        Axel

-- 
Humboldt-Universität zu Berlin
Institut für Informatik
Signalverarbeitung und Mustererkennung
Dipl.-Inf. Axel Weiß
Rudower Chaussee 25
12489 Berlin-Adlershof
+49-30-2093-3050
** www.freesp.de **

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to