You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/04/20 17:57:03 UTC

(commons-codec) branch master updated (e80ddb7b -> 5f0cfd46)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git


    from e80ddb7b Bump github/codeql-action from 3.24.10 to 3.25.1 (#269)
     new c6c5f11e Base64 constructor makes a better defensive copy of the line separator array
     new 55043334 Better exception message
     new ebe805a2 Base64 constructor makes a defensive copy of a custom alphabet array
     new fcc70e6f Base32 constructor makes a defensive copy of the line separator array
     new d2215d5d Base32 constructor fails-fast with a NullPointerException if the custom alphabet array is null
     new c56b9566 Bullet-proof internals
     new 8714b5f6 Remove dead comment
     new 5f0cfd46 Longer lines

The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |  4 ++
 .../org/apache/commons/codec/binary/Base16.java    | 32 ++-----------
 .../org/apache/commons/codec/binary/Base32.java    | 14 +++---
 .../org/apache/commons/codec/binary/Base64.java    | 55 +++++++++++-----------
 .../apache/commons/codec/binary/BaseNCodec.java    | 24 ++++------
 5 files changed, 53 insertions(+), 76 deletions(-)


(commons-codec) 08/08: Longer lines

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 5f0cfd46c89df69b579f37562ff1eded7ffd4b5c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 20 13:56:58 2024 -0400

    Longer lines
    
    - Whitespace
    - Simplify if
---
 .../org/apache/commons/codec/binary/Base16.java    | 29 ++--------------------
 .../org/apache/commons/codec/binary/Base32.java    |  2 --
 .../apache/commons/codec/binary/BaseNCodec.java    | 24 +++++++-----------
 3 files changed, 11 insertions(+), 44 deletions(-)

diff --git a/src/main/java/org/apache/commons/codec/binary/Base16.java b/src/main/java/org/apache/commons/codec/binary/Base16.java
index c3dfe818..29689442 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base16.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base16.java
@@ -68,12 +68,7 @@ public class Base16 extends BaseNCodec {
      * This array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" equivalents as specified in Table 5 of RFC
      * 4648.
      */
-    // @formatter:off
-    private static final byte[] UPPER_CASE_ENCODE_TABLE = {
-            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
-            'A', 'B', 'C', 'D', 'E', 'F'
-    };
-    // @formatter:on
+    private static final byte[] UPPER_CASE_ENCODE_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
 
     /**
      * This array is a lookup table that translates Unicode characters drawn from the a lower-case "Base16 Alphabet" into their 4-bit positive integer
@@ -95,12 +90,7 @@ public class Base16 extends BaseNCodec {
     /**
      * This array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" lower-case equivalents.
      */
-    // @formatter:off
-    private static final byte[] LOWER_CASE_ENCODE_TABLE = {
-            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
-            'a', 'b', 'c', 'd', 'e', 'f'
-    };
-    // @formatter:on
+    private static final byte[] LOWER_CASE_ENCODE_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
 
     /** Mask used to extract 4 bits, used when decoding character. */
     private static final int MASK_4BITS = 0x0f;
@@ -164,42 +154,33 @@ public class Base16 extends BaseNCodec {
             }
             return;
         }
-
         final int dataLen = Math.min(data.length - offset, length);
         final int availableChars = (context.ibitWorkArea != 0 ? 1 : 0) + dataLen;
-
         // small optimization to short-cut the rest of this method when it is fed byte-by-byte
         if (availableChars == 1 && availableChars == dataLen) {
             // store 1/2 byte for next invocation of decode, we offset by +1 as empty-value is 0
             context.ibitWorkArea = decodeOctet(data[offset]) + 1;
             return;
         }
-
         // we must have an even number of chars to decode
         final int charsToProcess = availableChars % BYTES_PER_ENCODED_BLOCK == 0 ? availableChars : availableChars - 1;
         final int end = offset + dataLen;
-
         final byte[] buffer = ensureBufferSize(charsToProcess / BYTES_PER_ENCODED_BLOCK, context);
-
         int result;
         if (dataLen < availableChars) {
             // we have 1/2 byte from previous invocation to decode
             result = context.ibitWorkArea - 1 << BITS_PER_ENCODED_BYTE;
             result |= decodeOctet(data[offset++]);
-
             buffer[context.pos++] = (byte) result;
-
             // reset to empty-value for next invocation!
             context.ibitWorkArea = 0;
         }
-
         final int loopEnd = end - 1;
         while (offset < loopEnd) {
             result = decodeOctet(data[offset++]) << BITS_PER_ENCODED_BYTE;
             result |= decodeOctet(data[offset++]);
             buffer[context.pos++] = (byte) result;
         }
-
         // we have one char of a hex-pair left over
         if (offset < end) {
             // store 1/2 byte for next invocation of decode, we offset by +1 as empty-value is 0
@@ -212,11 +193,9 @@ public class Base16 extends BaseNCodec {
         if ((octet & 0xff) < decodeTable.length) {
             decoded = decodeTable[octet];
         }
-
         if (decoded == -1) {
             throw new IllegalArgumentException("Invalid octet in encoded value: " + (int) octet);
         }
-
         return decoded;
     }
 
@@ -225,19 +204,15 @@ public class Base16 extends BaseNCodec {
         if (context.eof) {
             return;
         }
-
         if (length < 0) {
             context.eof = true;
             return;
         }
-
         final int size = length * BYTES_PER_ENCODED_BLOCK;
         if (size < 0) {
             throw new IllegalArgumentException("Input length exceeds maximum size for encoded data: " + length);
         }
-
         final byte[] buffer = ensureBufferSize(size, context);
-
         final int end = offset + length;
         for (int i = offset; i < end; i++) {
             final int value = data[i];
diff --git a/src/main/java/org/apache/commons/codec/binary/Base32.java b/src/main/java/org/apache/commons/codec/binary/Base32.java
index ad39da40..06ecec47 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base32.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base32.java
@@ -437,7 +437,6 @@ public class Base32 extends BaseNCodec {
         // This approach makes the '=' padding characters completely optional.
         if (context.eof && context.modulus > 0) { // if modulus == 0, nothing to do
             final byte[] buffer = ensureBufferSize(decodeSize, context);
-
             // We ignore partial bytes, i.e. only multiples of 8 count.
             // Any combination not part of a valid encoding is either partially decoded
             // or will raise an exception. Possible trailing characters are 2, 4, 5, 7.
@@ -507,7 +506,6 @@ public class Base32 extends BaseNCodec {
     @Override
     void encode(final byte[] input, int inPos, final int inAvail, final Context context) {
         // package protected for access from I/O streams
-
         if (context.eof) {
             return;
         }
diff --git a/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java b/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
index df68641a..c598abc3 100644
--- a/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
+++ b/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
@@ -359,7 +359,6 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
         if (Integer.compareUnsigned(newCapacity, MAX_BUFFER_SIZE) > 0) {
             newCapacity = createPositiveCapacity(minCapacity);
         }
-
         final byte[] b = Arrays.copyOf(context.buffer, newCapacity);
         context.buffer = b;
         return b;
@@ -435,8 +434,7 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
      * @param lineLength if &gt; 0, use chunking with a length {@code lineLength}
      * @param chunkSeparatorLength the chunk separator length, if relevant
      */
-    protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize,
-                         final int lineLength, final int chunkSeparatorLength) {
+    protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength) {
         this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, PAD_DEFAULT);
     }
 
@@ -453,8 +451,7 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
      * @param chunkSeparatorLength the chunk separator length, if relevant
      * @param pad byte used as padding byte.
      */
-    protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize,
-                         final int lineLength, final int chunkSeparatorLength, final byte pad) {
+    protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad) {
         this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, pad, DECODING_POLICY_DEFAULT);
     }
 
@@ -473,9 +470,8 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
      * @param decodingPolicy Decoding policy.
      * @since 1.15
      */
-    protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize,
-                         final int lineLength, final int chunkSeparatorLength, final byte pad,
-                         final CodecPolicy decodingPolicy) {
+    protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad,
+            final CodecPolicy decodingPolicy) {
         this.unencodedBlockSize = unencodedBlockSize;
         this.encodedBlockSize = encodedBlockSize;
         final boolean useChunking = lineLength > 0 && chunkSeparatorLength > 0;
@@ -505,12 +501,11 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
      * @return {@code true} if any byte is a valid character in the alphabet or PAD; {@code false} otherwise
      */
     protected boolean containsAlphabetOrPad(final byte[] arrayOctet) {
-        if (arrayOctet == null) {
-            return false;
-        }
-        for (final byte element : arrayOctet) {
-            if (pad == element || isInAlphabet(element)) {
-                return true;
+        if (arrayOctet != null) {
+            for (final byte element : arrayOctet) {
+                if (pad == element || isInAlphabet(element)) {
+                    return true;
+                }
             }
         }
         return false;
@@ -672,7 +667,6 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
             context.buffer = new byte[Math.max(size, getDefaultBufferSize())];
             context.pos = 0;
             context.readPos = 0;
-
             // Overflow-conscious:
             // x + y > z == x + y - z > 0
         } else if (context.pos + size - context.buffer.length > 0) {


(commons-codec) 07/08: Remove dead comment

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 8714b5f62bb5fa5950aa5e8908bd0d8d3334dba5
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 20 13:48:45 2024 -0400

    Remove dead comment
---
 src/main/java/org/apache/commons/codec/binary/Base64.java | 2 --
 1 file changed, 2 deletions(-)

diff --git a/src/main/java/org/apache/commons/codec/binary/Base64.java b/src/main/java/org/apache/commons/codec/binary/Base64.java
index f482db9d..443ccc66 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base64.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base64.java
@@ -701,8 +701,6 @@ public class Base64 extends BaseNCodec {
         }
     }
 
-    // Implementation of the Encoder Interface
-
     /**
      * Calculates a decode table for a given encode table.
      *


(commons-codec) 01/08: Base64 constructor makes a better defensive copy of the line separator array

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit c6c5f11eae145d8e8c655e622f0fc5dd74e6db2a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 20 11:13:37 2024 -0400

    Base64 constructor makes a better defensive copy of the line separator
    array
---
 src/changes/changes.xml                                   |  1 +
 src/main/java/org/apache/commons/codec/binary/Base64.java | 11 ++++++-----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 359a4155..018e6f41 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Optimize memory allocation in PhoneticEngine.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec and QCodec encode() methods throw UnsupportedCharsetException instead of EncoderException.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Set Javadoc link to latest Java API LTS version.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Base64 constructor makes a better defensive copy of the line separator array.</action>
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add override org.apache.commons.codec.language.bm.Rule.PhonemeExpr.size().</action>
       <action type="add" dev="ggregory" due-to="Chris Kocel, Gary Gregory">Add support for Base64 custom alphabets #266.</action>
diff --git a/src/main/java/org/apache/commons/codec/binary/Base64.java b/src/main/java/org/apache/commons/codec/binary/Base64.java
index dc5c2b3b..5f927b05 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base64.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base64.java
@@ -661,7 +661,7 @@ public class Base64 extends BaseNCodec {
      *            4). If lineLength &lt;= 0, then the output will not be divided into lines (chunks). Ignored when
      *            decoding.
      * @param lineSeparator
-     *            Each line of encoded data will end with this sequence of bytes.
+     *            Each line of encoded data will end with this sequence of bytes; the constructor makes a defensive copy.
      * @param padding padding byte.
      * @param encodeTable
      *            The manual encodeTable - a byte array of 64 chars.
@@ -683,13 +683,14 @@ public class Base64 extends BaseNCodec {
         // TODO could be simplified if there is no requirement to reject invalid line sep when length <=0
         // @see test case Base64Test.testConstructors()
         if (lineSeparator != null) {
-            if (containsAlphabetOrPad(lineSeparator)) {
-                final String sep = StringUtils.newStringUtf8(lineSeparator);
+            final byte[] lineSeparatorCopy = lineSeparator.clone();
+            if (containsAlphabetOrPad(lineSeparatorCopy)) {
+                final String sep = StringUtils.newStringUtf8(lineSeparatorCopy);
                 throw new IllegalArgumentException("lineSeparator must not contain base64 characters: [" + sep + "]");
             }
             if (lineLength > 0) { // null line-sep forces no chunking rather than throwing IAE
-                this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
-                this.lineSeparator = lineSeparator.clone();
+                this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparatorCopy.length;
+                this.lineSeparator = lineSeparatorCopy;
             } else {
                 this.encodeSize = BYTES_PER_ENCODED_BLOCK;
                 this.lineSeparator = null;


(commons-codec) 04/08: Base32 constructor makes a defensive copy of the line separator array

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit fcc70e6fa1271158dd8f3a90350fa2589713f257
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 20 13:44:17 2024 -0400

    Base32 constructor makes a defensive copy of the line separator
    array
---
 src/changes/changes.xml                                   | 1 +
 src/main/java/org/apache/commons/codec/binary/Base32.java | 9 +++++----
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c88d2d7c..01ee6076 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Optimize memory allocation in PhoneticEngine.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec and QCodec encode() methods throw UnsupportedCharsetException instead of EncoderException.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Set Javadoc link to latest Java API LTS version.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Base32 constructor makes a defensive copy of the line separator array.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Base64 constructor makes a defensive copy of the line separator array.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Base64 constructor makes a defensive copy of a custom alphabet array.</action>
       <!-- ADD -->
diff --git a/src/main/java/org/apache/commons/codec/binary/Base32.java b/src/main/java/org/apache/commons/codec/binary/Base32.java
index 47f136f8..ebbb57e3 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base32.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base32.java
@@ -359,13 +359,14 @@ public class Base32 extends BaseNCodec {
             if (lineSeparator == null) {
                 throw new IllegalArgumentException("lineLength " + lineLength + " > 0, but lineSeparator is null");
             }
+            final byte[] lineSeparatorCopy = lineSeparator.clone();
             // Must be done after initializing the tables
-            if (containsAlphabetOrPad(lineSeparator)) {
-                final String sep = StringUtils.newStringUtf8(lineSeparator);
+            if (containsAlphabetOrPad(lineSeparatorCopy)) {
+                final String sep = StringUtils.newStringUtf8(lineSeparatorCopy);
                 throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]");
             }
-            this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
-            this.lineSeparator = lineSeparator.clone();
+            this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparatorCopy.length;
+            this.lineSeparator = lineSeparatorCopy;
         } else {
             this.encodeSize = BYTES_PER_ENCODED_BLOCK;
             this.lineSeparator = null;


(commons-codec) 02/08: Better exception message

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 55043334240eb2a1838e37ea1c8a6e434d328fdf
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 20 11:15:02 2024 -0400

    Better exception message
---
 src/main/java/org/apache/commons/codec/binary/Base64.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/codec/binary/Base64.java b/src/main/java/org/apache/commons/codec/binary/Base64.java
index 5f927b05..df72909f 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base64.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base64.java
@@ -676,7 +676,7 @@ public class Base64 extends BaseNCodec {
             decodeTable = DECODE_TABLE;
         } else {
             if (encodeTable.length != ALPHABET_LENGTH) {
-                throw new IllegalArgumentException("encodeTable must be exactly 64 bytes long");
+                throw new IllegalArgumentException("encodeTable must have exactly 64 entries.");
             }
             decodeTable = calculateDecodeTable(encodeTable);
         }


(commons-codec) 06/08: Bullet-proof internals

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit c56b95664913aab406f768c66f9264481b28c1bb
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 20 13:48:12 2024 -0400

    Bullet-proof internals
---
 src/main/java/org/apache/commons/codec/binary/Base16.java | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/main/java/org/apache/commons/codec/binary/Base16.java b/src/main/java/org/apache/commons/codec/binary/Base16.java
index 65f75c42..c3dfe818 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base16.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base16.java
@@ -17,6 +17,8 @@
 
 package org.apache.commons.codec.binary;
 
+import java.util.Objects;
+
 import org.apache.commons.codec.CodecPolicy;
 
 /**
@@ -138,6 +140,7 @@ public class Base16 extends BaseNCodec {
      */
     private Base16(final boolean lowerCase, final byte[] encodeTable, final CodecPolicy decodingPolicy) {
         super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, 0, 0, PAD_DEFAULT, decodingPolicy);
+        Objects.requireNonNull(encodeTable, "encodeTable");
         this.encodeTable = encodeTable;
         this.decodeTable = encodeTable == LOWER_CASE_ENCODE_TABLE ? LOWER_CASE_DECODE_TABLE : UPPER_CASE_DECODE_TABLE;
     }


(commons-codec) 03/08: Base64 constructor makes a defensive copy of a custom alphabet array

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit ebe805a2730ad38886f9f04bd4d242e0a8c9caaa
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 20 13:40:04 2024 -0400

    Base64 constructor makes a defensive copy of a custom alphabet array
---
 src/changes/changes.xml                            |  3 +-
 .../org/apache/commons/codec/binary/Base64.java    | 44 +++++++++++-----------
 2 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 018e6f41..c88d2d7c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -49,7 +49,8 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Optimize memory allocation in PhoneticEngine.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec and QCodec encode() methods throw UnsupportedCharsetException instead of EncoderException.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Set Javadoc link to latest Java API LTS version.</action>
-      <action type="fix" dev="ggregory" due-to="Gary Gregory">Base64 constructor makes a better defensive copy of the line separator array.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Base64 constructor makes a defensive copy of the line separator array.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Base64 constructor makes a defensive copy of a custom alphabet array.</action>
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add override org.apache.commons.codec.language.bm.Rule.PhonemeExpr.size().</action>
       <action type="add" dev="ggregory" due-to="Chris Kocel, Gary Gregory">Add support for Base64 custom alphabets #266.</action>
diff --git a/src/main/java/org/apache/commons/codec/binary/Base64.java b/src/main/java/org/apache/commons/codec/binary/Base64.java
index df72909f..f482db9d 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base64.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base64.java
@@ -498,6 +498,8 @@ public class Base64 extends BaseNCodec {
      */
     private final int encodeSize;
 
+    private final boolean isUrlSafe;
+
     /**
      * Constructs a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
      * <p>
@@ -646,8 +648,7 @@ public class Base64 extends BaseNCodec {
     /**
      * Constructs a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
      * <p>
-     * When encoding the line length and line separator are given in the constructor, and the encoding table is
-     * STANDARD_ENCODE_TABLE.
+     * When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE.
      * </p>
      * <p>
      * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
@@ -656,29 +657,28 @@ public class Base64 extends BaseNCodec {
      * When decoding all variants are supported.
      * </p>
      *
-     * @param lineLength
-     *            Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of
-     *            4). If lineLength &lt;= 0, then the output will not be divided into lines (chunks). Ignored when
-     *            decoding.
-     * @param lineSeparator
-     *            Each line of encoded data will end with this sequence of bytes; the constructor makes a defensive copy.
-     * @param padding padding byte.
-     * @param encodeTable
-     *            The manual encodeTable - a byte array of 64 chars.
+     * @param lineLength     Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4). If lineLength &lt;= 0,
+     *                       then the output will not be divided into lines (chunks). Ignored when decoding.
+     * @param lineSeparator  Each line of encoded data will end with this sequence of bytes; the constructor makes a defensive copy. May be null.
+     * @param padding        padding byte.
+     * @param encodeTable    The manual encodeTable - a byte array of 64 chars.
      * @param decodingPolicy The decoding policy.
-     * @throws IllegalArgumentException
-     *             Thrown when the {@code lineSeparator} contains Base64 characters.
+     * @throws IllegalArgumentException Thrown when the {@code lineSeparator} contains Base64 characters.
      */
     private Base64(final int lineLength, final byte[] lineSeparator, final byte padding, final byte[] encodeTable, final CodecPolicy decodingPolicy) {
         super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength, toLength(lineSeparator), padding, decodingPolicy);
-        this.encodeTable = Objects.requireNonNull(encodeTable, "encodeTable");
-        if (encodeTable == STANDARD_ENCODE_TABLE || encodeTable == URL_SAFE_ENCODE_TABLE) {
+        Objects.requireNonNull(encodeTable, "encodeTable");
+        if (encodeTable.length != ALPHABET_LENGTH) {
+            throw new IllegalArgumentException("encodeTable must have exactly 64 entries.");
+        }
+        this.isUrlSafe = encodeTable == URL_SAFE_ENCODE_TABLE;
+        if (encodeTable == STANDARD_ENCODE_TABLE || this.isUrlSafe) {
             decodeTable = DECODE_TABLE;
+            // No need of a defensive copy of an internal table.
+            this.encodeTable = encodeTable;
         } else {
-            if (encodeTable.length != ALPHABET_LENGTH) {
-                throw new IllegalArgumentException("encodeTable must have exactly 64 entries.");
-            }
-            decodeTable = calculateDecodeTable(encodeTable);
+            this.encodeTable = encodeTable.clone();
+            this.decodeTable = calculateDecodeTable(this.encodeTable);
         }
         // TODO could be simplified if there is no requirement to reject invalid line sep when length <=0
         // @see test case Base64Test.testConstructors()
@@ -919,13 +919,13 @@ public class Base64 extends BaseNCodec {
     }
 
     /**
-     * Returns our current encode mode. True if we're URL-SAFE, false otherwise.
+     * Returns our current encode mode. True if we're URL-safe, false otherwise.
      *
-     * @return true if we're in URL-SAFE mode, false otherwise.
+     * @return true if we're in URL-safe mode, false otherwise.
      * @since 1.4
      */
     public boolean isUrlSafe() {
-        return this.encodeTable == URL_SAFE_ENCODE_TABLE;
+        return isUrlSafe;
     }
 
     /**


(commons-codec) 05/08: Base32 constructor fails-fast with a NullPointerException if the custom alphabet array is null

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit d2215d5dec3031f819c3bb514587d92a6aec8eff
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 20 13:46:13 2024 -0400

    Base32 constructor fails-fast with a NullPointerException if the custom
    alphabet array is null
---
 src/changes/changes.xml                                   | 1 +
 src/main/java/org/apache/commons/codec/binary/Base32.java | 3 +++
 2 files changed, 4 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 01ee6076..c1e7f5e6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Optimize memory allocation in PhoneticEngine.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec and QCodec encode() methods throw UnsupportedCharsetException instead of EncoderException.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Set Javadoc link to latest Java API LTS version.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">Base32 constructor fails-fast with a NullPointerException if the custom alphabet array is null.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Base32 constructor makes a defensive copy of the line separator array.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Base64 constructor makes a defensive copy of the line separator array.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Base64 constructor makes a defensive copy of a custom alphabet array.</action>
diff --git a/src/main/java/org/apache/commons/codec/binary/Base32.java b/src/main/java/org/apache/commons/codec/binary/Base32.java
index ebbb57e3..ad39da40 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base32.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base32.java
@@ -17,6 +17,8 @@
 
 package org.apache.commons.codec.binary;
 
+import java.util.Objects;
+
 import org.apache.commons.codec.CodecPolicy;
 
 /**
@@ -353,6 +355,7 @@ public class Base32 extends BaseNCodec {
      */
     private Base32(final int lineLength, final byte[] lineSeparator, final byte[] encodeTable, final byte padding, final CodecPolicy decodingPolicy) {
         super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength, toLength(lineSeparator), padding, decodingPolicy);
+        Objects.requireNonNull(encodeTable, "encodeTable");
         this.encodeTable = encodeTable;
         this.decodeTable = encodeTable == HEX_ENCODE_TABLE ? HEX_DECODE_TABLE : DECODE_TABLE;
         if (lineLength > 0) {