You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2026/06/14 15:43:45 UTC

(spark) branch master updated: [SPARK-57427][CORE] Improve `UTF8String` bounds checks with `Objects.checkIndex`

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

dongjoon-hyun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 4608e44a739e [SPARK-57427][CORE] Improve `UTF8String` bounds checks with `Objects.checkIndex`
4608e44a739e is described below

commit 4608e44a739e017b006c2fae16ccc0e4e96072f8
Author: Dongjoon Hyun <do...@apache.org>
AuthorDate: Sun Jun 14 08:43:35 2026 -0700

    [SPARK-57427][CORE] Improve `UTF8String` bounds checks with `Objects.checkIndex`
    
    ### What changes were proposed in this pull request?
    
    Use `java.util.Objects.checkIndex(index, length)` (Java 9+) in place of the
    hand-written `index < 0 || index >= length` bounds checks in `UTF8String.getChar`
    and `UTF8String.codePointFrom`.
    
    ### Why are the changes needed?
    
    It replaces boilerplate with a single, intention-revealing JDK call. No functional
    change (the same `IndexOutOfBoundsException` is thrown).
    
    Additionally, this provides a better exception message than the current one, `new IndexOutOfBoundsException()`, in case of failure.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    Pass the CIs.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Claude Opus 4.8)
    
    Closes #56489 from dongjoon-hyun/SPARK-57427.
    
    Authored-by: Dongjoon Hyun <do...@apache.org>
    Signed-off-by: Dongjoon Hyun <do...@apache.org>
---
 .../src/main/java/org/apache/spark/unsafe/types/UTF8String.java  | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/common/unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java b/common/unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java
index 96b103ae3388..ab57e308ee4d 100644
--- a/common/unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java
+++ b/common/unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java
@@ -25,6 +25,7 @@ import java.util.Arrays;
 import java.util.function.Function;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Objects;
 import java.util.regex.Pattern;
 
 import com.esotericsoftware.kryo.Kryo;
@@ -711,9 +712,7 @@ public final class UTF8String implements Comparable<UTF8String>, Externalizable,
    * If `byteIndex` of the first byte in the code point is known, use `codePointFrom` instead.
    */
   public int getChar(int charIndex) {
-    if (charIndex < 0 || charIndex >= numChars()) {
-      throw new IndexOutOfBoundsException();
-    }
+    Objects.checkIndex(charIndex, numChars());
     int charCount = 0, byteCount = 0;
     while (charCount < charIndex) {
       byteCount += numBytesForFirstByte(getByte(byteCount));
@@ -727,9 +726,7 @@ public final class UTF8String implements Comparable<UTF8String>, Externalizable,
    * If byte index is invalid, throws exception.
    */
   public int codePointFrom(int byteIndex) {
-    if (byteIndex < 0 || byteIndex >= numBytes) {
-      throw new IndexOutOfBoundsException();
-    }
+    Objects.checkIndex(byteIndex, numBytes);
     byte b = getByte(byteIndex);
     int numBytes = numBytesForFirstByte(b);
     return switch (numBytes) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org