You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have noticed a potential issue in the driver code for the AD7606. In the readAndReturn() method (for example, in the AD7606_8080 class), a pointer is returned to a locally declared array which resides on the stack. Here is an excerpt from the code:
int16_t*AD7606_8080::readAndReturn()
{
int16_trawDataBuffer[8]; // This data is only valid inside the function, it is on the STACK// ... code to populate rawDataBuffer ...returnrawDataBuffer;
}
Because rawDataBuffer is allocated on the stack, its lifetime is limited to the duration of the function call. Returning a pointer to this buffer leads to undefined behavior since the data becomes invalid once the function returns.
One workaround could be to declare rawDataBuffer as static, which would extend its lifetime beyond the function scope. However, please note that while this might work in a single-threaded context, it can introduce issues with reentrancy and thread safety.
I have noticed this pattern repeated in other parts of the codebase as well. It might be worthwhile to review these instances to ensure that all returned pointers reference valid memory. A more robust solution might be to have the caller supply a buffer or to use dynamic memory allocation (with proper memory management).
Thank you for your attention to this matter.
The text was updated successfully, but these errors were encountered:
I have noticed a potential issue in the driver code for the AD7606. In the
readAndReturn()
method (for example, in theAD7606_8080
class), a pointer is returned to a locally declared array which resides on the stack. Here is an excerpt from the code:Because
rawDataBuffer
is allocated on the stack, its lifetime is limited to the duration of the function call. Returning a pointer to this buffer leads to undefined behavior since the data becomes invalid once the function returns.One workaround could be to declare
rawDataBuffer
asstatic
, which would extend its lifetime beyond the function scope. However, please note that while this might work in a single-threaded context, it can introduce issues with reentrancy and thread safety.I have noticed this pattern repeated in other parts of the codebase as well. It might be worthwhile to review these instances to ensure that all returned pointers reference valid memory. A more robust solution might be to have the caller supply a buffer or to use dynamic memory allocation (with proper memory management).
Thank you for your attention to this matter.
The text was updated successfully, but these errors were encountered: