gnodet-bot commented on code in PR #26770:
URL: https://github.com/apache/camel/pull/26770#discussion_r4082902966


##########
core/camel-core-processor/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java:
##########
@@ -46,6 +46,24 @@ protected void doStart() throws Exception {
                     "Loadbalacing with " + getProcessors().size()
                                                + " should match number of 
distributions " + ratios.size());
         }
+        // a ratio that is negative, or ratios that are all zero or add up to 
more than an int can hold,
+        // would make the processor selection loop forever or fail on every 
exchange
+        long sum = 0;

Review Comment:
   The `doStart()` validation correctly uses a `long sum` to detect overflow — 
but the constructor on line 32-34 (unchanged by this PR) has already computed 
`distributionRatioSum` via `mapToInt(...).sum()`, which is plain `int` 
arithmetic and silently overflows for inputs like `[Integer.MAX_VALUE, 1]`.
   
   For those inputs, the constructor sets `runtimeRatioSum = -2147483648` 
before `doStart()` throws. In practice this is harmless (the route never 
starts), but the object is in an internally inconsistent state between 
construction and startup. A subclass that reads `runtimeRatioSum` in its own 
constructor (after `super(...)`) would see a negative value and could draw 
wrong conclusions.
   
   Consider moving the validation into the constructor, or at minimum using a 
`long`-based computation there:
   ```java
   long longSum = 
ratios.stream().mapToLong(DistributionRatio::getDistributionWeight).sum();
   if (longSum > Integer.MAX_VALUE || longSum < 0) {
       throw new IllegalArgumentException("Sum of distribution ratios overflows 
int: " + longSum);
   }
   this.distributionRatioSum = (int) longSum;
   this.runtimeRatioSum = distributionRatioSum;
   ```
   (This would also eliminate the duplicate iteration in `doStart()`.)



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to