summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-03-07 22:02:35 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-03-08 19:35:24 +0200
commit4de31ccc9ef47e7b16330d226d071d5d006faa6d (patch)
tree44d90a288dffdf92b3678f76c6e1e8b148e443fb
parent291d4878bba51603f875257d5b6b3c99b62b6685 (diff)
libcamera: controls: Fix strict aliasing violation
gcc 8.3.0 for ARM complains about strict aliasing violations: ../../src/libcamera/controls.cpp: In member function ‘void libcamera::ControlValue::release()’: ../../src/libcamera/controls.cpp:111:13: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] delete[] *reinterpret_cast<char **>(&storage_); Fix it and simplify the code at the same time. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
-rw-r--r--include/libcamera/controls.h5
-rw-r--r--src/libcamera/controls.cpp20
2 files changed, 14 insertions, 11 deletions
diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h
index 4767e2d3..0e111ab7 100644
--- a/include/libcamera/controls.h
+++ b/include/libcamera/controls.h
@@ -160,7 +160,10 @@ private:
ControlType type_ : 8;
bool isArray_ : 1;
std::size_t numElements_ : 16;
- uint64_t storage_;
+ union {
+ uint64_t value_;
+ void *storage_;
+ };
void release();
void set(ControlType type, bool isArray, const void *data,
diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp
index 94bdbdd9..4326174a 100644
--- a/src/libcamera/controls.cpp
+++ b/src/libcamera/controls.cpp
@@ -107,9 +107,9 @@ void ControlValue::release()
{
std::size_t size = numElements_ * ControlValueSize[type_];
- if (size > sizeof(storage_)) {
- delete[] *reinterpret_cast<char **>(&storage_);
- storage_ = 0;
+ if (size > sizeof(value_)) {
+ delete[] reinterpret_cast<uint8_t *>(storage_);
+ storage_ = nullptr;
}
}
@@ -176,9 +176,9 @@ ControlValue &ControlValue::operator=(const ControlValue &other)
Span<const uint8_t> ControlValue::data() const
{
std::size_t size = numElements_ * ControlValueSize[type_];
- const uint8_t *data = size > sizeof(storage_)
- ? *reinterpret_cast<const uint8_t * const *>(&storage_)
- : reinterpret_cast<const uint8_t *>(&storage_);
+ const uint8_t *data = size > sizeof(value_)
+ ? reinterpret_cast<const uint8_t *>(storage_)
+ : reinterpret_cast<const uint8_t *>(&value_);
return { data, size };
}
@@ -308,11 +308,11 @@ void ControlValue::set(ControlType type, bool isArray, const void *data,
std::size_t size = elementSize * numElements;
void *storage;
- if (size > sizeof(storage_)) {
- storage = reinterpret_cast<void *>(new char[size]);
- *reinterpret_cast<void **>(&storage_) = storage;
+ if (size > sizeof(value_)) {
+ storage_ = reinterpret_cast<void *>(new uint8_t[size]);
+ storage = storage_;
} else {
- storage = reinterpret_cast<void *>(&storage_);
+ storage = reinterpret_cast<void *>(&value_);
}
memcpy(storage, data, size);