Skip to content

Commit

Permalink
ui: Sanitize timespan passed to timeline.updateVisibleTime()
Browse files Browse the repository at this point in the history
- Don't allow the timespan to become shorter than 10ns.
- Use timespan.fitWithin rather than timespan.intersect to avoid
  changing the duration.

Bug: 352566470
Change-Id: I1f85b46f80bb25e58ff0ddeaa72e7ef9a111883f
  • Loading branch information
stevegolton committed Jul 26, 2024
1 parent 439b00c commit c9bf44b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
12 changes: 12 additions & 0 deletions ui/src/common/high_precision_time_span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ export class HighPrecisionTimeSpan {
return this;
}

/**
* Clamp duration to some minimum value. The start remains the same, just the
* duration is changed.
*/
clampDuration(minDuration: number): HighPrecisionTimeSpan {
if (this.duration < minDuration) {
return new HighPrecisionTimeSpan(this.start, minDuration);
} else {
return this;
}
}

/**
* Checks whether this span completely contains a time instant.
*/
Expand Down
9 changes: 9 additions & 0 deletions ui/src/common/high_precision_time_span_unittest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ describe('HighPrecisionTimeSpan', () => {
expect(result.duration).toBeCloseTo(1);
});

test('clampDuration', () => {
const span = new HPTimeSpan(hptime('5'), 1);
const clamped = span.clampDuration(10);

expect(clamped.start.integral).toBe(5n);
expect(clamped.start.fractional).toBeCloseTo(0);
expect(clamped.duration).toBeCloseTo(10);
});

test('equality', () => {
const span = new HPTimeSpan(hptime('10'), 10);
expect(span.equals(span)).toBe(true);
Expand Down
9 changes: 4 additions & 5 deletions ui/src/frontend/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {HighPrecisionTimeSpan} from '../common/high_precision_time_span';
import {Area, State} from '../common/state';
import {raf} from '../core/raf_scheduler';
import {Store} from '../public';

import {ratelimit} from './rate_limiters';

interface Range {
Expand Down Expand Up @@ -110,10 +109,10 @@ export class Timeline {

// Set visible window using a high precision time span
updateVisibleTimeHP(ts: HighPrecisionTimeSpan) {
this._visibleWindow = ts.intersect(
this.traceSpan.start,
this.traceSpan.end,
);
this._visibleWindow = ts
.clampDuration(MIN_DURATION)
.fitWithin(this.traceSpan.start, this.traceSpan.end);

this.rateLimitedPoker();
}

Expand Down

0 comments on commit c9bf44b

Please sign in to comment.