pbacsko commented on a change in pull request #3423:
URL: https://github.com/apache/hadoop/pull/3423#discussion_r717676453



##########
File path: 
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java
##########
@@ -301,9 +305,18 @@ public void clear() {
    */
   private boolean ensureCapacity(final int capacity) {
     if (bytes.length < capacity) {
+      // use long to allow overflow
+      long tmpLength = bytes.length;
+      long tmpCapacity = capacity;
+
       // Try to expand the backing array by the factor of 1.5x
-      // (by taking the current size + diving it by half)
-      int targetSize = Math.max(capacity, bytes.length + (bytes.length >> 1));
+      // (by taking the current size + diving it by half).
+      //
+      // If the calculated value is beyond the size
+      // limit, we cap it to ARRAY_MAX_SIZE
+      int targetSize = (int)Math.min(ARRAY_MAX_SIZE,

Review comment:
       The problem is that `bytes.length + (bytes.length >> 1);` might overflow 
and end up being negative which results in choosing `capacity` all the time 
instead of `ARRAY_MAX_SIZE`. So we either temporarily store this as `long` and 
cast back to `int` or we directly check if `targetSize` is negative after `int 
targetSize = (int) bytes.length + (bytes.length >> 1);`. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-issues-h...@hadoop.apache.org

Reply via email to