diff options
| author | Nikita Popov <npopov@redhat.com> | 2024-07-05 09:26:54 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-05 09:26:54 +0200 |
| commit | 8101cbff77aca47ee3c7de8e43d0546057ff2ee6 (patch) | |
| tree | 4de857d9d30a94b7851689039b2037d37ad8d44a /openmp/libompd/src | |
| parent | eb7ebd51ecfdbf541044761851dd2007368d710f (diff) | |
[ompd] Fix strict aliasing violation in TargetValue::getValue() (#97739)
For the case where baseTypeSize does not match the size of T, read the
value into a separate char buffer first. This avoids accessing buf using
two different types.
Fixes https://github.com/llvm/llvm-project/issues/94616.
Diffstat (limited to 'openmp/libompd/src')
| -rw-r--r-- | openmp/libompd/src/TargetValue.h | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/openmp/libompd/src/TargetValue.h b/openmp/libompd/src/TargetValue.h index 87e4575885e7..d7429f7705df 100644 --- a/openmp/libompd/src/TargetValue.h +++ b/openmp/libompd/src/TargetValue.h @@ -231,22 +231,27 @@ public: template <typename T> ompd_rc_t TBaseValue::getValue(T &buf) { assert(sizeof(T) >= baseTypeSize); - ompd_rc_t ret = getValue(&buf, 1); - if (sizeof(T) > baseTypeSize) { - switch (baseTypeSize) { - case 1: - buf = (T) * ((int8_t *)&buf); - break; - case 2: - buf = (T) * ((int16_t *)&buf); - break; - case 4: - buf = (T) * ((int32_t *)&buf); - break; - case 8: - buf = (T) * ((int64_t *)&buf); - break; - } + if (sizeof(T) == baseTypeSize) + return getValue(&buf, 1); + + char tmp[sizeof(T)]; + ompd_rc_t ret = getValue(tmp, 1); + switch (baseTypeSize) { + case 1: + buf = (T) * ((int8_t *)tmp); + break; + case 2: + buf = (T) * ((int16_t *)tmp); + break; + case 4: + buf = (T) * ((int32_t *)tmp); + break; + case 8: + buf = (T) * ((int64_t *)tmp); + break; + default: + assert(0 && "Invalid baseTypeSize"); + break; } return ret; } |
