Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save head encoding on sanitized String(s) #492

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public TextBannerFormatter(final Supplier<String> bannerSupplier, final ExtForma
public String getHead(final Handler h) {
final String dh = Objects.requireNonNullElse(delegate.getHead(h), "");
final String banner = Objects.requireNonNullElse(bannerSupplier.get(), "");
return banner + dh;
// it doesn't use + because dh can be empty and we both don't want to create an indy and
// we don't want to create a new string
return banner.concat(dh);
dmlloyd marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ protected void setCharsetPrivate(Charset charset) throws SecurityException {
}
}

public Charset getCharset() {
lock.lock();
try {
return super.getCharset();
} finally {
lock.unlock();
}
}

/** {@inheritDoc} Setting a writer will replace any target output stream. */
public void setWriter(final Writer writer) {
lock.lock();
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/jboss/logmanager/handlers/WriterHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.Closeable;
import java.io.Flushable;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.logging.ErrorManager;
import java.util.logging.Formatter;

Expand Down Expand Up @@ -175,9 +177,11 @@ private void writeHead(final Writer writer) {
final Formatter formatter = getFormatter();
if (formatter != null) {
final String head = formatter.getHead(this);
if (checkHeadEncoding) {
if (!getCharset().newEncoder().canEncode(head)) {
reportError("Section header cannot be encoded into charset \"" + getCharset().name() + "\"", null,
if (!head.isEmpty() && checkHeadEncoding) {
dmlloyd marked this conversation as resolved.
Show resolved Hide resolved
Charset cs = getCharset();
// UTF-8 is always safe since the UTF-16 chars in String(s) are always encodable
if (!StandardCharsets.UTF_8.equals(cs) && cs.newEncoder().canEncode(head)) {
dmlloyd marked this conversation as resolved.
Show resolved Hide resolved
reportError("Section header cannot be encoded into charset \"" + cs.name() + "\"", null,
ErrorManager.GENERIC_FAILURE);
return;
}
Expand Down