]> git.saurik.com Git - wxWidgets.git/commitdiff
check the return code of wxStream::Read() in wxImageHandler::DoCanRead()
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 18 May 2002 12:41:51 +0000 (12:41 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 18 May 2002 12:41:51 +0000 (12:41 +0000)
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

src/common/gifdecod.cpp
src/common/imagbmp.cpp
src/common/image.cpp
src/common/imagiff.cpp
src/common/imagjpeg.cpp
src/common/imagpcx.cpp
src/common/imagpng.cpp
src/common/imagtiff.cpp
src/common/xpmdecod.cpp

index 011d8cf3ad06e2d385bbc767bb450ed19f04eaaf..f7acdfb4e67017a8a8d6cbf39632ab1138589bb0 100644 (file)
@@ -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;
 }
 
 
index f8552ff5a55be8ec951f4535014194675c529653..c9b3a3a55490eb256ddf5f4754a66000944700de 100644 (file)
@@ -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
         {
index 0f2e9b6def4abbabaf8f75bb8b32868052fefc0c..a18a4473bbadb1317138c7f618252d5425baec56 100644 (file)
@@ -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() )
     {
index affbd43967ac48604f8c5baeacef30be75e06ebd..25d700bc377dadfc3e7968959fb1741a9815975a 100644 (file)
@@ -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);
 }
 
 
index 23c5e7aa366ea3c8b5f0e3df15c90b4834e6f772..a36895eb111201b359d8e162e4c40ba4950c913c 100644 (file)
@@ -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
index 2e50e2c5a502ced4c829492350b2613d9e5709be..1b9874db40fefc3e6db88ce116eea19a51baeba7 100644 (file)
@@ -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
index 9320f2f371911893fcffd07b9416f0501038c7f4..cfdf35d5fb5f269ab9bbf80c455459a6b3a1e190 100644 (file)
@@ -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
index 0c741c92c1725465379fbfc19bdeb9d7da841607..48ca43ab9856c737e2980d08925d94970610ff79 100644 (file)
@@ -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');
 }
 
 
index 73ce1f5d4b769336ba6abb3a98944d25bc9c88aa..3b33f948acb91f734f01538aa29ffaec4acfa9d5 100644 (file)
@@ -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)