From: Vadim Zeitlin Date: Sat, 18 May 2002 12:41:51 +0000 (+0000) Subject: check the return code of wxStream::Read() in wxImageHandler::DoCanRead() X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/79fa23744b2c622ebe9ff89a3ee8c8ec8017ee3b check the return code of wxStream::Read() in wxImageHandler::DoCanRead() and avoid reading uninitialized memory when it fails git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15601 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/common/gifdecod.cpp b/src/common/gifdecod.cpp index 011d8cf3ad..f7acdfb4e6 100644 --- a/src/common/gifdecod.cpp +++ b/src/common/gifdecod.cpp @@ -617,10 +617,12 @@ bool wxGIFDecoder::CanRead() { unsigned char buf[3]; - m_f->Read(buf, 3); - m_f->SeekI(-3, wxFromCurrent); + if ( !m_f->Read(buf, WXSIZEOF(buf)) ) + return FALSE; + + m_f->SeekI(-WXSIZEOF(buf), wxFromCurrent); - return (memcmp(buf, "GIF", 3) == 0); + return memcmp(buf, "GIF", WXSIZEOF(buf)) == 0; } diff --git a/src/common/imagbmp.cpp b/src/common/imagbmp.cpp index f8552ff5a5..c9b3a3a554 100644 --- a/src/common/imagbmp.cpp +++ b/src/common/imagbmp.cpp @@ -876,9 +876,13 @@ bool wxBMPHandler::DoCanRead(wxInputStream& stream) { unsigned char hdr[2]; - stream.Read(hdr, 2); - stream.SeekI(-2, wxFromCurrent); - return (hdr[0] == 'B' && hdr[1] == 'M'); + if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) + return FALSE; + + stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent); + + // do we have the BMP file signature? + return hdr[0] == 'B' && hdr[1] == 'M'; } @@ -1259,7 +1263,7 @@ bool wxANIHandler::LoadFile(wxImage *image, wxInputStream& stream, //now either data or a FCC if ( (FCC1 == *riff32) || (FCC1 == *list32) ) { - stream.Read(&FCC2, 4); + stream.Read(&FCC2, 4); } else { @@ -1305,11 +1309,11 @@ bool wxANIHandler::DoCanRead(wxInputStream& stream) return TRUE; // we always have a data size: stream.Read(&datalen, 4); - datalen = wxINT32_SWAP_ON_BE(datalen) ; + datalen = wxINT32_SWAP_ON_BE(datalen) ; // now either data or a FCC: if ( (FCC1 == *riff32) || (FCC1 == *list32) ) { - stream.Read(&FCC2, 4); + stream.Read(&FCC2, 4); } else { @@ -1317,7 +1321,11 @@ bool wxANIHandler::DoCanRead(wxInputStream& stream) } // try to read next data chunk: - stream.Read(&FCC1, 4); + if ( !stream.Read(&FCC1, 4) ) + { + // reading failed -- either EOF or IO error, bail out anyhow + return FALSE; + } } return FALSE; @@ -1348,8 +1356,8 @@ int wxANIHandler::GetImageCount(wxInputStream& stream) datalen = wxINT32_SWAP_ON_BE(datalen) ; // now either data or a FCC: if ( (FCC1 == *riff32) || (FCC1 == *list32) ) - { - stream.Read(&FCC2, 4); + { + stream.Read(&FCC2, 4); } else { diff --git a/src/common/image.cpp b/src/common/image.cpp index 0f2e9b6def..a18a4473bb 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -1015,7 +1015,7 @@ int wxImage::GetImageCount( const wxString &name, long type ) bool wxImage::CanRead( wxInputStream &stream ) { - wxList &list=GetHandlers(); + const wxList& list = GetHandlers(); for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() ) { diff --git a/src/common/imagiff.cpp b/src/common/imagiff.cpp index affbd43967..25d700bc37 100644 --- a/src/common/imagiff.cpp +++ b/src/common/imagiff.cpp @@ -230,12 +230,14 @@ int wxIFFDecoder::GetTransparentColour() const { return m_image->transparent; } // bool wxIFFDecoder::CanRead() { - unsigned char buf[12] = ""; + unsigned char buf[12]; - m_f->Read(buf, 12); - m_f->SeekI(-12, wxFromCurrent); + if ( !m_f->Read(buf, WXSIZEOF(buf)) ) + return FALSE; + + m_f->SeekI(-WXSIZEOF(buf), wxFromCurrent); - return (memcmp(buf, "FORM", 4) == 0 && memcmp(buf+8, "ILBM", 4) == 0); + return (memcmp(buf, "FORM", 4) == 0) && (memcmp(buf+8, "ILBM", 4) == 0); } diff --git a/src/common/imagjpeg.cpp b/src/common/imagjpeg.cpp index 23c5e7aa36..a36895eb11 100644 --- a/src/common/imagjpeg.cpp +++ b/src/common/imagjpeg.cpp @@ -378,9 +378,11 @@ bool wxJPEGHandler::DoCanRead( wxInputStream& stream ) { unsigned char hdr[2]; - stream.Read(hdr, 2); - stream.SeekI(-2, wxFromCurrent); - return (hdr[0] == 0xFF && hdr[1] == 0xD8); + if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) + return FALSE; + + stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent); + return hdr[0] == 0xFF && hdr[1] == 0xD8; } #endif // wxUSE_STREAMS diff --git a/src/common/imagpcx.cpp b/src/common/imagpcx.cpp index 2e50e2c5a5..1b9874db40 100644 --- a/src/common/imagpcx.cpp +++ b/src/common/imagpcx.cpp @@ -486,13 +486,14 @@ bool wxPCXHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbos bool wxPCXHandler::DoCanRead( wxInputStream& stream ) { - unsigned char c; + unsigned char c = stream.GetC(); + if ( !stream ) + return FALSE; - c = stream.GetC(); stream.SeekI(-1, wxFromCurrent); // not very safe, but this is all we can get from PCX header :-( - return (c == 10); + return c == 10; } #endif // wxUSE_STREAMS && wxUSE_PCX diff --git a/src/common/imagpng.cpp b/src/common/imagpng.cpp index 9320f2f371..cfdf35d5fb 100644 --- a/src/common/imagpng.cpp +++ b/src/common/imagpng.cpp @@ -418,9 +418,12 @@ bool wxPNGHandler::DoCanRead( wxInputStream& stream ) { unsigned char hdr[4]; - stream.Read(hdr, 4); - stream.SeekI(-4, wxFromCurrent); - return (hdr[0] == 0x89 && hdr[1] == 'P' && hdr[2] == 'N' && hdr[3] == 'G'); + if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) + return FALSE; + + stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent); + + return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0; } #endif // wxUSE_STREAMS diff --git a/src/common/imagtiff.cpp b/src/common/imagtiff.cpp index 0c741c92c1..48ca43ab98 100644 --- a/src/common/imagtiff.cpp +++ b/src/common/imagtiff.cpp @@ -370,11 +370,13 @@ bool wxTIFFHandler::DoCanRead( wxInputStream& stream ) { unsigned char hdr[2]; - stream.Read(&hdr, 2); - stream.SeekI(-2, wxFromCurrent); + if ( !stream.Read(&hdr, WXSIZEOF(hdr)) ) + return FALSE; + + stream.SeekI(-WXSIZEOF(hdr), wxFromCurrent); - return ((hdr[0] == 0x49 && hdr[1] == 0x49) || - (hdr[0] == 0x4D && hdr[1] == 0x4D)); + return (hdr[0] == 'I' && hdr[1] == 'I') || + (hdr[0] == 'M' && hdr[1] == 'M'); } diff --git a/src/common/xpmdecod.cpp b/src/common/xpmdecod.cpp index 73ce1f5d4b..3b33f948ac 100644 --- a/src/common/xpmdecod.cpp +++ b/src/common/xpmdecod.cpp @@ -126,10 +126,12 @@ bool wxXPMDecoder::CanRead(wxInputStream& stream) { unsigned char buf[9]; - stream.Read(buf, 9); - stream.SeekI(-9, wxFromCurrent); + if ( !stream.Read(buf, WXSIZEOF(buf)) ) + return FALSE; - return (memcmp(buf, "/* XPM */", 9) == 0); + stream.SeekI(-WXSIZEOF(buf), wxFromCurrent); + + return memcmp(buf, "/* XPM */", WXSIZEOF(buf)) == 0; } wxImage wxXPMDecoder::ReadFile(wxInputStream& stream)