Ticket #5288: libs_libmythupnp-negative-returns.patch

File libs_libmythupnp-negative-returns.patch, 3.2 KB (added by Erik Hovland <erik@…>, 12 years ago)

reworks the member function so that it does not have a file race nor a unchecked open() call

  • libs/libmythupnp/httprequest.cpp

    Fixes any defect where a function can return a negative value and that
    
    From: Erik Hovland <erik@hovland.org>
    
    value is passed to something that cannot handle that negative value.
    ---
    
     libs/libmythupnp/httprequest.cpp |   23 ++++++++++-------------
     1 files changed, 10 insertions(+), 13 deletions(-)
    
    diff --git a/libs/libmythupnp/httprequest.cpp b/libs/libmythupnp/httprequest.cpp
    index 203433f..3347daf 100644
    a b long HTTPRequest::SendResponseFile( QString sFileName ) 
    315315    setsockopt( getSocketHandle(), SOL_TCP, TCP_CORK, &g_on, sizeof( g_on ));
    316316#endif
    317317
    318     if (QFile::exists( sFileName ))
     318    QFile respFile( sFileName );
     319
     320    if (respFile.open( QIODevice::ReadOnly ))
    319321    {
    320322
    321323        m_sResponseTypeText = TestMimeType( sFileName );
    long HTTPRequest::SendResponseFile( QString sFileName ) 
    324326        // Get File size
    325327        // ------------------------------------------------------------------
    326328
    327         struct stat st;
    328 
    329         if (stat( sFileName.ascii(), &st ) == 0)
    330             llSize = llEnd = st.st_size;
     329        llSize = llEnd = respFile.size();
    331330
    332331        m_nResponseStatus = 200;
    333332
    long HTTPRequest::SendResponseFile( QString sFileName ) 
    379378    else
    380379    {
    381380        VERBOSE(VB_UPNP,
    382                 QString("HTTPRequest::SendResponseFile(%1) - cannot find file!")
     381                QString("HTTPRequest::SendResponseFile(%1) - cannot open file!")
    383382                .arg(sFileName));
    384383        m_nResponseStatus = 404;
     384        return -1;
    385385    }
    386386
    387387    // -=>TODO: Should set "Content-Length: *" if file is still recording
    long HTTPRequest::SendResponseFile( QString sFileName ) 
    401401    if (( m_eType != RequestTypeHead ) && (llSize != 0))
    402402    {
    403403        __off64_t offset = llStart;
    404         int       file   = open( sFileName.ascii(), O_RDONLY | O_LARGEFILE );
    405404        ssize_t   sent   = 0; 
     405        int       file   = respFile.handle();
    406406
    407407        do
    408408        { 
    long HTTPRequest::SendResponseFile( QString sFileName ) 
    410410            // The loop is needed in any case. 
    411411
    412412            sent = sendfile64( getSocketHandle(), file, &offset,
    413                                 (size_t)(llSize > INT_MAX ? INT_MAX : llSize)); 
     413                               size_t( llSize > INT_MAX ? INT_MAX : llSize )); 
    414414
    415415            llSize  = llEnd - offset; 
    416             //VERBOSE(VB_UPNP, QString("SendResponseFile : --- size = %1, offset = %2, sent = %3").arg(llSize).arg(offset).arg(sent));
     416            //VERBOSE(VB_UPNP, QString("SendResponseFile : --- size = %1, offset = %2, sent = %3").arg(llSize).arg(offset).arg(sent));
    417417        }
    418418        while (( sent >= 0 ) && ( llSize > 0 )); 
    419419
    420420        if (sent == -1)
    421421        {
    422             VERBOSE(VB_UPNP,QString("SendResponseFile( %1 ) Error: %2 [%3]" )
     422            VERBOSE(VB_UPNP,QString( "SendResponseFile( %1 ) Error: %2 [%3]" )
    423423                               .arg( sFileName )
    424424                               .arg( errno     )
    425425                               .arg( strerror( errno ) ));
    426 
    427426            nBytes = -1;
    428427        }
    429            
    430         close( file );
    431428    }
    432429
    433430    // ----------------------------------------------------------------------