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

steveloughran pushed a commit to branch branch-3.5
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/branch-3.5 by this push:
     new adad3de7336 YARN-11964: Resource.castToIntSafely() should clamp 
negative values to 0 (#8506)
adad3de7336 is described below

commit adad3de73364ac758087e25fb34209b507b92032
Author: Ryu Kobayashi <[email protected]>
AuthorDate: Tue May 26 19:36:11 2026 +0900

    YARN-11964: Resource.castToIntSafely() should clamp negative values to 0 
(#8506)
    
    Negative resource values returned by YARN RM (e.g. due to transient
    overload or node failures) were passed through castToIntSafely() without
    any guard, propagating invalid negative ints to downstream components.
    Add a guard that clamps values below 0 to 0, consistent with the
    existing upper-bound clamp to Integer.MAX_VALUE.
    
    Contributed by Ryu Kobayashi
---
 .../main/java/org/apache/hadoop/yarn/api/records/Resource.java    | 8 ++++++--
 .../java/org/apache/hadoop/yarn/api/records/TestResource.java     | 5 +++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java
 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java
index 80e569d5a9e..9f73f96bece 100644
--- 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java
+++ 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java
@@ -513,13 +513,17 @@ public int hashCode() {
   }
 
   /**
-   * Convert long to int for a resource value safely. This method assumes
-   * resource value is positive.
+   * Convert long to int for a resource value safely. Negative values are
+   * clamped to 0; values exceeding Integer.MAX_VALUE are clamped to
+   * Integer.MAX_VALUE.
    *
    * @param value long resource value
    * @return int resource value
    */
   protected static int castToIntSafely(long value) {
+    if (value < 0) {
+      return 0;
+    }
     if (value > Integer.MAX_VALUE) {
       return Integer.MAX_VALUE;
     }
diff --git 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/test/java/org/apache/hadoop/yarn/api/records/TestResource.java
 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/test/java/org/apache/hadoop/yarn/api/records/TestResource.java
index 060ca39c697..1d8f6ad8c32 100644
--- 
a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/test/java/org/apache/hadoop/yarn/api/records/TestResource.java
+++ 
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/test/java/org/apache/hadoop/yarn/api/records/TestResource.java
@@ -43,6 +43,11 @@ void testCastToIntSafely() {
         Resource.castToIntSafely(Long.MAX_VALUE),
         "Cast to Integer.MAX_VALUE if the long is greater than "
             + "Integer.MAX_VALUE");
+
+    assertEquals(0, Resource.castToIntSafely(-1),
+        "Cast to 0 if the long is negative");
+    assertEquals(0, Resource.castToIntSafely(Long.MIN_VALUE),
+        "Cast to 0 if the long is negative");
   }
 
   @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to