Skip to content

Commit

Permalink
bugtraq: Fallback to UTF-8 if commit encoding is unsupported
Browse files Browse the repository at this point in the history
Reading the encoding of a commit can result in a Unsupported- or
IllegalCharsetException. This happens when for whatever reason the
commit has an encoding recorded that the system doesn't understand.
Instead of completely failing, fallback to UTF-8.
  • Loading branch information
flaix committed Nov 11, 2022
1 parent d993339 commit d00bdf2
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -55,6 +58,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.nio.charset.StandardCharsets.UTF_8;

public final class BugtraqConfig {

// Constants ==============================================================
Expand Down Expand Up @@ -229,7 +234,7 @@ private static Config getBaseConfig(@NotNull Repository repository, @NotNull Str
FileMode entmode = tw.getFileMode(0);
if (FileMode.REGULAR_FILE == entmode) {
ObjectLoader ldr = repository.open(entid, Constants.OBJ_BLOB);
content = new String(ldr.getCachedBytes(), commit.getEncoding());
content = new String(ldr.getCachedBytes(), guessEncoding(commit));
break;
}
}
Expand Down Expand Up @@ -265,6 +270,15 @@ private static Config getBaseConfig(@NotNull Repository repository, @NotNull Str
return baseConfig;
}

@NotNull
private static Charset guessEncoding(RevCommit commit) {
try {
return commit.getEncoding();
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
return UTF_8;
}
}

@Nullable
private static String getString(@Nullable String subsection, @NotNull String key, @NotNull Config config, @Nullable Config baseConfig) {
final String value = config.getString(BUGTRAQ, subsection, key);
Expand Down

0 comments on commit d00bdf2

Please sign in to comment.