Skip to content

analyzer: Thinks result of __builtin_mul_overflow can be uninitialized #136292

@Zentrik

Description

@Zentrik
#include <stddef.h>

int test(size_t nel, size_t elsz) {
  size_t nbytes;
  int overflow = __builtin_mul_overflow(nel, elsz, &nbytes);
  int overflow2 = __builtin_add_overflow(nel, nbytes, &nbytes);
  return overflow * overflow2;
}
> clang++ --analyze -Xclang -analyzer-output=text -std=c++20
clang++: warning: argument unused during compilation: '-S' [-Wunused-command-line-argument]
<source>:6:19: warning: 2nd function call argument is an uninitialized value [core.CallAndMessage]
    6 |   int overflow2 = __builtin_add_overflow(nel, nbytes, &nbytes);
      |                   ^                           ~~~~~~
<source>:4:3: note: 'nbytes' declared without an initial value
    4 |   size_t nbytes;
      |   ^~~~~~~~~~~~~
<source>:5:18: note: Assuming overflow
    5 |   int overflow = __builtin_mul_overflow(nel, elsz, &nbytes);
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:6:19: note: 2nd function call argument is an uninitialized value
    6 |   int overflow2 = __builtin_add_overflow(nel, nbytes, &nbytes);
      |                   ^                           ~~~~~~
1 warning generated.
Compiler returned: 0

Godbolt: https://godbolt.org/z/4q4Efcax8

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions