In debugging a small library I'm writing atop Bouncy Castle C#, I
expanded and corrected a couple of error messages. Also, I made a
PemReader.ReadObject() and PemParser.ReadPemObject() raise an exception
in case of file format error, instead of silently returning a null.
That makes the program crash right when the error occurs. Rather than
sneaking a null object into a variable to produce a crash later.
diff --git a/crypto/src/openssl/PEMReader.cs b/crypto/src/openssl/PEMReader.cs
index 8c19fe6..da2b51a 100644
--- a/crypto/src/openssl/PEMReader.cs
+++ b/crypto/src/openssl/PEMReader.cs
@@ -87,7 +87,7 @@ public object ReadObject()
             PemObject obj = ReadPemObject();
 
             if (obj == null)
-                return null;
+                throw new ArgumentNullException("PemObject from ReadPemObject null.");
 
             // TODO Follow Java build and map to parser objects?
 //			if (parsers.Contains(obj.Type))
diff --git a/crypto/src/x509/PEMParser.cs b/crypto/src/x509/PEMParser.cs
index 8c117f3..b95e871 100644
--- a/crypto/src/x509/PEMParser.cs
+++ b/crypto/src/x509/PEMParser.cs
@@ -81,13 +81,13 @@ class PemParser
 
 				if (!(o is Asn1Sequence))
 				{
-					throw new IOException("malformed PEM data encountered");
+					throw new FileFormatException("malformed PEM data encountered");
 				}
 
 				return (Asn1Sequence) o;
 			}
 
-			return null;
+			throw new FileFormatException("No non-empty PEM (Base64) certificate found.");
 		}
 	}
 }

Reply via email to