Segmentation fault caused by asn1c codec function

Segmentation fault caused by asn1c codec function

Encode function

uper_encode_to_new_buffer(&asn_DEF_MessageFrame, 0,msgFrame, (void**)&chbuf);
  For the Encode function, the main reason is that the address space encoded by msgFrame will be directly given to the encoded buf, so there is no need to apply and release buf separately, otherwise a segment error will be reported. For details, please refer to my previous blog segment error (the core has been Dump) - details that are easily overlooked

Decode function

uper_decode(opt_codec_ctx, &asn_DEF_MessageFrame, (void**)&mf, decodeData, messageLength, 0, 0);
  For the Decode function, you need to pay attention to the following points:
  1. decodeData cannot be a static array. You can use a static array when you decode once in a separate test, but if you want to decode multiple times, the static array will cause problems when decoding for the second time. , the data in it can no longer be accessed. At present, it is guessed that in the decode function, the stack where decodeData is located has been operated, and the system cannot automatically recycle it, so when the static array is accessed next time, a segment error will be reported.
  2. For the MessageFrame_t* mf you applied for; after decoding, you need to recycle resources. Here you can use the API that comes with asn1c: , ASN_FREE_STRUCT(asn_DEF_MessageFrame,mf);However, it is not enough to just release mf. Through single-step debugging, even if mf is freed, However, after applying for space for mf next time, the address space where mf is located actually retains the value generated by the previous decoding. In this case, if the structure of the message body is the same every time it is decoded, it is okay to say, if the structure of the message body is inconsistent, it will also Segmentation error will be reported, and it will also cause data duplication. The safest way is to set all the values ​​in its address space to 0 after applying for space for mf, that is, memset(mf, 0, sizeof(MessageFrame_t)); of course In fact, no matter where it is, after applying for an address space, it is safest to reset the value in it to 0.

Guess you like

Origin blog.csdn.net/qq_35308053/article/details/117092986