llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.26k stars 11.67k forks source link

[analyzer] false-positive garbage value warning with multi-demensional constant array access #53518

Open ueno opened 2 years ago

ueno commented 2 years ago

With clang version 13.0.0 (Fedora 13.0.0-3.fc35), I got the following warning when compiling under scan-build:

gost/kuznyechik.c:112:33: warning: The right operand of '^' is a garbage value [core.UndefinedBinaryOperatorResult]
  memcpy(t, &kuz_table[0][(b[0] ^ c[0]) * 16], KUZNYECHIK_BLOCK_SIZE);
                                ^ ~~~~
1 warning generated.

The code path is as follows:

  1. subkey is called with the 3rd argument i set to 0
  2. LSX is called with the 3rd argument c pointing to kuz_key_table[i + 2]
  3. The above line is hit

kuz_key_table is defined as a constant 2-dimensional array (static const uint8_t kuz_key_table[32][16]) and the above access never involves out-of-bounds. Perhaps #49948 might be related.

t184256 commented 2 years ago

Extracted out and trimmed down:

int table[2][2] = { {1, 2}, {3, 4} };
int f(const int* c) { return c[0]; }
int main() { return f(table[1]); }
test.c:2:23: warning: Undefined or garbage value returned to caller [core.uninitialized.UndefReturn]
int f(const int* c) { return c[0]; }
                      ^~~~~~~~~~~

Curiously, passing table[0] or dropping the const modifier makes the warning go away.