f598fdc820ed43647b8f6dc0ac38763b9aaf66e4
[AGL/meta-agl.git] / meta-security / recipes-security / security-manager / security-manager / 0001-Avoid-casting-from-const-T-to-void.patch
1 From 14c8842ed8a37fecbc70d46e27b49ae929b0c85f Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Jos=C3=A9=20Bollo?= <jose.bollo@iot.bzh>
3 Date: Fri, 1 Feb 2019 15:37:44 +0100
4 Subject: [PATCH] Avoid casting from "const T&" to "void*"
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Latest version of g++ refuse the cast
10
11      reinterpret_cast<void (Service::*)(void*)>(serviceFunction)
12
13 I made no investigation to know if the problem
14 is coming from the const or not.
15
16 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
17 ---
18  src/server/main/include/service-thread.h | 43 ++++++++++--------------
19  1 file changed, 18 insertions(+), 25 deletions(-)
20
21 diff --git a/src/server/main/include/service-thread.h b/src/server/main/include/service-thread.h
22 index 964d168..92b0ec8 100644
23 --- a/src/server/main/include/service-thread.h
24 +++ b/src/server/main/include/service-thread.h
25 @@ -9,78 +94,72 @@ public:
26              Join();
27          while (!m_eventQueue.empty()){
28              auto front = m_eventQueue.front();
29 -            delete front.eventPtr;
30 +            delete front;
31              m_eventQueue.pop();
32          }
33      }
34  
35      template <class T>
36      void Event(const T &event,
37                 Service *servicePtr,
38                 void (Service::*serviceFunction)(const T &))
39      {
40 -        EventDescription description;
41 -        description.serviceFunctionPtr =
42 -            reinterpret_cast<void (Service::*)(void*)>(serviceFunction);
43 -        description.servicePtr = servicePtr;
44 -        description.eventFunctionPtr = &ServiceThread::EventCall<T>;
45 -        description.eventPtr = new T(event);
46 +        EventCallerBase *ec = new EventCaller<T>(event, servicePtr, serviceFunction);
47          {
48              std::lock_guard<std::mutex> lock(m_eventQueueMutex);
49 -            m_eventQueue.push(description);
50 +            m_eventQueue.push(ec);
51          }
52          m_waitCondition.notify_one();
53      }
54  
55  protected:
56  
57 -    struct EventDescription {
58 -        void (Service::*serviceFunctionPtr)(void *);
59 -        Service *servicePtr;
60 -        void (ServiceThread::*eventFunctionPtr)(const EventDescription &event);
61 -        GenericEvent* eventPtr;
62 -    };
63 -
64 -    template <class T>
65 -    void EventCall(const EventDescription &desc) {
66 -        auto fun = reinterpret_cast<void (Service::*)(const T&)>(desc.serviceFunctionPtr);
67 -        const T& eventLocale = *(static_cast<T*>(desc.eventPtr));
68 -        (desc.servicePtr->*fun)(eventLocale);
69 -    }
70 +    struct EventCallerBase {
71 +       virtual void fire() = 0;
72 +       virtual ~EventCallerBase() {}
73 +    };
74  
75 +    template <class T>
76 +    struct EventCaller : public EventCallerBase {
77 +        T *event; Service *target; void (Service::*function)(const T&);
78 +        EventCaller(const T &e, Service *c, void (Service::*f)(const T&)) : event(new T(e)), target(c), function(f) {}
79 +       ~EventCaller() { delete event; }
80 +       void fire() { (target->*function)(*event); }
81 +    };
82 +
83      static void ThreadLoopStatic(ServiceThread *ptr) {
84          ptr->ThreadLoop();
85      }
86  
87      void ThreadLoop(){
88          for (;;) {
89 -            EventDescription description = {NULL, NULL, NULL, NULL};
90 +            EventCallerBase *ec = NULL;
91              {
92                  std::unique_lock<std::mutex> ulock(m_eventQueueMutex);
93                  if (m_quit)
94                      return;
95                  if (!m_eventQueue.empty()) {
96 -                    description = m_eventQueue.front();
97 +                    ec = m_eventQueue.front();
98                      m_eventQueue.pop();
99                  } else {
100                      m_waitCondition.wait(ulock);
101                  }
102              }
103  
104 -            if (description.eventPtr != NULL) {
105 +            if (ec != NULL) {
106                  UNHANDLED_EXCEPTION_HANDLER_BEGIN
107                  {
108 -                    (this->*description.eventFunctionPtr)(description);
109 -                    delete description.eventPtr;
110 +                    ec->fire();
111                  }
112                  UNHANDLED_EXCEPTION_HANDLER_END
113 +                delete ec;
114              }
115          }
116      }
117  
118      std::thread m_thread;
119      std::mutex m_eventQueueMutex;
120 -    std::queue<EventDescription> m_eventQueue;
121 +    std::queue<EventCallerBase*> m_eventQueue;
122      std::condition_variable m_waitCondition;
123  
124      State m_state;
125 -- 
126 2.17.2
127