549c89
diff --git a/common/rfb/PixelFormat.cxx b/common/rfb/PixelFormat.cxx
549c89
index 76051dc..a9d015d 100644
549c89
--- a/common/rfb/PixelFormat.cxx
549c89
+++ b/common/rfb/PixelFormat.cxx
549c89
@@ -75,7 +75,8 @@ PixelFormat::PixelFormat(int b, int d, bool e, bool t,
549c89
     redMax(rm), greenMax(gm), blueMax(bm),
549c89
     redShift(rs), greenShift(gs), blueShift(bs)
549c89
 {
549c89
-  assert(isSane());
549c89
+  if (!isSane())
549c89
+    throw Exception("invalid pixel format");
549c89
 
549c89
   updateState();
549c89
 }
549c89
@@ -672,8 +673,16 @@ bool PixelFormat::isSane(void)
549c89
     return false;
549c89
 
549c89
   totalBits = bits(redMax) + bits(greenMax) + bits(blueMax);
549c89
-  if (totalBits > bpp)
549c89
+  if (totalBits > depth)
549c89
+    return false;
549c89
+
549c89
+  if ((bits(redMax) + redShift) > bpp)
549c89
+    return false;
549c89
+  if ((bits(greenMax) + greenShift) > bpp)
549c89
     return false;
549c89
+  if ((bits(blueMax) + blueShift) > bpp)
549c89
+    return false;
549c89
+
549c89
 
549c89
   if (((redMax << redShift) & (greenMax << greenShift)) != 0)
549c89
     return false;