<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/58359>58359</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Wrong return type for __get_cpuid_max in cpuid.h
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
knatten
</td>
</tr>
</table>
<pre>
The function [__get_cpuid_max](https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/cpuid.h#L264-L292) returns `int`, but should return `unsigned int`.
The type of the expression (`__eax`) it is trying to return is `unsigned int`:
```c
static __inline int __get_cpuid_max (unsigned int __leaf, unsigned int *__sig)
{
unsigned int __eax, __ebx, __ecx, __edx;
// Redacted
return __eax;
}
```
And also [callers](https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/cpuid.h#L298) expect an `unsigned int`:
```c
unsigned int __max_leaf = __get_cpuid_max(__leaf & 0x80000000, 0);
```
It looks like the signature should be changed to return `unsigned int` instead of `int`.
If you run a [reproducer with clang 15](https://godbolt.org/z/oh14c55bn), ubsan also reports:
> /opt/compiler-explorer/clang-15.0.0/lib/clang/15.0.0/include/cpuid.h:291:12: runtime error: implicit conversion from type 'unsigned int' of value 2147483656 (32-bit, unsigned) to type 'int' changed the value to -2147483640 (32-bit, signed)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /opt/compiler-explorer/clang-15.0.0/lib/clang/15.0.0/include/cpuid.h:291:12 in
/opt/compiler-explorer/clang-15.0.0/lib/clang/15.0.0/include/cpuid.h:298:31: runtime error: implicit conversion from type 'int' of value -2147483640 (32-bit, signed) to type 'unsigned int' changed the value to 2147483656 (32-bit, unsigned)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /opt/compiler-explorer/clang-15.0.0/lib/clang/15.0.0/include/cpuid.h:298:31 in
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVV2P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