Skip to content

Commit e404498

Browse files
committed
Added some more corrupt inputs - relates to github #1888
1 parent 4c40f99 commit e404498

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

pg/src/main/java/org/bouncycastle/gpg/SExpression.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ private static SExpression parseExpression(InputStream src, SExpression expr, By
148148

149149
if (accumulator.size() > 0)
150150
{
151+
if (expr == null)
152+
{
153+
throw new IOException("invalid input stream");
154+
}
155+
151156
expr.addValue(Strings.fromByteArray(accumulator.toByteArray()));
152157
}
153158

@@ -168,11 +173,19 @@ private static SExpression parseExpression(InputStream src, SExpression expr, By
168173
}
169174
else if (c == '#')
170175
{
176+
if (expr == null)
177+
{
178+
throw new IOException("invalid input stream at '#'");
179+
}
171180
consumeUntilSkipWhiteSpace(src, accumulator, '#');
172181
expr.addValue(Hex.decode(accumulator.toByteArray()));
173182
}
174183
else if (c == '"')
175184
{
185+
if (expr == null)
186+
{
187+
throw new IOException("invalid input stream at '\"'");
188+
}
176189
consumeUntilSkipCRorLF(src, accumulator, '"');
177190
expr.addValue(new SExpression.QuotedString(Strings.fromByteArray(accumulator.toByteArray())));
178191
}

pg/src/test/java/org/bouncycastle/openpgp/test/SExprTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,17 @@ public String getName()
143143
}
144144

145145
private void corruptStreamTest()
146-
throws Exception
147146
{
147+
try
148+
{
149+
SExpression.parse(new ByteArrayInputStream(Strings.toByteArray("12")), 2);
150+
fail("no exception");
151+
}
152+
catch (IOException e)
153+
{
154+
isEquals("invalid input stream", e.getMessage());
155+
}
156+
148157
try
149158
{
150159
SExpression.parse(new ByteArrayInputStream(Strings.toByteArray("2:3abc")), 2);
@@ -154,6 +163,26 @@ private void corruptStreamTest()
154163
{
155164
isEquals("invalid input stream at ':'", e.getMessage());
156165
}
166+
167+
try
168+
{
169+
SExpression.parse(new ByteArrayInputStream(Strings.toByteArray("#3abc")), 2);
170+
fail("no exception");
171+
}
172+
catch (IOException e)
173+
{
174+
isEquals(e.getMessage(), "invalid input stream at '#'", e.getMessage());
175+
}
176+
177+
try
178+
{
179+
SExpression.parse(new ByteArrayInputStream(Strings.toByteArray("\"3abc")), 2);
180+
fail("no exception");
181+
}
182+
catch (IOException e)
183+
{
184+
isEquals(e.getMessage(), "invalid input stream at '\"'", e.getMessage());
185+
}
157186
}
158187

159188
public void performTest()

0 commit comments

Comments
 (0)