-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bluetooth: host: Dangling pointer in le_adv_recv #48459
Comments
Why is this an issue? It would only be an issue if the It is true that the lifetime of the |
Then why are you setting it? |
It is being forwarded to the upper layers in the callback. What is happening is basically this: struct my_struct {
int a;
int *b;
};
void foo(struct my_struct *ms)
{
int local_val = 10;
ms->b = &local_val;
/* ms->b is valid */
callbacks->my_callback(ms);
/* my_callback can access and use both ms->a and ms->b */
}
void bar(void)
{
struct my_struct ms;
ms.a = 5;
foo(&ms);
/* ms->b is no longer valid */
} Which isn't really a problem, but on the other hand isn't really great either as |
Alright, if the callback, called from the Nevertheless, it is still "returning" the pointer to a local object to the caller of I guess we can fix this in one of the following ways: a) sanitize the The latter should be preferred if there are no negative side effects (e.g. significant footprint increase). |
Agreed. The struct in question is a shortlived-stack allocated variable and the increased footprint of embedding the address into the struct isn't a problem. It is, however, different than how we generally report Bluetooth addresses in callback structures. @stephanosio Did you add any additional warnings when you built? Because I've been building for native_posix at least once a week for years and never seen this warning, and this code is somewhat old. |
You are not going to see the above warning unless you are on a bleeding edge distro with the GCC 12.1 as the default host compiler.
|
I just compiled with GCC 12.1 (I am running Arch Linux) and I do not get this warning :S Edit: I actually get it for that specific test, but not when building other applications. |
Option a) also has the benefit of not changing the (stable and widely used) API. |
For b) I think this should be done more thoroughly if decided to do it. There are other structs that does the same thing as well, e.g.
IMO this is the best option. |
zephyrproject-rtos/sdk-ng#530 (reply in thread) This has become a blocker for the Zephyr SDK 0.15.0. Since it will likely take some time to properly fix, I will send a PR to partially disable this warning for the affected function for now. UPDATE: See #48690. |
This commit temporarily disables the dangling pointer warning (`-Wdangling-pointer`) in the `le_adv_recv` function in order to allow this file to build successfully with the GCC 12, which introduced this warning. A proper fix will be provided later as part of the GitHub issue zephyrproject-rtos#48459. Signed-off-by: Stephanos Ioannidis <[email protected]>
This commit temporarily disables the dangling pointer warning (`-Wdangling-pointer`) in the `le_adv_recv` function in order to allow this file to build successfully with the GCC 12, which introduced this warning. A proper fix will be provided later as part of the GitHub issue #48459. Signed-off-by: Stephanos Ioannidis <[email protected]>
Clear pointer to the le_adv_recv() stack frame before returning to the calling function. This fixes a potential compiler warning newer gcc versions. zephyr/subsys/bluetooth/host/scan.c: In function ‘le_adv_recv’: zephyr/subsys/bluetooth/host/scan.c:463:20: error: storing the address of local variable ‘id_addr’ in ‘*info.addr’ [-Werror=dangling-pointer=] 463 | info->addr = &id_addr; | ~~~~~~~~~~~^~~~~~~~~~ zephyr/subsys/bluetooth/host/scan.c:439:22: note: ‘id_addr’ declared here 439 | bt_addr_le_t id_addr; | ^~~~~~~ Fixes #48459 Signed-off-by: Johan Hedberg <[email protected]>
Clear pointer to the le_adv_recv() stack frame before returning to the calling function. This fixes a potential compiler warning newer gcc versions. zephyr/subsys/bluetooth/host/scan.c: In function ‘le_adv_recv’: zephyr/subsys/bluetooth/host/scan.c:463:20: error: storing the address of local variable ‘id_addr’ in ‘*info.addr’ [-Werror=dangling-pointer=] 463 | info->addr = &id_addr; | ~~~~~~~~~~~^~~~~~~~~~ zephyr/subsys/bluetooth/host/scan.c:439:22: note: ‘id_addr’ declared here 439 | bt_addr_le_t id_addr; | ^~~~~~~ Fixes zephyrproject-rtos#48459 Signed-off-by: Johan Hedberg <[email protected]> Signed-off-by: Herman Berget <[email protected]> (cherry picked from commit 7e951d0)
Describe the bug
The
le_adv_recv
function sets theaddr
field of thestruct bt_le_scan_recv_info *info
argument to the address of a local (stack) variable (hence, dangling pointer):zephyr/subsys/bluetooth/host/scan.c
Line 463 in c0317fb
To Reproduce
Build
tests/bluetooth/host_long_adv_recv/bluetooth.host_long_adv_recv
test onnative_posix
with GCC 12.1 as the host compiler.Expected behavior
No dangling pointers are used
Impact
Undefined behaviour
Logs and console output
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: