Skip to content

Commit

Permalink
Added some more corrupt inputs - relates to github #1888
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Jan 11, 2025
1 parent 4c40f99 commit e404498
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pg/src/main/java/org/bouncycastle/gpg/SExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ private static SExpression parseExpression(InputStream src, SExpression expr, By

if (accumulator.size() > 0)
{
if (expr == null)
{
throw new IOException("invalid input stream");
}

expr.addValue(Strings.fromByteArray(accumulator.toByteArray()));
}

Expand All @@ -168,11 +173,19 @@ private static SExpression parseExpression(InputStream src, SExpression expr, By
}
else if (c == '#')
{
if (expr == null)
{
throw new IOException("invalid input stream at '#'");
}
consumeUntilSkipWhiteSpace(src, accumulator, '#');
expr.addValue(Hex.decode(accumulator.toByteArray()));
}
else if (c == '"')
{
if (expr == null)
{
throw new IOException("invalid input stream at '\"'");
}
consumeUntilSkipCRorLF(src, accumulator, '"');
expr.addValue(new SExpression.QuotedString(Strings.fromByteArray(accumulator.toByteArray())));
}
Expand Down
31 changes: 30 additions & 1 deletion pg/src/test/java/org/bouncycastle/openpgp/test/SExprTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,17 @@ public String getName()
}

private void corruptStreamTest()
throws Exception
{
try
{
SExpression.parse(new ByteArrayInputStream(Strings.toByteArray("12")), 2);
fail("no exception");
}
catch (IOException e)
{
isEquals("invalid input stream", e.getMessage());
}

try
{
SExpression.parse(new ByteArrayInputStream(Strings.toByteArray("2:3abc")), 2);
Expand All @@ -154,6 +163,26 @@ private void corruptStreamTest()
{
isEquals("invalid input stream at ':'", e.getMessage());
}

try
{
SExpression.parse(new ByteArrayInputStream(Strings.toByteArray("#3abc")), 2);
fail("no exception");
}
catch (IOException e)
{
isEquals(e.getMessage(), "invalid input stream at '#'", e.getMessage());
}

try
{
SExpression.parse(new ByteArrayInputStream(Strings.toByteArray("\"3abc")), 2);
fail("no exception");
}
catch (IOException e)
{
isEquals(e.getMessage(), "invalid input stream at '\"'", e.getMessage());
}
}

public void performTest()
Expand Down

0 comments on commit e404498

Please sign in to comment.