@@ -251,30 +251,42 @@ class O2PrimaryServerDevice final : public fair::mq::Device
251251 void launchInfoThread ()
252252 {
253253 static std::vector<std::thread> threads;
254+
255+ auto sendErrorReply = [](fair::mq::Channel& channel) {
256+ LOG (error) << " UNKNOWN REQUEST" ;
257+ std::unique_ptr<fair::mq::Message> reply (channel.NewSimpleMessage ((int )(404 )));
258+ channel.Send (reply);
259+ };
260+
254261 LOG (info) << " LAUNCHING STATUS THREAD" ;
255- auto lambda = [this ]() {
262+ auto lambda = [this , sendErrorReply ]() {
256263 while (mState != O2PrimaryServerState::Stopped) {
257264 auto & channel = GetChannels ().at (" o2sim-primserv-info" ).at (0 );
258265 if (!channel.IsValid ()) {
259266 LOG (error) << " channel primserv-info not valid" ;
260267 }
261- std::unique_ptr<fair::mq::Message> request (channel.NewSimpleMessage (- 1 ));
268+ std::unique_ptr<fair::mq::Message> request (channel.NewSimpleMessage (( int )(- 1 ) ));
262269 int timeout = 100 ; // 100ms --> so as not to block and allow for proper termination of this thread
263270 if (channel.Receive (request, timeout) > 0 ) {
264- LOG (info) << " INFO REQUEST RECEIVED" ;
265- if (*(int *)(request->GetData ()) == (int )O2PrimaryServerInfoRequest::Status) {
271+ int request_payload; // we expect an (int) ~ to type O2PrimaryServerInfoRequest
272+ if (request->GetSize () != sizeof (request_payload)) {
273+ LOG (error) << " Obtained request with unexpected payload size" ;
274+ sendErrorReply (channel); // ALWAYS reply
275+ }
276+
277+ memcpy (&request_payload, request->GetData (), sizeof (request_payload));
278+
279+ if (request_payload == (int )O2PrimaryServerInfoRequest::Status) {
266280 LOG (info) << " Received status request" ;
267281 // request needs to be a simple enum of type O2PrimaryServerInfoRequest
268282 std::unique_ptr<fair::mq::Message> reply (channel.NewSimpleMessage ((int )mState .load ()));
269283 if (channel.Send (reply) > 0 ) {
270284 LOG (info) << " Send status successful" ;
271285 }
272- } else if (*( int *)request-> GetData () == (int )O2PrimaryServerInfoRequest::Config) {
286+ } else if (request_payload == (int )O2PrimaryServerInfoRequest::Config) {
273287 HandleConfigRequest (channel);
274288 } else {
275- LOG (fatal) << " UNKNOWN REQUEST" ;
276- std::unique_ptr<fair::mq::Message> reply (channel.NewSimpleMessage (404 ));
277- channel.Send (reply);
289+ sendErrorReply (channel);
278290 }
279291 }
280292 }
@@ -450,6 +462,8 @@ class O2PrimaryServerDevice final : public fair::mq::Device
450462 if (channel.Send (message) > 0 ) {
451463 LOG (info) << " config reply send " ;
452464 return true ;
465+ } else {
466+ LOG (error) << " Failure sending config reply " ;
453467 }
454468 return true ;
455469 }
0 commit comments