Skip to content

Commit

Permalink
#57 Fix out-of-bounds exception thrown while rendering stack traces.
Browse files Browse the repository at this point in the history
  • Loading branch information
vy committed Oct 29, 2020
1 parent 2aa6853 commit 175598f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
`maxStringLength` is used in combination with
`emptyPropertyExclusionEnabled=true`. (#55)

- Fix `ArrayIndexOutOfBoundsException` thrown when `stackTrace:text` produces
an output violating the truncation limit. (#57)

### (2019-10-15) v0.21

- Add feature comparison matrix.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ Let us try to answer some common questions:

- [bakomchik](https://github.com/bakomchik)
- [chrissydee](https://github.com/chrissydee)
- [Daniel Lundsgaard Skovenborg](https://github.com/waldeinburg)
- [Eric Schwartz](https://github.com/emschwar)
- [Felix Barnsteiner](https://github.com/felixbarny)
- [Johann Schmitz](https://github.com/ercpe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public int getCapacity() {
return bufferedWriter.getCapacity();
}

public boolean isOverflow() {
return bufferedWriter.isOverflow();
}

@Override
public void close() {
bufferedWriter.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ public final class BufferedWriter extends Writer {

private int position;

private boolean overflow;

BufferedWriter(int capacity) {
this.buffer = new char[capacity];
this.position = 0;
this.overflow = false;
}

char[] getBuffer() {
Expand All @@ -25,10 +28,23 @@ int getCapacity() {
return buffer.length;
}

boolean isOverflow() {
return overflow;
}

@Override
public void write(char[] source, int offset, int length) {
System.arraycopy(source, offset, buffer, position, length);
position += length;
if (!overflow) {
int limit = buffer.length - position;
if (length > limit) {
overflow = true;
System.arraycopy(source, offset, buffer, position, limit);
position = buffer.length;
} else {
System.arraycopy(source, offset, buffer, position, length);
position += length;
}
}
}

@Override
Expand All @@ -37,6 +53,7 @@ public void flush() {}
@Override
public void close() {
position = 0;
overflow = false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1455,4 +1455,39 @@ public void test_maxStringLength_with_emptyPropertyExclusionEnabled() throws Exc

}

@Test
public void test_StackTraceTextResolver_with_maxStringLength() throws Exception {

// Create the event template.
ObjectNode eventTemplateRootNode = JSON_NODE_FACTORY.objectNode();
eventTemplateRootNode.put("stackTrace", "${json:exception:stackTrace:text}");
String eventTemplate = eventTemplateRootNode.toString();

// Create the layout.
int maxStringLength = eventTemplate.length();
Configuration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build();
LogstashLayout layout = LogstashLayout
.newBuilder()
.setConfiguration(configuration)
.setEventTemplate(eventTemplate)
.setMaxStringLength(maxStringLength)
.setStackTraceEnabled(true)
.build();

// Create the log event.
SimpleMessage message = new SimpleMessage("foo");
LogEvent logEvent = Log4jLogEvent
.newBuilder()
.setLoggerName(LogstashLayoutTest.class.getSimpleName())
.setMessage(message)
.setThrown(NonAsciiUtf8MethodNameContainingException.INSTANCE)
.build();

// Check the serialized event.
String serializedLogEvent = layout.toSerializable(logEvent);
JsonNode rootNode = OBJECT_MAPPER.readTree(serializedLogEvent);
assertThat(point(rootNode, "stackTrace").asText()).isNotBlank();

}

}

0 comments on commit 175598f

Please sign in to comment.