msgpack-c icon indicating copy to clipboard operation
msgpack-c copied to clipboard

bad_cast exception when msgpack::unpack

Open CoderMayou opened this issue 6 years ago • 3 comments

Hi All,

I am getting bad_cast while msgpack::unpack message. There are two process involved ,one is packing and another process unpacking. Tried to unpack immediately after pack in the same process and unpacking is successful. But when tried to unpack in another process failing with bad_cast.

Below are the deatils:

int32_t xyz::sendMsg() { std::stringstream ss; msgpack::pack(ss,*this);

GroupRpyMsg  rpyMsg;
bool rtn = unpackMsg(rpyMsg, ss.str().data() , ss.str().size());
if(rtn)
{   
    std::cout << "send Success: " << rpyMsg.string().c_str() << std::endl;
}   
else
{   
    std::cout << "send Failed: " << rpyMsg.string().c_str() << std::endl;
}   
return send(ss.str().data(), ss.str().size());

}

template<typename T> bool unpackMsg(T& tobj, const char* buf, size_t size) { msgpack::unpacked unp; msgpack::unpack(unp, buf, size); msgpack::object obj = unp.get(); try {

    tobj = obj.as<T>();
	
	/*Printing*/
	
    std::cout << "T: ";
    std::cout << typeid(T).name() << std::endl;

    std::cout << "buf: ";
    for (std::size_t i = 0;
            i != size;
            ++i) {
        std::cout << std::hex << std::setw(2) << std::setfill('0');
        std::cout << (static_cast<int>((buf + size)[i]) & 0xff) << ' ';
    }
    std::cout << std::endl;

	std::cout << "obj: ";
    std::cout << obj << std::endl;
    std::cout << "Success: " << std::endl;
	
    return true;
}
catch (const msgpack::type_error& err) {
	
	/*Printing*/
    std::cout << "T: ";
    std::cout << typeid(T).name() << std::endl;

    std::cout << "buf: ";
    for (std::size_t i = 0;
            i != size;
            ++i) {
        std::cout << std::hex << std::setw(2) << std::setfill('0');
        std::cout << (static_cast<int>((buf + size)[i]) & 0xff) << ' ';
    }
    std::cout << std::endl;

    std::cout << "obj: ";
    std::cout << obj  << std::endl;
    std::cout <<  error: " << err.what() << std::endl;
	
    return false;
}

}

Trying to print values when success and in failure Time.

FAIL:

T: 14GroupRpyMsg
buf: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b1 01 00 00 00 00 00 00 78 00 00 d4 28 7f 00 00 78 00 00 d4 28 7f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 01 00 00 00 00 00 00 78 00 00 d4 28 7f 00 00 78 00 00 d4 28 7f 00 00 00 00 00 00 00 00 00 00 81 01 00 00 00 00 00 00 60 73 30 d4 28 7f 00 00 78 00 obj: {"msgId_":1, "src_":{"taskId":ad, "appId":0}, "dest_":{"taskId":ba, "appId":9}, "groupName_":"", "action_":0, "result_":0, "size_":0, "group_":{"name_":"", "nodes_":[]}, "nodeName_":"10.1.1.2", "nodeAddr_":"10.1.1.2", "clusterState_":1} error: std::bad_cast

SUCCESS:

Below is another thread unpacking same response and unpack success: LES :

T: 14GroupRpyMsg
buf: 00 09 10 a6 7f 00 00 21 00 00 00 00 00 00 00 60 20 0a 10 a6 7f 00 00 40 bf 7f 10 a6 7f 00 00 20 00 00 00 00 00 00 00 64 00 00 00 00 00 00 00 48 00 00 00 00 00 00 00 af be ed fe 00 00 00 00 08 76 41 2e a6 7f 00 00 e0 75 41 2e a6 7f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 aa 42 2e a6 7f 00 00 01 00 00 00 09 00 00 00 45 00 00 00 00 00 00 00 26 00 00 00 00 00 00 00 af be ed fe 00 00 00 00 2f 76 61 72 2f 6c 6f 67 2f 73 6f 6e 75 73 2f 74 6d 70 obj: {"msgId_":1, "src_":{"taskId":ad, "appId":0}, "dest_":{"taskId":ae, "appId":1}, "groupName_":"", "action_":0, "result_":0, "size_":0, "group_":{"name_":"", "nodes_":[]}, "nodeName_":"10.1.1.2", "nodeAddr_":"10.1.1.2", "clusterState_":1}

Success:

In Above both the SUCESS and FAILURE obj: printing is same,not understanding,why bad cast error is coming? Note: taskId":is different in both logs

Please let me know if you need any input?

Thanks Mayou

CoderMayou avatar Mar 15 '19 09:03 CoderMayou

Could you update your code using https://guides.github.com/features/mastering-markdown/ ? It's very difficult to read.

redboltz avatar Mar 15 '19 10:03 redboltz

Hi All,

I am getting bad_cast while msgpack::unpack message. There are two process involved ,one is packing and another process unpacking. Tried to unpack immediately after pack in the same process and unpacking is successful. But when tried to unpack in another process failing with bad_cast.

Below are the deatils:

     
int32_t xyz::sendMsg()
{
std::stringstream ss;
msgpack::pack(ss,*this);
 **/unpacking in same function*/**
GroupRpyMsg  rpyMsg;
bool rtn = unpackMsg(rpyMsg, ss.str().data() , ss.str().size());
if(rtn)
{   
    std::cout << "send Success: " << rpyMsg.string().c_str() << std::endl;
}   
else
{   
    std::cout << "send Failed: " << rpyMsg.string().c_str() << std::endl;
}   
return send(ss.str().data(), ss.str().size());  **/*Sending to another thread*/**
}
template
bool unpackMsg(T& tobj, const char* buf, size_t size)
{
   msgpack::unpacked unp;
   msgpack::unpack(unp, buf, size);
   msgpack::object obj = unp.get();
   try
  {

    tobj = obj.as<T>();
	
	**/*Printing*/**
	
    std::cout << "T: ";
    std::cout << typeid(T).name() << std::endl;

    std::cout << "buf: ";
    for (std::size_t i = 0;
            i != size;
            ++i) {
        std::cout << std::hex << std::setw(2) << std::setfill('0');
        std::cout << (static_cast<int>((buf + size)[i]) & 0xff) << ' ';
    }
    std::cout << std::endl;

	std::cout << "obj: ";
    std::cout << obj << std::endl;
    std::cout << "Success: " << std::endl;
	
    return true;
  }
  catch (const msgpack::type_error& err) {
	
	/*Printing*/
    std::cout << "T: ";
    std::cout << typeid(T).name() << std::endl;

    std::cout << "buf: ";
    for (std::size_t i = 0;
            i != size;
            ++i) {
        std::cout << std::hex << std::setw(2) << std::setfill('0');
        std::cout << (static_cast<int>((buf + size)[i]) & 0xff) << ' ';
    }
    std::cout << std::endl;

    std::cout << "obj: ";
    std::cout << obj  << std::endl;
    std::cout <<  error: " << err.what() << std::endl;
	
    return false;
  }
}

Trying to print values when success and in failure Time.

FAIL CASE

T: 14GroupRpyMsg buf: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b1 01 00 00 00 00 00 00 78 00 00 d4 28 7f 00 00 78 00 00 d4 28 7f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 01 00 00 00 00 00 00 78 00 00 d4 28 7f 00 00 78 00 00 d4 28 7f 00 00 00 00 00 00 00 00 00 00 81 01 00 00 00 00 00 00 60 73 30 d4 28 7f 00 00 78 00 obj: {"msgId_":1, "src_":{"taskId":ad, "appId":0}, "dest_":{"taskId":ba, "appId":9}, "groupName_":"", "action_":0, "result_":0, "size_":0, "group_":{"name_":"", "nodes_":[]}, "nodeName_":"10.1.1.2", "nodeAddr_":"10.1.1.2", "clusterState_":1} error: std::bad_cast

SUCCESS CASE

Below is another thread unpacking same response and unpack success:

T: 14GroupRpyMsg buf: 00 09 10 a6 7f 00 00 21 00 00 00 00 00 00 00 60 20 0a 10 a6 7f 00 00 40 bf 7f 10 a6 7f 00 00 20 00 00 00 00 00 00 00 64 00 00 00 00 00 00 00 48 00 00 00 00 00 00 00 af be ed fe 00 00 00 00 08 76 41 2e a6 7f 00 00 e0 75 41 2e a6 7f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 aa 42 2e a6 7f 00 00 01 00 00 00 09 00 00 00 45 00 00 00 00 00 00 00 26 00 00 00 00 00 00 00 af be ed fe 00 00 00 00 2f 76 61 72 2f 6c 6f 67 2f 73 6f 6e 75 73 2f 74 6d 70 obj: {"msgId_":1, "src_":{"taskId":ad, "appId":0}, "dest_":{"taskId":ae, "appId":1}, "groupName_":"", "action_":0, "result_":0, "size_":0, "group_":{"name_":"", "nodes_":[]}, "nodeName_":"10.1.1.2", "nodeAddr_":"10.1.1.2", "clusterState_":1}

Success:

In Above both the **SUCESS and FAILURE obj: printing is same ** ,not understanding,why bad cast error is coming?

Note: taskId":is different in both logs

Please let me know if you need any input?

Thanks Mayou

CoderMayou avatar Mar 15 '19 11:03 CoderMayou

MessagePack format is defined as follows: https://github.com/msgpack/msgpack/blob/master/spec.md

It seems that your buf: is not correct. obj: indicates that the data is MAP format family. https://github.com/msgpack/msgpack/blob/master/spec.md#map-format-family

However, buf: starts with 00.

Here is a simple program to check from hexfile to msgpack::object and convert your type.

https://wandbox.org/permlink/3ePESIFSXXCMJjwe

  1. Edit msgpack.dat, 2nd tab.
  2. Edit type, line 24.
  3. Run

redboltz avatar Mar 15 '19 12:03 redboltz