1

I'm trying to get my code clang-tidy clean, and it's complaining that I'm using garbage values.

I have some data stored as words, and I want to use it byte by byte.

clang-tidy doesn't seem to identify the array size post c-style cast.

A minimal example:

#include <stdio.h>

int main(void) {
  int words[2] = { 123456789, 987654321 };
  char* bytes = (char*)words;

  printf("%d\n", bytes[1] * 2);
  return 0;
}

The error:

/home/ce/example.c:7:27: warning: The left operand of '*' is a garbage value [clang-analyzer-core.UndefinedBinaryOperatorResult]
  printf("%d\n", bytes[1] * 2);
                          ^
/home/ce/example.c:7:27: note: The left operand of '*' is a garbage value

Note: I'm using c99

user100046
  • 422
  • 4
  • 11
  • Came across this while searching for clang-tidy answers myself. This does not work regardless of clang-tidy. I'm curious as to what you expect to see after taking an array of two ints, where the size of int depends on the implementation but is at least 2 bytes, and then truncating it to 1 char before printing it as an int? On a sidenote, if you want to talk about `words` it's portable to use the fixed sized alternatives eg `typedef uint16_t WORD` available in stdint.h. – firmament Nov 28 '20 at 22:34

0 Answers0