llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[analyzer] False positive alpha.unix.cstring.UninitializedRead on pointer arithmetic across sub-array boundary

Open earnol opened this issue 6 months ago • 4 comments

After #198346 alpha.unix.cstring.UninitializedRead reports a false positive when a pointer into a fully-initialized multidimensional array is advanced past the innermost dimension boundary and then used as a source argument to memcpy.

The array is fully zero-initialized with = {}, so all elements are defined. The pointer arithmetic crosses a sub-array boundary (which is technically UB per strict C24 standard reading of §6.5.7p8), but the memory is contiguous and initialized. The checker incorrectly reports the element as "undefined" rather than "out of bounds" — conflating sub-array boundary crossing with uninitialized memory.

Reproducer

typedef long a;
const a b[4][1200] = {};
void *memcpy(void *, const void *, unsigned long);
extern void *dst[];
void e() {
  const a *f = &b[0][0];
  int g = 2001;
  for (unsigned long i = 0; i < 2; i++) {
    memcpy(dst[i], f, sizeof(a));
    f = f + g;
  }
}
$ clang --analyze -Xclang -analyzer-checker=alpha.unix.cstring.UninitializedRead -Xclang -analyzer-checker=core -Xclang -analyzer-output=text test.c

test.c:9:5: warning: The first element of the 2nd argument is undefined [alpha.unix.cstring.UninitializedRead]
    memcpy(dst[i], f, sizeof(a));
    ^

Godbolt link available here.

The warning fires on the second loop iteration when f has been advanced by 2001 elements. The array b has innermost dimension 1200, so f + 2001 crosses into b[1][801]. The total array size is 4 * 1200 = 4800 elements, so offset 2001 is well within the flat array bounds, and the entire array is zero-initialized.

Expected behavior

No UninitializedRead warning. The memory at f + 2001 is initialized (the entire array is = {}).

If the analyzer wants to warn about pointer arithmetic crossing sub-array boundaries, it should use a different checker/message (e.g., alpha.unix.cstring.OutOfBounds or a new diagnostic), not UninitializedRead.

Bisection

  • Fires: after https://github.com/llvm/llvm-project/pull/198346 (commit 51279e7e0527)
  • Silent: before that commit

The PR fixed CStringChecker::checkInit() to correctly resolve buffer start offsets for offset buffer arguments. As a side effect, it now treats elements beyond the innermost sub-array dimension as "undefined" even when the containing array is fully initialized.

Impact

This causes false positives in codebases that use pointer arithmetic on multidimensional arrays as flat buffers.

earnol avatar May 22 '26 19:05 earnol

@llvm/issue-subscribers-clang-static-analyzer

Author: earnol

After #198346 alpha.unix.cstring.UninitializedRead reports a false positive when a pointer into a fully-initialized multidimensional array is advanced past the innermost dimension boundary and then used as a source argument to memcpy.

The array is fully zero-initialized with = {}, so all elements are defined. The pointer arithmetic crosses a sub-array boundary (which is technically UB per strict C24 standard reading of §6.5.6p8), but the memory is contiguous and initialized. The checker incorrectly reports the element as "undefined" rather than "out of bounds" — conflating sub-array boundary crossing with uninitialized memory.

Reproducer

typedef long a;
const a b[4][1200] = {};
void *memcpy(void *, const void *, unsigned long);
extern void *dst[];
void e() {
  const a *f = &amp;b[0][0];
  int g = 2001;
  for (unsigned long i = 0; i &lt; 2; i++) {
    memcpy(dst[i], f, sizeof(a));
    f = f + g;
  }
}
$ clang --analyze -Xclang -analyzer-checker=alpha.unix.cstring.UninitializedRead -Xclang -analyzer-checker=core -Xclang -analyzer-output=text test.c

test.c:9:5: warning: The first element of the 2nd argument is undefined [alpha.unix.cstring.UninitializedRead]
    memcpy(dst[i], f, sizeof(a));
    ^

Godbolt link available here.

The warning fires on the second loop iteration when f has been advanced by 2001 elements. The array b has innermost dimension 1200, so f + 2001 crosses into b[1][801]. The total array size is 4 * 1200 = 4800 elements, so offset 2001 is well within the flat array bounds, and the entire array is zero-initialized.

Expected behavior

No UninitializedRead warning. The memory at f + 2001 is initialized (the entire array is = {}).

If the analyzer wants to warn about pointer arithmetic crossing sub-array boundaries, it should use a different checker/message (e.g., alpha.unix.cstring.OutOfBounds or a new diagnostic), not UninitializedRead.

Bisection

  • Fires: after https://github.com/llvm/llvm-project/pull/198346 (commit 51279e7e0527)
  • Silent: before that commit

The PR fixed CStringChecker::checkInit() to correctly resolve buffer start offsets for offset buffer arguments. As a side effect, it now treats elements beyond the innermost sub-array dimension as "undefined" even when the containing array is fully initialized.

Impact

This causes false positives in codebases that use pointer arithmetic on multidimensional arrays as flat buffers.

@gamesh411 Could you check this issue?

NagyDonat avatar May 26 '26 08:05 NagyDonat

I also noticed additional issue:

typedef long a;
const a b[4][1200];
void *memcpy(void *, const void *, unsigned long);
extern void *dst[];
void e() {
  const a *f = &b[0][0];
  int g = 2001;
  for (unsigned long i = 0; i < 2; i++) {
    memcpy(dst[i], f, sizeof(a));
    f = f + g;
  }
}

Does not produce ANY diagnostics. But from language perspective b is global variable so it is also zero initialized. Static analyzed does not account for it. But it's looks like an separate issue. Do you advice me to file the separate issue?

earnol avatar May 26 '26 20:05 earnol

Hey @earnol! Thanks for reporting this regression. I hope to handle it at the RegionStore level, but as a fallback, we can also fix the handling of multidimensional C arrays in the checker. Still, I think the entire infrastructure would benefit from the fix in #200044.

gamesh411 avatar May 28 '26 08:05 gamesh411