fix(cloudwatch): keep GetLogEvents paging inside the list bounds (#2355)

GetLogEvents ends a page at Math.min(pageStart + maxEvents, total), and
maxEvents is Math.min(limit > 0 ? limit : Integer.MAX_VALUE,
maxEventsPerQuery). A max-events cap configured near Integer.MAX_VALUE, the
natural way to express "no cap" since the setting has no upper bound, makes
pageStart + maxEvents overflow negative as soon as a forward token moves
pagination past the first page. subList then throws IllegalArgumentException
and the call fails with a 500.

The end of the stream is where a client meets this rather than some deep page:
GetLogEvents echoes its forward token when nothing is left, so a paginator
always spends one more call on the token it was just handed.

Take the window out of what remains instead of adding to the offset, which
keeps both operands within the list's own bounds. The backward and tail
branches already subtract and clamp, so they are unaffected, and behaviour at
the default cap of 10000 is unchanged.

The regression test drives the second paginator call under an unbounded cap,
and fails without the fix with fromIndex(3) > toIndex(-2147483646).
This commit is contained in:
TiM authored and GitHub committed 2026-08-17 13:40:15 -04:00
1 parent 112602fc6e
commit c163a8678c
2 files changed
+37 -1

No files matched your search

@@ -428,7 +428,10 @@ public class CloudWatchLogsService {
if (nextToken != null && nextToken.startsWith("f/")) {
int offset = parseTokenIndex(nextToken, 2);
pageStart = Math.min(offset, total);
pageEnd = Math.min(pageStart + maxEvents, total);
// Take the window out of what is left rather than adding to the offset, so a
// max-events cap configured near Integer.MAX_VALUE cannot overflow the end
// index negative once pagination has moved past the first page.
pageEnd = pageStart + Math.min(maxEvents, total - pageStart);
} else if (nextToken != null && nextToken.startsWith("b/")) {
int end = parseTokenIndex(nextToken, 2);
pageEnd = Math.min(end, total);
@@ -524,6 +524,39 @@ class CloudWatchLogsServiceTest {
assertEquals("f/3", atEnd.nextForwardToken(), "token must echo back to signal end of stream");
}
@Test
void getLogEventsPagesForwardWithAnUnboundedMaxEventsPerQuery() {
CloudWatchLogsService unboundedService = new CloudWatchLogsService(
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new InMemoryStorage<>(),
Integer.MAX_VALUE,
new RegionResolver("us-east-1", "000000000000")
);
unboundedService.createLogGroup("/app/logs", null, null, REGION);
unboundedService.createLogStream("/app/logs", "stream-1", REGION);
long now = System.currentTimeMillis();
unboundedService.putLogEvents("/app/logs", "stream-1", List.of(
Map.of("timestamp", now, "message", "a"),
Map.of("timestamp", now + 1, "message", "b"),
Map.of("timestamp", now + 2, "message", "c")
), REGION);
CloudWatchLogsService.LogEventsResult page =
unboundedService.getLogEvents("/app/logs", "stream-1", null, null, 0, true, null, REGION);
assertEquals(3, page.events().size());
assertEquals("f/3", page.nextForwardToken());
// GetLogEvents echoes its token at the end of the stream, so a paginator always
// spends one more call on the token it was just handed.
CloudWatchLogsService.LogEventsResult atEnd = unboundedService.getLogEvents(
"/app/logs", "stream-1", null, null, 0, true, page.nextForwardToken(), REGION);
assertEquals(0, atEnd.events().size());
assertEquals("f/3", atEnd.nextForwardToken());
}
@Test
void getLogEventsRejectsMalformedNextToken() {
service.createLogGroup("/app/logs", null, null, REGION);