Skip to content

[bufferoverrun] fix the minimum of two capped symbolic bounds - #2333

Closed
VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:pr/bufferoverrun-capped-bounds-1125
Closed

VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:pr/bufferoverrun-capped-bounds-1125

Conversation

@VladimirMakaev

Copy link
Copy Markdown
Contributor

A bound c - max(d, s) is capped at c - d, the value it takes once the max picks the
constant. For two such bounds exact_min combined the caps with max instead of min, so for
incomparable operands it returned a value above their true minimum. Through underapprox_min
and overapprox_max that made the interval join drop values, and overruns on the dropped
values went unreported.

int pick(int x, int b) {
  int k = x; if (k < -1) k = -1; k = k + 1; /* 1 + max(-1, x) */
  int m = x; if (m < 2) m = 2;              /* max(2, x) */
  return b ? k : m; /* joined to 1 + max(-1, x), which is 0 at x = -20, so */
}                   /* a[pick(-20, g())] = 0 on a two-element array was silent */

The join is now 1 + max(1, x), the exact maximum, so no precision is given up. The same
branch is reached from overapprox_min and underapprox_max, where the old value was sound
but loose and is now exact as well, so pruning and min/max can get tighter. The companion
case for c + min(d, s) already combined the caps with min and is unchanged.

Test plan

New C cases in codetoanalyze/c/bufferoverrun/minmax.c for the joined upper bound, the joined
lower bound and an unsigned symbol: each overrun is now reported and each in-bounds companion
stays silent. Every other expectation is unchanged.

The macOS build and the Objective-C buffer-overrun target this change affects also pass:
VladimirMakaev#13

A bound `c - max(d, s)` is capped at `c - d`, the value it takes once the max picks the
constant. For two such bounds `exact_min` combined the caps with `max` instead of `min`, so for
incomparable operands it returned a value above their true minimum. Through `underapprox_min`
and `overapprox_max` that made the interval join drop values, and overruns on the dropped
values went unreported.

    int pick(int x, int b) {
      int k = x; if (k < -1) k = -1; k = k + 1; /* 1 + max(-1, x) */
      int m = x; if (m < 2) m = 2;              /* max(2, x) */
      return b ? k : m; /* joined to 1 + max(-1, x), which is 0 at x = -20, so */
    }                   /* a[pick(-20, g())] = 0 on a two-element array was silent */

The join is now `1 + max(1, x)`, the exact maximum, so no precision is given up. The same
branch is reached from `overapprox_min` and `underapprox_max`, where the old value was sound
but loose and is now exact as well, so pruning and `min`/`max` can get tighter. The companion
case for `c + min(d, s)` already combined the caps with `min` and is unchanged.

## Test plan
New C cases in `codetoanalyze/c/bufferoverrun/minmax.c` for the joined upper bound, the joined
lower bound and an unsigned symbol: each overrun is now reported and each in-bounds companion
stays silent. Every other expectation is unchanged.
@meta-cla meta-cla Bot added the CLA Signed label Nov 26, 2026
@dulmarod

Copy link
Copy Markdown
Contributor

bufferoverrun has been deprecated for a long time with no one with expertise around. I doubt we will get to a place where we can find real issues, but let me know if you have a plan here.

@VladimirMakaev

Copy link
Copy Markdown
Contributor Author

Thanks, closing this: we'll keep the bufferoverrun changes in our fork and won't pursue them upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants