11-12-2020 07:53 AM
Hi,
I'm getting a fatal run-time error when I debug a program in CVI 2020. I get this message: Derefence of a 2 byte object where only 1 bytes exist. I attached the project, in a .rar file, to facilitate the process. I get the error exactly in the dust_hdlc.c file in the 224 line, inside the dust_hdlc_pkdecode function, here:
*bytes_written = 0;
Thanks you for helping,
Mikel
11-13-2020 05:54 AM - edited 11-13-2020 06:04 AM
The debugger is apparently correct. In dust_dn2500.c you call the according function as such:
rtn = dust_hdlc_pkdecode(ia->bf_src, ia->len_src,
ia->bf_dest, DUST_DN2500_MAX_DEST_BUF_SIZE, (uint16_t*)&(ia->len_dest));
C allows casting of any pointer type into any other one without much fuss, but that does not mean that the code created for that is correct or would not have potential side effects.
In the declaration for your struct dust_dn2500_t you have this however:
struct dust_dn2500_t {
.....
uint8_t bf_src[DUST_DN2500_MAX_ENCODED_PACKET_SIZE];
uint8_t len_src;
/* destination buffer */
uint8_t bf_dest[DUST_DN2500_MAX_DEST_BUF_SIZE];
uint8_t len_dest;
uint8_t idx;
...
};
As you can see you only allocate an uint8_t here. C does not require a variable size extension when you cast a pointer type to a bigger pointer type since C was designed under the assumption to be close to the bare metal, not to be a high level programming language.
The code where the debugger complains, if done as minimalistic as C allows, would simply overwrite the idx variable too.
While typecasts of scalars in C are usually fairly harmless you really really have to double and triple check whenever you have to typecast pointer types, to make sure you are not assigning apples to potatos.
This is one of the reason why they actually broke compatibility in C++ about typecasts. It was otherwise simply way to easy to mix up object instances and let them point to something entirely different than what the code claimed to be.
11-13-2020 07:27 AM
Hi Rolf,
Thanks for answering. I've already found the problem. In the definition of the dust_hdlc_pkdecode i defined:
int8_t dust_hdlc_pkdecode(uint8_t* buf_src, uint16_t size_src,
uint8_t* buf_dest, uint16_t size_of_dest,
uint8_t* bytes_written);
And the I used as a uint16_t. That was the problem.
Thanks,
Mikel