什么是 "false positive"?

What is a "false positive"?

我在 Understanding Strict Aliasing 上注意到以下代码:

uint32_t
swap_words( uint32_t arg )
{
    U32*     in = (U32*)&arg;
    uint16_t lo = in->u16[0];
    uint16_t hi = in->u16[1];

    in->u16[0] = hi;
    in->u16[1] = lo;

    return (in->u32);
}

据作者介绍,

The above source when compiled with GCC 4.0 with the -Wstrict-aliasing=2 flag enabled will generate a warning. This warning is an example of a false positive. This type of cast is allowed and will generate the appropriate code (see below). It is documented clearly that -Wstrict-aliasing=2 may return false positives.

我想知道什么是误报,在别名变量时我应该注意它吗?


这里是上面提到的"appropriate code (see below)",如果相关的话:

在 GNU C 版本 4.0.0(Apple Computer, Inc. build 5026)(powerpc-apple-darwin8) 上使用 -fstrict-aliasing -O3 -Wstrict-aliasing -std=c99 编译,

swap_words:
  stw r3,24(r1)  ; Store arg
  lhz r0,24(r1)  ; Load hi
  lhz r2,26(r1)  ; Load lo
  sth r0,26(r1)  ; Store result[1] = hi
  sth r2,24(r1)  ; Store result[0] = lo
  lwz r3,24(r1)  ; Load result
  blr            ; Return

gcc 使用特殊的启发式方法来查找与严格的别名规则有关的问题。这些试探法可能有误报,即报告警告但没有任何可报告的情况。

这里的“误报”是指警告不正确,即这里没有问题。 假阳性 是科学和医学中的标准术语,表示评估过程中的错误导致 mistaken detection of a condition tested。 (假阴性 将在适当的时候未能发出警告。)

如果您确定警告是误报,则无需修复代码即可将其删除。您可能仍想重组代码以避免编译警告(或者如果可能,关闭该代码部分的警告)。通常无警告的编译确保当真正的警告出现时,您应该注意,您不会错过它。

误报 通常意味着当某些东西假装答案是肯定的/符合某些条件但实际上它不是真的。

所以这里 gcc 给你一个警告,关于从整数到包含两个 16 位整数的相同大小的结构的不正确转换,因为这是危险的操作。但是你知道它实际上是正确的并生成正确的代码。