You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/06/10 10:31:25 UTC

[GitHub] [rocketmq] Wushiyii opened a new pull request, #4447: [ISSUE #4349] fix negative index when index reach Integer.MAX_VALUE

Wushiyii opened a new pull request, #4447:
URL: https://github.com/apache/rocketmq/pull/4447

   ## What is the purpose of the change
   See https://github.com/apache/rocketmq/issues/4349
   
   In ThreadLocalIndex.java, when the random index increase to Integer.MAX_VALUE(2147483647), the next increment value is Integer.MIN_VALUE(-2147483648). And the code `Math.abs(index)` will also return the same negatice value Integer.MIN_VALUE(-2147483648)
   
   ```java
       /**
        * Returns the absolute value of an {@code int} value.
        * If the argument is not negative, the argument is returned.
        * If the argument is negative, the negation of the argument is returned.
        *
        * <p>Note that if the argument is equal to the value of
        * {@link Integer#MIN_VALUE}, the most negative representable
        * {@code int} value, the result is that same value, which is
        * negative.
        *
        * @param   a   the argument whose absolute value is to be determined
        * @return  the absolute value of the argument.
        */
       public static int abs(int a) {
           return (a < 0) ? -a : a;
       }
   ```
   
   ## Brief changelog
   [ISSUE #4349] Fix negative ThreadLocalIndex
   


-- 
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: dev-unsubscribe@rocketmq.apache.org

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


[GitHub] [rocketmq] duhenglucky merged pull request #4447: [ISSUE #4349] fix negative index when index reach Integer.MAX_VALUE

Posted by GitBox <gi...@apache.org>.
duhenglucky merged PR #4447:
URL: https://github.com/apache/rocketmq/pull/4447


-- 
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: dev-unsubscribe@rocketmq.apache.org

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


[GitHub] [rocketmq] nxllxn commented on a diff in pull request #4447: [ISSUE #4349] fix negative index when index reach Integer.MAX_VALUE

Posted by GitBox <gi...@apache.org>.
nxllxn commented on code in PR #4447:
URL: https://github.com/apache/rocketmq/pull/4447#discussion_r1004658293


##########
client/src/main/java/org/apache/rocketmq/client/common/ThreadLocalIndex.java:
##########
@@ -31,7 +32,7 @@ public int incrementAndGet() {
         }
 
         this.threadLocalIndex.set(++index);
-        return Math.abs(index);
+        return Math.abs(index & POSITIVE_MASK);

Review Comment:
   Awesome bit operation, but it seems that the Math.abs isn't needed anymore. correct me if I am wrong!



-- 
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: commits-unsubscribe@rocketmq.apache.org

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