<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/66470>66470</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Miscompilation in RawTherapee with Clang 15+ and optimization
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang,
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Floessie
</td>
</tr>
</table>
<pre>
Hello dear LLVM team,
over at RawTherapee we got a bug report of a SEGV happening when compiled with Clang 16:
https://github.com/Beep6581/RawTherapee/issues/6847
I was able to reduce the test case and can share it here:
```c++
namespace
{
template<typename T>
bool assignFromData(
bool has_params_edited,
T& value,
bool& params_edited_value
)
{
if (has_params_edited) {
params_edited_value = true;
}
return true;
}
struct GeneralParamsEdited {
bool v0;
bool v1;
};
struct ParamsEdited {
GeneralParamsEdited general;
};
}
int main(int argc, char** argv)
{
ParamsEdited* const pedited =
argc > 1
? new ParamsEdited
: nullptr;
int v0;
int v1;
assignFromData(pedited, v0, pedited->general.v0);
assignFromData(pedited, v1, pedited->general.v1);
return 0;
}
```
Same code in Godbolt:
https://godbolt.org/z/aW7dEYTb9
The reduced code segfaults when compiled with Clang 15 or 16 with `-Og`. When changing the `has_params_edited` parameter in `assignFromData()` from `bool` to `void*` the problem is gone. The difference in assembly between the two versions shows why:
```diff
--- fail.log
+++ good.log
@@ -12,9 +12,8 @@
test rax, rax
je .LBB0_5
.LBB0_4:
- mov byte ptr [rax], 1
+ mov word ptr [rax], 257
.LBB0_5:
- mov byte ptr [rax + 1], 1
xor eax, eax
pop rcx
ret
```
I'm aware that passing `pedited->general.v[01]` with `pedited == nullptr` is UB, but this shouldn't affect `has_params_edited` which is meant to protect exactly against this during runtime.
Best regards,
Flössie
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVkGP4jgT_TXmUgI5DgnkwKGBZr6RZvStdnpntKeWkxSJR44d2Q5Mz69fVUL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