https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129094

            Bug ID: 129094
           Summary: [analyzer] range::eval_condition gives UNKNOWN for x
                    >= C / x <= C when C is at the range bound
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: xan9xxx at gmail dot com
  Target Milestone: ---

$ cat test.c
extern void __analyzer_eval (int);

void
test_lower (int x)
{
  int bound = 3;
  if (x < 3)
    return;
  __analyzer_eval (x >= 3);     /* TRUE */
  __analyzer_eval (x >= bound); /* UNKNOWN; expected TRUE since bound is 3 */
}

void
test_upper (int x)
{
  int limit = 4;
  if (x >= limit)
    return;
  __analyzer_eval (x < limit); /* TRUE */
  __analyzer_eval (x <= 3);    /* UNKNOWN; expected TRUE */
}

$ gcc -O0 -fanalyzer -c test.c
test.c: In function 'test_upper':
test.c:19:3: warning: TRUE
test.c:20:3: warning: UNKNOWN
test.c: In function 'test_lower':
test.c:9:3: warning: TRUE
test.c:10:3: warning: UNKNOWN

Tested with GCC 17.0.0 20261126 (experimental) on x86_64-pc-linux-gnu.
Source tree HEAD: 48688cfeadf6ec3b2dd22783f5048db6fc444d04
Also reproduces with Debian GCC 14.2.0.

In test_lower, the literal comparison x >= 3 is already x > 2 in GIMPLE
at -O0, whereas x >= bound retains the >= operator.  In test_upper, the
analyzer knows that x < limit, and limit is 4, so x <= 3 should also be
TRUE.  Here x <= 3 is kept as LE_EXPR in GIMPLE, so no variable is
needed.

In gcc/analyzer/constraint-manager.cc, range::eval_condition handles
GE_EXPR and GT_EXPR together.  At a closed lower bound of 3,
below_lower_bound (3) is false, so both comparisons give UNKNOWN.  That
is the expected result for x > 3, but x >= 3 is known to be true.  The
LE_EXPR/LT_EXPR case has the same issue at a closed upper bound.

I noticed this while testing closefrom (3) for PR106283. On a path
where fd > 2, the closefrom model asks whether fd >= 3. The analyzer
stops tracking fd instead of marking it closed, so a subsequent
close (fd) does not trigger -Wanalyzer-fd-double-close.

Reply via email to