Skip to content

Commit 8101cbf

Browse files
authoredJul 5, 2024
[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 llvm/llvm-project#94616.
1 parent eb7ebd5 commit 8101cbf

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed
 

‎openmp/libompd/src/TargetValue.h

+21-16
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,27 @@ class TBaseValue : public TValue {
231231

232232
template <typename T> ompd_rc_t TBaseValue::getValue(T &buf) {
233233
assert(sizeof(T) >= baseTypeSize);
234-
ompd_rc_t ret = getValue(&buf, 1);
235-
if (sizeof(T) > baseTypeSize) {
236-
switch (baseTypeSize) {
237-
case 1:
238-
buf = (T) * ((int8_t *)&buf);
239-
break;
240-
case 2:
241-
buf = (T) * ((int16_t *)&buf);
242-
break;
243-
case 4:
244-
buf = (T) * ((int32_t *)&buf);
245-
break;
246-
case 8:
247-
buf = (T) * ((int64_t *)&buf);
248-
break;
249-
}
234+
if (sizeof(T) == baseTypeSize)
235+
return getValue(&buf, 1);
236+
237+
char tmp[sizeof(T)];
238+
ompd_rc_t ret = getValue(tmp, 1);
239+
switch (baseTypeSize) {
240+
case 1:
241+
buf = (T) * ((int8_t *)tmp);
242+
break;
243+
case 2:
244+
buf = (T) * ((int16_t *)tmp);
245+
break;
246+
case 4:
247+
buf = (T) * ((int32_t *)tmp);
248+
break;
249+
case 8:
250+
buf = (T) * ((int64_t *)tmp);
251+
break;
252+
default:
253+
assert(0 && "Invalid baseTypeSize");
254+
break;
250255
}
251256
return ret;
252257
}

0 commit comments

Comments
 (0)
Please sign in to comment.