edgar-mtz-e / slimdx

Automatically exported from code.google.com/p/slimdx
0 stars 0 forks source link

Undefined behavior in BaseEffect::GetValue #326

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In BaseEffect::GetValue an auto_ptr is used with an array:

std::auto_ptr<char> data( new char[bytes] );

I'm quite certain this leads to undefined behavior as the dtor of auto_ptr
will call delete, not delete[] on the data. Normally a std::vector(bytes)
would be the correct alternative, however, the DataStream needs to take
over the ownership of the buffer later in the code. The following would be
a working, though not very elegant alternative:

DataStream^ BaseEffect::GetValue( EffectHandle^ parameter, int bytes )
{
    D3DXHANDLE handle = parameter != nullptr ? parameter->InternalHandle :
NULL;
    char * data = new char[bytes];
    try 
    {

        HRESULT hr = InternalPointer->GetValue( handle, data, bytes );

        if( RECORD_D3D9( hr ).IsFailure )
        {
            return nullptr;
        }

        DataStream^ ds = gcnew DataStream( data, bytes, true, true, false );            
        ds->TakeOwnership(); 
        data = NULL; 
        return ds;
    } 
    finally 
    {
        delete[] data; // data == null if DataStream was created.
    }
}

Original issue reported on code.google.com by m...@andreloker.de on 28 Aug 2008 at 2:21

GoogleCodeExporter commented 9 years ago
That's an odd one. I'll fix it up.

Original comment by Mike.Popoloski on 28 Aug 2008 at 5:17

GoogleCodeExporter commented 9 years ago
Fixed.

Original comment by Mike.Popoloski on 29 Aug 2008 at 1:24

GoogleCodeExporter commented 9 years ago

Original comment by Mike.Popoloski on 29 Aug 2008 at 1:24