Skip to content

Commit

Permalink
Fail more gracefully for a too-many-arguments case
Browse files Browse the repository at this point in the history
  • Loading branch information
jobarr-amzn committed Oct 14, 2024
1 parent f7c5a43 commit 6b0b210
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.amazon.ion.impl.macro.MacroRef.Companion.byName

/**
* Reads encoding directives from the given [IonReader]. This performs a similar function to
* IonReaderContinuableCoreBinary.EncodingDirectiveReader, though that one requires more logic to handle continuable
* [IonReaderContinuableCoreBinary.EncodingDirectiveReader], though that one requires more logic to handle continuable
* input. The two could be unified at the expense of higher complexity than is needed by the non-continuable text
* implementation. If the text reader is replaced with a continuable implementation in the future,
* IonReaderContinuableCoreBinary.EncodingDirectiveReader should be moved to the top level and shared by both readers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,8 @@ class IonRawBinaryWriter_1_1 internal constructor(
currentContainer = containerStack.peek()

if (currentContainer.type == EEXP) {
val signature = presenceBitmapStack.peek().signature
if (currentContainer.numChildren >= signature.size) throw IllegalArgumentException("Too many arguments for macro with signature $signature")
presenceBitmapStack.peek()[currentContainer.numChildren] = when (justExitedContainer.type) {
LIST, SEXP, STRUCT, EEXP -> PresenceBitmap.EXPRESSION
EXPR_GROUP -> if (thisContainerTotalLength == 0L) PresenceBitmap.VOID else PresenceBitmap.GROUP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,18 @@ class IonRawBinaryWriterTest_1_1 {
}
}

@Test
fun `calling stepOut() with too many parameters in a length-prefixed e-expression throws IllegalArgumentException`() {
val rawWriter = ionWriter()

assertThrows<IllegalArgumentException> {
rawWriter.stepInEExp(64, usingLengthPrefix = true, dummyMacro(nArgs = 0))
rawWriter.stepInEExp(SystemMacro.None)
rawWriter.stepOut()
rawWriter.stepOut()
}
}

@Test
fun `calling continueExpressionGroup() has no effect when there are no expressions in the current segment`() {
assertWriterOutputEquals(
Expand Down

0 comments on commit 6b0b210

Please sign in to comment.