Issue |
131789
|
Summary |
[clang-20] Incorrect result when comparing negative long int with unsigned int
|
Labels |
new issue
|
Assignees |
|
Reporter |
erxiaozhou
|
## Description
I've encountered an issue in clang-20 where comparing a negative long int with an unsigned int produces an incorrect result. The comparison of `-2L > a` (where `a` is an unsigned int with value 0) returns 0 (false), which contradicts the C standard's type conversion rules.
## Reduced Test Case
```c
#include <stdio.h>
unsigned int a = 0;
int main() {
printf("%u\n", -2L > a);
}
```
## Compiler Information
- Compiler: clang-20
- Compilation command: `clang-20 -o test test.c`
## Actual vs Expected Behavior
- **Actual output**: 0
- **Expected output**: 1
## Analysis
According to the C standard (C17 6.3.1.8), when a signed integer is compared with an unsigned integer, the signed integer should be converted to the unsigned type. When a negative value like `-2L` is converted to unsigned, it should result in a large positive value (following two's complement conversion).
Therefore, the _expression_ `-2L > a` should:
1. Convert `-2L` to an unsigned long int
2. This conversion results in a very large positive number (ULONG_MAX - 1)
3. This large positive number is compared with 0
4. The comparison should evaluate to true (1)
The fact that clang-20 returns 0 indicates a potential mishandling of the type conversion rules in this specific case.
## System Information
- Operating System: Ubuntu-20.04
- Architecture: x86-64
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs