This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
The following commit(s) were added to refs/heads/master by this push:
new 7ab7a26b Fix null charset name handling in codecs (#454)
7ab7a26b is described below
commit 7ab7a26bde60a0de8ab8b67a51844247ad321fed
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 14 06:20:13 2026 -0500
Fix null charset name handling in codecs (#454)
Use the JVM default charset for null charset names in Hex, BCodec,
QCodec, QuotedPrintableCodec, and RFC1522Codec. Update Javadoc,
add regression tests, and record each fix in changes.xml.
---
src/changes/changes.xml | 5 +++
.../java/org/apache/commons/codec/binary/Hex.java | 5 ++-
.../java/org/apache/commons/codec/net/BCodec.java | 9 ++---
.../java/org/apache/commons/codec/net/QCodec.java | 9 ++---
.../commons/codec/net/QuotedPrintableCodec.java | 6 +--
.../org/apache/commons/codec/net/RFC1522Codec.java | 5 ++-
.../org/apache/commons/codec/binary/HexTest.java | 5 +++
.../commons/codec/digest/DigestUtilsTest.java | 44 +++++++++++-----------
.../org/apache/commons/codec/net/BCodecTest.java | 14 +++++++
.../org/apache/commons/codec/net/QCodecTest.java | 14 +++++++
.../codec/net/QuotedPrintableCodecTest.java | 6 +++
.../apache/commons/codec/net/RFC1522CodecTest.java | 9 +++++
12 files changed, 92 insertions(+), 39 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 91542acc..f3ce08de 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -47,6 +47,11 @@ The <action> type attribute can be add,update,fix,remove.
<!-- FIX -->
<action type="fix" dev="ggregory" due-to="Aryan R. Ganjoo, Gary Gregory"
issue="CODEC-347">Handle names emptied during Match Rating Approach cleaning
(#448).</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory"
issue="CODEC-348">Clarify Base64 STRICT and LENIENT decoding Javadoc, document
the 1.23.0 behavioral incompatibility, and add examples for MIME, standard
Base64, Base64 URL, exact wrapped input, XML whitespace normalization,
identifier comparison, and streaming validation. Decoding behavior is
unchanged.</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">Hex(String) now
uses the JVM default charset when the charset name is null.</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec(String)
now uses the JVM default charset when the charset name is null.</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">QCodec(String)
now uses the JVM default charset when the charset name is null.</action>
+ <action type="fix" dev="ggregory" due-to="Gary
Gregory">QuotedPrintableCodec(String) now uses the JVM default charset when the
charset name is null.</action>
+ <action type="fix" dev="ggregory" due-to="Gary
Gregory">RFC1522Codec.encodeText(String, String) now uses the JVM default
charset when the charset name is null.</action>
<!-- ADD -->
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump
org.apache.commons:commons-parent from 105 to 107.</action>
diff --git a/src/main/java/org/apache/commons/codec/binary/Hex.java
b/src/main/java/org/apache/commons/codec/binary/Hex.java
index 4a4a361c..3943c519 100644
--- a/src/main/java/org/apache/commons/codec/binary/Hex.java
+++ b/src/main/java/org/apache/commons/codec/binary/Hex.java
@@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.BinaryEncoder;
import org.apache.commons.codec.CharEncoding;
+import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
@@ -425,13 +426,13 @@ public class Hex implements BinaryEncoder, BinaryDecoder {
/**
* Creates a new codec with the given charset name.
*
- * @param charsetName The charset name.
+ * @param charsetName The charset name, or {@code null} to use {@link
Charset#defaultCharset()}.
* @throws java.nio.charset.UnsupportedCharsetException Thrown if the
named charset is unavailable.
* @since 1.4
* @since 1.7 throws UnsupportedCharsetException if the named charset is
unavailable
*/
public Hex(final String charsetName) {
- this(Charset.forName(charsetName));
+ this(Charsets.toCharset(charsetName));
}
/**
diff --git a/src/main/java/org/apache/commons/codec/net/BCodec.java
b/src/main/java/org/apache/commons/codec/net/BCodec.java
index ab367483..34948d76 100644
--- a/src/main/java/org/apache/commons/codec/net/BCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/BCodec.java
@@ -22,6 +22,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
+import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.CodecPolicy;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
@@ -117,15 +118,14 @@ public class BCodec extends RFC1522Codec implements
StringEncoder, StringDecoder
/**
* Constructs a new instance for the selection of a default Charset.
*
- * @param charsetName
- * the default Charset to use.
+ * @param charsetName The charset name, or {@code null} to use {@link
Charset#defaultCharset()}.
* @throws java.nio.charset.UnsupportedCharsetException
* Thrown if the named Charset is unavailable.
* @since 1.7 throws UnsupportedCharsetException if the named Charset is
unavailable
* @see Charset
*/
public BCodec(final String charsetName) {
- this(Charset.forName(charsetName));
+ this(Charsets.toCharset(charsetName));
}
/**
@@ -267,8 +267,7 @@ public class BCodec extends RFC1522Codec implements
StringEncoder, StringDecoder
*
* @param strSource
* string to convert to Base64 form.
- * @param sourceCharset
- * the Charset for {@code value}.
+ * @param sourceCharset The charset name, or {@code null} to use {@link
Charset#defaultCharset()}.
* @return Base64 string.
* @throws EncoderException
* Thrown if a failure condition is encountered during the
encoding process.
diff --git a/src/main/java/org/apache/commons/codec/net/QCodec.java
b/src/main/java/org/apache/commons/codec/net/QCodec.java
index f061de3b..db6c90b4 100644
--- a/src/main/java/org/apache/commons/codec/net/QCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/QCodec.java
@@ -23,6 +23,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.BitSet;
+import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringDecoder;
@@ -129,15 +130,14 @@ public class QCodec extends RFC1522Codec implements
StringEncoder, StringDecoder
/**
* Constructs a new instance for the selection of a default Charset.
*
- * @param charsetName
- * the Charset to use.
+ * @param charsetName The charset name, or {@code null} to use {@link
Charset#defaultCharset()}.
* @throws java.nio.charset.UnsupportedCharsetException
* Thrown if the named Charset is unavailable.
* @since 1.7 throws UnsupportedCharsetException if the named Charset is
unavailable
* @see Charset
*/
public QCodec(final String charsetName) {
- this(Charset.forName(charsetName));
+ this(Charsets.toCharset(charsetName));
}
/**
@@ -285,8 +285,7 @@ public class QCodec extends RFC1522Codec implements
StringEncoder, StringDecoder
*
* @param sourceStr
* string to convert to quoted-printable form.
- * @param sourceCharset
- * the Charset for sourceStr.
+ * @param sourceCharset The charset name, or {@code null} to use {@link
Charset#defaultCharset()}.
* @return quoted-printable string.
* @throws EncoderException
* Thrown if a failure condition is encountered during the
encoding process.
diff --git
a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
index 7d537723..a44153a3 100644
--- a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
@@ -27,6 +27,7 @@ import java.util.BitSet;
import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.BinaryEncoder;
+import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringDecoder;
@@ -355,15 +356,14 @@ public class QuotedPrintableCodec implements
BinaryEncoder, BinaryDecoder, Strin
/**
* Constructs a new instance for the selection of a default Charset.
*
- * @param charsetName The default string Charset to use.
+ * @param charsetName The charset name, or {@code null} to use {@link
Charset#defaultCharset()}.
* @throws UnsupportedCharsetException Thrown if no support for the named
Charset is available in this instance of the Java virtual machine.
- * @throws IllegalArgumentException Thrown if the given charsetName is
null.
* @throws IllegalCharsetNameException Thrown if the given Charset name is
illegal.
*
* @since 1.7 throws UnsupportedCharsetException if the named Charset is
unavailable
*/
public QuotedPrintableCodec(final String charsetName) throws
IllegalCharsetNameException, IllegalArgumentException,
UnsupportedCharsetException {
- this(Charset.forName(charsetName), false);
+ this(Charsets.toCharset(charsetName), false);
}
/**
diff --git a/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
b/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
index 38c9efd1..a62344e7 100644
--- a/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
+++ b/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
@@ -22,6 +22,7 @@ import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Objects;
+import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.binary.StringUtils;
@@ -161,7 +162,7 @@ abstract class RFC1522Codec {
* </p>
*
* @param text A string to encode.
- * @param charsetName The charset to use.
+ * @param charsetName The charset name, or {@code null} to use {@link
Charset#defaultCharset()}.
* @return RFC 1522 compliant "encoded-word".
* @throws EncoderException Thrown if there is an error
condition during the encoding process.
* @throws UnsupportedCharsetException Thrown if charset is not available.
@@ -172,7 +173,7 @@ abstract class RFC1522Codec {
// Don't attempt charsetName conversion.
return null;
}
- return encodeText(text, Charset.forName(charsetName));
+ return encodeText(text, Charsets.toCharset(charsetName));
}
/**
diff --git a/src/test/java/org/apache/commons/codec/binary/HexTest.java
b/src/test/java/org/apache/commons/codec/binary/HexTest.java
index dd3bf060..37d9b37f 100644
--- a/src/test/java/org/apache/commons/codec/binary/HexTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/HexTest.java
@@ -188,6 +188,11 @@ class HexTest {
assertEquals(sourceString, actualStringFromBytes, name);
}
+ @Test
+ void testConstructorNullCharsetName() {
+ assertEquals(Charset.defaultCharset(), new Hex((String)
null).getCharset());
+ }
+
@ParameterizedTest
@MethodSource("org.apache.commons.codec.CharsetsTest#getAvailableCharsetNames()")
void testCustomCharset(final String name) throws
UnsupportedEncodingException, DecoderException {
diff --git a/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
b/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
index abd2fb06..72c83a16 100644
--- a/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
+++ b/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
@@ -438,6 +438,28 @@ class DigestUtilsTest {
assertEquals("SHA-1", DigestUtils.getShaDigest().getAlgorithm());
}
+ @Test
+ void testGetDigestsJava9() {
+ if (isJava9Plus()) {
+ assertEquals("SHA3-224",
DigestUtils.getSha3_224Digest().getAlgorithm());
+ assertEquals("SHA3-256",
DigestUtils.getSha3_256Digest().getAlgorithm());
+ assertEquals("SHA3-384",
DigestUtils.getSha3_384Digest().getAlgorithm());
+ assertEquals("SHA3-512",
DigestUtils.getSha3_512Digest().getAlgorithm());
+ } else {
+ assertThrows(IllegalArgumentException.class,
DigestUtils::getSha3_224Digest);
+ assertThrows(IllegalArgumentException.class,
DigestUtils::getSha3_256Digest);
+ assertThrows(IllegalArgumentException.class,
DigestUtils::getSha3_384Digest);
+ assertThrows(IllegalArgumentException.class,
DigestUtils::getSha3_512Digest);
+ }
+ }
+
+ @Test
+ void testGetMessageDigest() {
+ final DigestUtils digestUtils = new
DigestUtils(MessageDigestAlgorithms.MD5);
+ assertNotNull(digestUtils.getMessageDigest());
+ assertEquals(MessageDigestAlgorithms.MD5,
digestUtils.getMessageDigest().getAlgorithm());
+ }
+
/**
* Tests SHAKE digest lookup according to runtime algorithm availability.
*
@@ -464,28 +486,6 @@ class DigestUtilsTest {
}
}
- @Test
- void testGetDigestsJava9() {
- if (isJava9Plus()) {
- assertEquals("SHA3-224",
DigestUtils.getSha3_224Digest().getAlgorithm());
- assertEquals("SHA3-256",
DigestUtils.getSha3_256Digest().getAlgorithm());
- assertEquals("SHA3-384",
DigestUtils.getSha3_384Digest().getAlgorithm());
- assertEquals("SHA3-512",
DigestUtils.getSha3_512Digest().getAlgorithm());
- } else {
- assertThrows(IllegalArgumentException.class,
DigestUtils::getSha3_224Digest);
- assertThrows(IllegalArgumentException.class,
DigestUtils::getSha3_256Digest);
- assertThrows(IllegalArgumentException.class,
DigestUtils::getSha3_384Digest);
- assertThrows(IllegalArgumentException.class,
DigestUtils::getSha3_512Digest);
- }
- }
-
- @Test
- void testGetMessageDigest() {
- final DigestUtils digestUtils = new
DigestUtils(MessageDigestAlgorithms.MD5);
- assertNotNull(digestUtils.getMessageDigest());
- assertEquals(MessageDigestAlgorithms.MD5,
digestUtils.getMessageDigest().getAlgorithm());
- }
-
@Test
void testInternalNoSuchAlgorithmException() {
assertThrows(IllegalArgumentException.class, () ->
DigestUtils.getDigest("Bogus Bogus"));
diff --git a/src/test/java/org/apache/commons/codec/net/BCodecTest.java
b/src/test/java/org/apache/commons/codec/net/BCodecTest.java
index d66d350a..f953d4b5 100644
--- a/src/test/java/org/apache/commons/codec/net/BCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/BCodecTest.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.stream.Stream;
@@ -112,6 +113,11 @@ class BCodecTest {
assertEquals(plain, bcodec.decode(encoded), "Basic B decoding test");
}
+ @Test
+ void testConstructorNullCharsetName() {
+ assertEquals(Charset.defaultCharset(), new BCodec((String)
null).getCharset());
+ }
+
@ParameterizedTest(name = "{0}: {1}")
@MethodSource("embeddedQuestionMarks")
void testDecodeEmbeddedQuestionMark(final BCodec codec, final String
encoded) {
@@ -157,6 +163,14 @@ class BCodecTest {
assertNull(bcodec.decode((String) null), "Null string B decoding
test");
}
+ @Test
+ void testEncodeNullCharsetName() throws Exception {
+ final BCodec codec = new BCodec("UTF-16BE");
+ final String text = "caf\u00e9";
+ assertEquals(codec.encode(text, Charset.defaultCharset()),
codec.encode(text, (String) null));
+ assertNull(codec.encode(null, (String) null));
+ }
+
@Test
void testEncodeObjects() throws Exception {
final BCodec bcodec = new BCodec();
diff --git a/src/test/java/org/apache/commons/codec/net/QCodecTest.java
b/src/test/java/org/apache/commons/codec/net/QCodecTest.java
index ad09c31d..b482beb3 100644
--- a/src/test/java/org/apache/commons/codec/net/QCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/QCodecTest.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.stream.Stream;
@@ -82,6 +83,11 @@ class QCodecTest {
assertEquals(plain, qcodec.decode(encoded), "Basic Q decoding test");
}
+ @Test
+ void testConstructorNullCharsetName() {
+ assertEquals(Charset.defaultCharset(), new QCodec((String)
null).getCharset());
+ }
+
@ParameterizedTest(name = "{0}")
@ValueSource(strings = { "=?UTF-8?Q?ABC?DEF?=", "=?UTF-8?Q?ABC??=",
"=?UTF-8?Q???=" })
void testDecodeEmbeddedQuestionMark(final String encoded) {
@@ -166,6 +172,14 @@ class QCodecTest {
assertNull(qcodec.decode((String) null), "Null string Q decoding
test");
}
+ @Test
+ void testEncodeNullCharsetName() throws Exception {
+ final QCodec codec = new QCodec("UTF-16BE");
+ final String text = "caf\u00e9";
+ assertEquals(codec.encode(text, Charset.defaultCharset()),
codec.encode(text, (String) null));
+ assertNull(codec.encode(null, (String) null));
+ }
+
@Test
void testEncodeObjects() throws Exception {
final QCodec qcodec = new QCodec();
diff --git
a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
index d83d6eec..4fa113fc 100644
--- a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.stream.Stream;
@@ -76,6 +77,11 @@ class QuotedPrintableCodecTest {
assertEquals(plain, qpcodec.decode(encoded), "Basic quoted-printable
decoding test");
}
+ @Test
+ void testConstructorNullCharsetName() {
+ assertEquals(Charset.defaultCharset(), new
QuotedPrintableCodec((String) null).getCharset());
+ }
+
@Test
void testDecodeInvalid() {
final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
diff --git a/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
b/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
index 349bf4b0..c4f63a29 100644
--- a/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
@@ -17,9 +17,11 @@
package org.apache.commons.codec.net;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.CharEncoding;
@@ -84,6 +86,13 @@ class RFC1522CodecTest {
assertExpectedDecoderException(text);
}
+ @Test
+ void testEncodeNullCharsetName() throws Exception {
+ final RFC1522TestCodec codec = new RFC1522TestCodec();
+ assertEquals(codec.encodeText("test", Charset.defaultCharset()),
codec.encodeText("test", (String) null));
+ assertNull(codec.encodeText(null, (String) null));
+ }
+
@Test
void testNullInput() throws Exception {
final RFC1522TestCodec testCodec = new RFC1522TestCodec();