basecamelectronics / sbgc32-serial-api

A complete C-library and example projects for the SimpleBGC32 Serial API integration. Serial API allows to communicate with all gimbal controllers developed by Basecamelectronics company.
Apache License 2.0
24 stars 12 forks source link

stack smashing detected #5

Closed JaCoHSU closed 2 weeks ago

JaCoHSU commented 2 months ago

Hello everybody,

I am using the simplebgc code on a jetson. When I build the project with dcmake_build_type=debug everything works fine. However, when I build it with dcmake_build_type=release the code crashes with "stack smashing detected". I am not sure where the problem is. I tried to break it down and it seems that the code crashes in the core.c when generalSBGC->AvailableBytesFunc() is called.

Any ideas why this is happening in release and how to solve it?

rickpresley commented 2 months ago

I just found this same issue. The GetAvailableBytes function in the Linux driver passes a pointer to "ui16 bytes" to the ioctl function call. In this case, ioctl is expecting a pointer to an int, which is larger than 16 bits. Because "bytes" is a return variable, it is located on the stack. That means that ioctl smashes the bytes variable on the stack.

For reference, I'm working with a Linux/ARM64 configuration.

qsivey commented 2 months ago

Hello! Thank you for your feedback. We need some time to examine this problem. We also want to inform you that we are preparing version 2.0, which includes many advanced features and new detailed example. We plan to release it in the near future.

poett1 commented 2 months ago

I can confirm that this only happens when CMAKE_BUILD_TYPE=Release and not with CMAKE_BUILD_TYPE=Debug. A workaround to get CMAKE_BUILD_TYPE=Release working is disabling compiler optimization by setting:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
qsivey commented 2 months ago

Navigate here and change 'bytes' type to ui32. It should work like this.

JaCoHSU commented 1 month ago

Changing ui16 to ui32 worked for me. Thanks for your help!