You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by gu...@apache.org on 2026/09/08 23:27:20 UTC

(spark) branch branch-4.3 updated: [SPARK-59308][ML] Avoid Int overflow when computing the decoded image size

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

HyukjinKwon pushed a commit to branch branch-4.3
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.3 by this push:
     new 21288dce4c0a [SPARK-59308][ML] Avoid Int overflow when computing the decoded image size
21288dce4c0a is described below

commit 21288dce4c0aadbf27f2a49699a28dc69027b55d
Author: Hyukjin Kwon <gu...@apache.org>
AuthorDate: Wed Sep 9 08:25:10 2026 +0900

    [SPARK-59308][ML] Avoid Int overflow when computing the decoded image size
    
    ### What changes were proposed in this pull request?
    
    `ImageSchema.decode` computed the decoded byte-array length as `height * width * nChannels` in
    `Int`. For a large image this multiplication can overflow to a wrong (possibly negative) value,
    slip past the `assert(imageSize < 1e9, ...)` guard, and then drive `Array.ofDim[Byte]` with a
    wrong or negative length (`NegativeArraySizeException`). The size is now computed in `Long` so the
    assertion sees the true value and rejects an oversized image with the intended message.
    
    ### Why are the changes needed?
    
    `BufferedImage` allows a pixel count near `Int.MaxValue`, and with `nChannels` the byte count can
    exceed `Int.MaxValue` and wrap negative. Computing in `Long` keeps the existing `< 1e9` guard
    effective. Legitimate images (well under 1e9 bytes) are unaffected.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No, other than a clear "image is too large" error (instead of a `NegativeArraySizeException`) for
    an image whose decoded size would exceed the existing limit.
    
    ### How was this patch tested?
    
    Existing `ImageSchema` tests continue to pass; the module compiles.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 4.8
    
    Closes #58596 from HyukjinKwon/SPARK-59308-ml-overflow.
    
    Authored-by: Hyukjin Kwon <gu...@apache.org>
    Signed-off-by: Hyukjin Kwon <gu...@apache.org>
    (cherry picked from commit 11e88a1b31e911d55750451db5d4f2bedac187d6)
---
 mllib/src/main/scala/org/apache/spark/ml/image/ImageSchema.scala | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/mllib/src/main/scala/org/apache/spark/ml/image/ImageSchema.scala b/mllib/src/main/scala/org/apache/spark/ml/image/ImageSchema.scala
index e172573312dd..852b5f72c717 100644
--- a/mllib/src/main/scala/org/apache/spark/ml/image/ImageSchema.scala
+++ b/mllib/src/main/scala/org/apache/spark/ml/image/ImageSchema.scala
@@ -157,9 +157,11 @@ object ImageSchema {
         (3, ocvTypes("CV_8UC3"))
       }
 
-      val imageSize = height * width * nChannels
+      // Compute in Long so a large image cannot overflow the multiplication and slip past the
+      // size assertion below.
+      val imageSize = height.toLong * width * nChannels
       assert(imageSize < 1e9, "image is too large")
-      val decoded = Array.ofDim[Byte](imageSize)
+      val decoded = Array.ofDim[Byte](imageSize.toInt)
 
       // Grayscale images in Java require special handling to get the correct intensity
       if (isGray) {


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