tmf: Fix integer overflow in time graph calculations
authorPatrick Tasse <patrick.tasse@gmail.com>
Tue, 23 Aug 2016 20:30:24 +0000 (16:30 -0400)
committerPatrick Tasse <patrick.tasse@gmail.com>
Wed, 24 Aug 2016 18:11:17 +0000 (14:11 -0400)
An x-coordinate very far to the right could overflow to be very far to
the left. This could prevent states and/or their labels to be drawn when
zoomed-in very close.

Change-Id: I6e006d366dfe427a9ac7df1a8f534fe95c230c3d
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/79565
Reviewed-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/widgets/timegraph/widgets/TimeGraphControl.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/widgets/timegraph/widgets/TimeGraphScale.java

index 6263b622b2414fbacf157d9a9a4db9d3fd3788f3..25129bb3d4d7ae9808f76829a6134dbdc548e830 100644 (file)
@@ -76,6 +76,7 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Menu;
+import org.eclipse.tracecompass.common.core.math.SaturatedArithmetic;
 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
 import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo;
 import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal;
@@ -1465,7 +1466,7 @@ public class TimeGraphControl extends TimeGraphBaseControl
         int width = getSize().x;
         int nameSpace = fTimeProvider.getNameSpace();
         double pixelsPerNanoSec = (width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (width - nameSpace - RIGHT_MARGIN) / (time1 - time0);
-        int x = getBounds().x + nameSpace + (int) ((time - time0) * pixelsPerNanoSec);
+        int x = SaturatedArithmetic.add(getBounds().x + nameSpace, (int) ((time - time0) * pixelsPerNanoSec));
         return x;
     }
 
@@ -1669,8 +1670,8 @@ public class TimeGraphControl extends TimeGraphBaseControl
         long selectionBegin = fTimeProvider.getSelectionBegin();
         long selectionEnd = fTimeProvider.getSelectionEnd();
         double pixelsPerNanoSec = (bounds.width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (bounds.width - nameSpace - RIGHT_MARGIN) / (time1 - time0);
-        int x0 = bounds.x + nameSpace + (int) ((selectionBegin - time0) * pixelsPerNanoSec);
-        int x1 = bounds.x + nameSpace + (int) ((selectionEnd - time0) * pixelsPerNanoSec);
+        int x0 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionBegin - time0) * pixelsPerNanoSec));
+        int x1 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionEnd - time0) * pixelsPerNanoSec));
 
         // draw selection lines
         if (fDragState != DRAG_SELECTION) {
@@ -1961,8 +1962,8 @@ public class TimeGraphControl extends TimeGraphBaseControl
             int lastX = -1;
             while (iterator.hasNext()) {
                 ITimeEvent event = iterator.next();
-                int x = rect.x + (int) ((event.getTime() - time0) * pixelsPerNanoSec);
-                int xEnd = rect.x + (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec);
+                int x = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() - time0) * pixelsPerNanoSec));
+                int xEnd = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec));
                 if (x >= rect.x + rect.width || xEnd < rect.x) {
                     // event is out of bounds
                     continue;
index f9a1b7560cd584b56dd19a29e3fdbab707ab969a..4cecd5708c0dc7043d2b5520953ff5c130ce6687 100644 (file)
@@ -33,6 +33,7 @@ import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.tracecompass.common.core.math.SaturatedArithmetic;
 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
@@ -366,8 +367,8 @@ public class TimeGraphScale extends TimeGraphBaseControl implements
             int x2;
             long selectionBegin = fTimeProvider.getSelectionBegin();
             long selectionEnd = fTimeProvider.getSelectionEnd();
-            x1 = leftSpace + (int) ((selectionBegin - time0) * pixelsPerNanoSec);
-            x2 = leftSpace + (int) ((selectionEnd - time0) * pixelsPerNanoSec);
+            x1 = SaturatedArithmetic.add(leftSpace, (int) ((selectionBegin - time0) * pixelsPerNanoSec));
+            x2 = SaturatedArithmetic.add(leftSpace, (int) ((selectionEnd - time0) * pixelsPerNanoSec));
             drawRangeDecorators(rect0, gc, x1, x2);
         }
 
@@ -398,7 +399,7 @@ public class TimeGraphScale extends TimeGraphBaseControl implements
 
         List<Integer> tickList = new ArrayList<>();
         while (true) {
-            int x = rect.x + leftSpace + (int) (Math.floor((time - time0) * pixelsPerNanoSec));
+            int x = SaturatedArithmetic.add(rect.x + leftSpace, (int) (Math.floor((time - time0) * pixelsPerNanoSec)));
             if (x >= rect.x + leftSpace + rect.width - rect0.width) {
                 break;
             }
This page took 0.029134 seconds and 5 git commands to generate.