OSDN Git Service

iio-sensors: avoid memset a dynamic class
authorChih-Wei Huang <cwhuang@linux.org.tw>
Thu, 23 Nov 2017 06:18:27 +0000 (14:18 +0800)
committerChih-Wei Huang <cwhuang@linux.org.tw>
Thu, 23 Nov 2017 06:18:27 +0000 (14:18 +0800)
A dynamic class doesn't support static initialization. The compiler
complains:

hardware/libsensors/iio-sensors.cpp:119:9: warning: destination for this 'memset' call is a pointer to dynamic class 'SensorBase'; vtable pointer will be overwritten [-Wdynamic-class-memaccess]
        memset(this, 0, sizeof(SensorBase));
        ~~~~~~ ^

Move all data members of SensorBase to a trivial class which could be
statically initialized.

Refer: http://en.cppreference.com/w/cpp/types/is_trivial

iio-sensors.cpp

index ef7436a..a94e06b 100644 (file)
@@ -92,7 +92,17 @@ SensorEvent::SensorEvent(int32_t id, int32_t t)
        timestamp = int64_t(ts.tv_sec) * NSEC_PER_SEC + ts.tv_nsec;
 }
 
-class SensorBase : public sensor_t {
+class sensor_base : public sensor_t {
+  protected:
+       sensor_base() { memset(this, 0, sizeof(*this)); }
+
+       bool enabled;
+       char *path;
+       const char ***nodes;
+       struct timespec delay;
+};
+
+class SensorBase : public sensor_base {
   public:
        SensorBase();
        virtual ~SensorBase();
@@ -107,17 +117,10 @@ class SensorBase : public sensor_t {
        int read_sysfs_str(const char *file, char *buf);
        int read_sysfs_int(const char *file);
        float read_sysfs_float(const char *file);
-
-       bool enabled;
-       char *path;
-       const char ***nodes;
-       struct timespec delay;
 };
 
 SensorBase::SensorBase()
 {
-       memset(this, 0, sizeof(SensorBase));
-
        vendor = "Android-x86 Open Source Project";
        version = 1;