Skip to content

Bluetooth: Host: Fix format specifier warnings #87262

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

Merged
merged 1 commit into from
Mar 18, 2025

Conversation

jhedberg
Copy link
Member

@jhedberg jhedberg commented Mar 18, 2025

Use %zu for size_t and %zd for ssize_t, and don't use those specifiers for any other type. Also make sure that field width specifiers get typecast to the expected int type.

@zephyrbot zephyrbot added area: Bluetooth area: Bluetooth Host Bluetooth Host (excluding BR/EDR) size: XS A PR changing only a single line of code labels Mar 18, 2025
@jhedberg jhedberg added the Trivial Changes that can be reviewed by anyone, i.e. doc changes, minor build system tweaks, etc. label Mar 18, 2025
@jhedberg
Copy link
Member Author

This was reported here: #82316 (comment)

@jhedberg jhedberg requested a review from nhutnguyenkc March 18, 2025 08:57
hermabe
hermabe previously approved these changes Mar 18, 2025
@nhutnguyenkc
Copy link
Collaborator

Thanks for addressing the issue. But it seems CI does not show the full log; the issue occurs in many places! Let me share the full warnings when I build locally. I am not sure that fixing only hci_core.c can resolve the issue. Please check this PR #86606 if you want to reproduce the issue

Build command: west build -p auto -b rza3ul_smarc/r9a07g063u02gbg tests/bluetooth/classic/sdp_c -T sdp.c

[216/324] Building C object zephyr/subsys/bluetooth/host/CMakeFiles/subsys__bluetooth__host.dir/hci_core.c.obj
In file included from /zephyr/include/zephyr/logging/log.h:11,
                 from /zephyr/include/zephyr/debug/stack.h:15,
                 from /zephyr/subsys/bluetooth/host/hci_core.c:25:
/zephyr/subsys/bluetooth/host/hci_core.c: In function 'hci_num_completed_packets':
/zephyr/subsys/bluetooth/host/hci_core.c:570:25: warning: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int' [-Wformat=]
  570 |                 LOG_ERR("evt num_handles (=%u) too large (%u > %u)",
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  571 |                         evt->num_handles,
  572 |                         sizeof(*evt) + sizeof(evt->h[0]) * evt->num_handles,
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                      |
      |                                      long unsigned int
/zephyr/include/zephyr/logging/log_core.h:315:50: note: in definition of macro 'Z_LOG2'
  315 |                         z_log_printf_arg_checker(__VA_ARGS__);                                     \
      |                                                  ^~~~~~~~~~~
/zephyr/include/zephyr/logging/log.h:44:25: note: in expansion of macro 'Z_LOG'
   44 | #define LOG_ERR(...)    Z_LOG(LOG_LEVEL_ERR, __VA_ARGS__)
      |                         ^~~~~
/zephyr/subsys/bluetooth/host/hci_core.c:570:17: note: in expansion of macro 'LOG_ERR'
  570 |                 LOG_ERR("evt num_handles (=%u) too large (%u > %u)",
      |                 ^~~~~~~
/zephyr/subsys/bluetooth/host/hci_core.c:570:60: note: format string is defined here
  570 |                 LOG_ERR("evt num_handles (=%u) too large (%u > %u)",
      |                                                           ~^
      |                                                            |
      |                                                            unsigned int
      |                                                           %lu
[221/324] Building C object zephyr/subsys/bluetooth/host/classic/CMakeFiles/subsys__bluetooth__host__classic.dir/rfcomm.c.obj
In file included from /zephyr/include/zephyr/logging/log.h:11,
                 from /zephyr/include/zephyr/debug/stack.h:15,
                 from /zephyr/subsys/bluetooth/host/classic/rfcomm.c:15:
/zephyr/subsys/bluetooth/host/classic/rfcomm.c: In function 'rfcomm_recv':
/zephyr/subsys/bluetooth/host/classic/rfcomm.c:1548:25: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
 1548 |                 LOG_ERR("Too small RFCOMM information (%d < %d)", buf->len,
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1549 |                         hdr_len + msg_len + sizeof(fcs));
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                           |
      |                                           long unsigned int
/zephyr/include/zephyr/logging/log_core.h:315:50: note: in definition of macro 'Z_LOG2'
  315 |                         z_log_printf_arg_checker(__VA_ARGS__);                                     \
      |                                                  ^~~~~~~~~~~
/zephyr/include/zephyr/logging/log.h:44:25: note: in expansion of macro 'Z_LOG'
   44 | #define LOG_ERR(...)    Z_LOG(LOG_LEVEL_ERR, __VA_ARGS__)
      |                         ^~~~~
/zephyr/subsys/bluetooth/host/classic/rfcomm.c:1548:17: note: in expansion of macro 'LOG_ERR'
 1548 |                 LOG_ERR("Too small RFCOMM information (%d < %d)", buf->len,
      |                 ^~~~~~~
/zephyr/subsys/bluetooth/host/classic/rfcomm.c:1548:62: note: format string is defined here
 1548 |                 LOG_ERR("Too small RFCOMM information (%d < %d)", buf->len,
      |                                                             ~^
      |                                                              |
      |                                                              int
      |                                                             %ld
[228/324] Building C object zephyr/subsys/bluetooth/host/CMakeFiles/subsys__bluetooth__host.dir/l2cap.c.obj
In file included from /zephyr/include/zephyr/logging/log.h:11,
                 from /zephyr/subsys/bluetooth/host/l2cap.c:35:
/zephyr/subsys/bluetooth/host/l2cap.c: In function 'l2cap_chan_le_recv_sdu':
/zephyr/subsys/bluetooth/host/l2cap.c:2453:17: warning: format '%zu' expects argument of type 'size_t', but argument 3 has type 'int' [-Wformat=]
 2453 |         LOG_DBG("chan %p len %zu", chan, buf->len);
      |                 ^~~~~~~~~~~~~~~~~        ~~~~~~~~
      |                                             |
      |                                             int
/zephyr/include/zephyr/logging/log_core.h:315:50: note: in definition of macro 'Z_LOG2'
  315 |                         z_log_printf_arg_checker(__VA_ARGS__);                                     \
      |                                                  ^~~~~~~~~~~
/zephyr/include/zephyr/logging/log.h:75:25: note: in expansion of macro 'Z_LOG'
   75 | #define LOG_DBG(...)    Z_LOG(LOG_LEVEL_DBG, __VA_ARGS__)
      |                         ^~~~~
/zephyr/subsys/bluetooth/host/l2cap.c:2453:9: note: in expansion of macro 'LOG_DBG'
 2453 |         LOG_DBG("chan %p len %zu", chan, buf->len);
      |         ^~~~~~~
/zephyr/subsys/bluetooth/host/l2cap.c:2453:32: note: format string is defined here
 2453 |         LOG_DBG("chan %p len %zu", chan, buf->len);
      |                              ~~^
      |                                |
      |                                long unsigned int
      |                              %u
/zephyr/subsys/bluetooth/host/l2cap.c: In function 'l2cap_chan_le_recv_seg':
/zephyr/subsys/bluetooth/host/l2cap.c:2495:17: warning: format '%zu' expects argument of type 'size_t', but argument 4 has type 'int' [-Wformat=]
 2495 |         LOG_DBG("chan %p seg %d len %zu", chan, seg, buf->len);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~             ~~~~~~~~
      |                                                         |
      |                                                         int
/zephyr/include/zephyr/logging/log_core.h:315:50: note: in definition of macro 'Z_LOG2'
  315 |                         z_log_printf_arg_checker(__VA_ARGS__);                                     \
      |                                                  ^~~~~~~~~~~
/zephyr/include/zephyr/logging/log.h:75:25: note: in expansion of macro 'Z_LOG'
   75 | #define LOG_DBG(...)    Z_LOG(LOG_LEVEL_DBG, __VA_ARGS__)
      |                         ^~~~~
/zephyr/subsys/bluetooth/host/l2cap.c:2495:9: note: in expansion of macro 'LOG_DBG'
 2495 |         LOG_DBG("chan %p seg %d len %zu", chan, seg, buf->len);
      |         ^~~~~~~
/zephyr/subsys/bluetooth/host/l2cap.c:2495:39: note: format string is defined here
 2495 |         LOG_DBG("chan %p seg %d len %zu", chan, seg, buf->len);
      |                                     ~~^
      |                                       |
      |                                       long unsigned int
      |                                     %u
/zephyr/subsys/bluetooth/host/l2cap.c: In function 'bt_l2cap_chan_send':
/zephyr/subsys/bluetooth/host/l2cap.c:3291:17: warning: format '%zu' expects argument of type 'size_t', but argument 4 has type 'int' [-Wformat=]
 3291 |         LOG_DBG("chan %p buf %p len %zu", chan, buf, buf->len);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~             ~~~~~~~~
      |                                                         |
      |                                                         int
/zephyr/include/zephyr/logging/log_core.h:315:50: note: in definition of macro 'Z_LOG2'
  315 |                         z_log_printf_arg_checker(__VA_ARGS__);                                     \
      |                                                  ^~~~~~~~~~~
/zephyr/include/zephyr/logging/log.h:75:25: note: in expansion of macro 'Z_LOG'
   75 | #define LOG_DBG(...)    Z_LOG(LOG_LEVEL_DBG, __VA_ARGS__)
      |                         ^~~~~
/zephyr/subsys/bluetooth/host/l2cap.c:3291:9: note: in expansion of macro 'LOG_DBG'
 3291 |         LOG_DBG("chan %p buf %p len %zu", chan, buf, buf->len);
      |         ^~~~~~~
/zephyr/subsys/bluetooth/host/l2cap.c:3291:39: note: format string is defined here
 3291 |         LOG_DBG("chan %p buf %p len %zu", chan, buf, buf->len);
      |                                     ~~^
      |                                       |
      |                                       long unsigned int
      |                                     %u
[235/324] Building C object zephyr/subsys/bluetooth/host/classic/CMakeFiles/subsys__bluetooth__host__classic.dir/l2cap_br.c.obj
In file included from /zephyr/include/zephyr/logging/log.h:11,
                 from /zephyr/subsys/bluetooth/host/classic/l2cap_br.c:33:
/zephyr/subsys/bluetooth/host/classic/l2cap_br.c: In function 'bt_l2cap_br_send_cb':
/zephyr/subsys/bluetooth/host/classic/l2cap_br.c:322:17: warning: format '%zu' expects argument of type 'size_t', but argument 4 has type 'int' [-Wformat=]
  322 |         LOG_DBG("chan %p buf %p len %zu", br_chan, buf, buf->len);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~                ~~~~~~~~
      |                                                            |
      |                                                            int
/zephyr/include/zephyr/logging/log_core.h:315:50: note: in definition of macro 'Z_LOG2'
  315 |                         z_log_printf_arg_checker(__VA_ARGS__);                                     \
      |                                                  ^~~~~~~~~~~
/zephyr/include/zephyr/logging/log.h:75:25: note: in expansion of macro 'Z_LOG'
   75 | #define LOG_DBG(...)    Z_LOG(LOG_LEVEL_DBG, __VA_ARGS__)
      |                         ^~~~~
/zephyr/subsys/bluetooth/host/classic/l2cap_br.c:322:9: note: in expansion of macro 'LOG_DBG'
  322 |         LOG_DBG("chan %p buf %p len %zu", br_chan, buf, buf->len);
      |         ^~~~~~~
/zephyr/subsys/bluetooth/host/classic/l2cap_br.c:322:39: note: format string is defined here
  322 |         LOG_DBG("chan %p buf %p len %zu", br_chan, buf, buf->len);
      |                                     ~~^
      |                                       |
      |                                       long unsigned int
      |                                     %u
[237/324] Building C object zephyr/subsys/bluetooth/host/shell/CMakeFiles/subsys__bluetooth__host__shell.dir/bt.c.obj
/zephyr/subsys/bluetooth/host/shell/bt.c: In function 'data_verbose_cb':
/zephyr/subsys/bluetooth/host/shell/bt.c:364:33: warning: field width specifier '*' expects argument of type 'int', but argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]     
  364 |         bt_shell_fprintf_info("%*sType 0x%02x: ", strlen(scan_response_label), "", data->type);
      |                                ~^~                ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                 |                 |
      |                                 int               size_t {aka long unsigned int}
In file included from /zephyr/subsys/bluetooth/host/shell/bt.c:41:
/zephyr/subsys/bluetooth/host/shell/bt.c: In function 'scan_recv':
/zephyr/subsys/bluetooth/host/shell/bt.c:574:31: warning: field width specifier '*' expects argument of type 'int', but argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]     
  574 |                 bt_shell_info("%*s[SCAN DATA START - %s]",
      |                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  575 |                               strlen(scan_response_label), "",
      |                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                               |
      |                               size_t {aka long unsigned int}
/zephyr/subsys/bluetooth/common/bt_shell_private.h:58:31: note: in definition of macro 'bt_shell_info'
   58 |         bt_shell_fprintf_info(_ft "\n", ##__VA_ARGS__)
      |                               ^~~
/zephyr/subsys/bluetooth/host/shell/bt.c:574:33: note: format string is defined here
  574 |                 bt_shell_info("%*s[SCAN DATA START - %s]",
      |                                ~^~
      |                                 |
      |                                 int
/zephyr/subsys/bluetooth/host/shell/bt.c:578:31: warning: field width specifier '*' expects argument of type 'int', but argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]     
  578 |                 bt_shell_info("%*s[SCAN DATA END]", strlen(scan_response_label), "");
      |                               ^~~~~~~~~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                     |
      |                                                     size_t {aka long unsigned int}
/zephyr/subsys/bluetooth/common/bt_shell_private.h:58:31: note: in definition of macro 'bt_shell_info'
   58 |         bt_shell_fprintf_info(_ft "\n", ##__VA_ARGS__)
      |                               ^~~
/zephyr/subsys/bluetooth/host/shell/bt.c:578:33: note: format string is defined here
  578 |                 bt_shell_info("%*s[SCAN DATA END]", strlen(scan_response_label), "");
      |                                ~^~
      |                                 |
      |                                 int
/zephyr/subsys/bluetooth/host/shell/bt.c: In function 'ad_init':
/zephyr/subsys/bluetooth/host/shell/bt.c:1938:40: warning: format '%d' expects argument of type 'int', but argument 2 has type 'ssize_t' {aka 'long int'} [-Wformat=]
 1938 |                         bt_shell_error("Failed to add CSIS data (err %d)", csis_ad_len);
      |                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  ~~~~~~~~~~~
      |                                                                            |
      |                                                                            ssize_t {aka long int}
/zephyr/subsys/bluetooth/common/bt_shell_private.h:88:32: note: in definition of macro 'bt_shell_error'
   88 |         bt_shell_fprintf_error(_ft "\n", ##__VA_ARGS__)
      |                                ^~~
/zephyr/subsys/bluetooth/host/shell/bt.c:1938:71: note: format string is defined here
 1938 |                         bt_shell_error("Failed to add CSIS data (err %d)", csis_ad_len);
      |                                                                      ~^
      |                                                                       |
      |                                                                       int
      |                                                                      %ld
[244/324] Building C object zephyr/subsys/bluetooth/host/classic/CMakeFiles/subsys__bluetooth__host__classic.dir/sdp.c.obj
In file included from /zephyr/include/zephyr/logging/log.h:11,
                 from /zephyr/subsys/bluetooth/host/classic/sdp.c:28:
/zephyr/subsys/bluetooth/host/classic/sdp.c: In function 'get_att_search_list':
/zephyr/subsys/bluetooth/host/classic/sdp.c:1011:33: warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]      
 1011 |                         LOG_WRN("Exceeded maximum array length %u of %p", max_filters, filter);
      |                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  ~~~~~~~~~~~
      |                                                                           |
      |                                                                           size_t {aka long unsigned int}
/zephyr/include/zephyr/logging/log_core.h:315:50: note: in definition of macro 'Z_LOG2'
  315 |                         z_log_printf_arg_checker(__VA_ARGS__);                                     \
      |                                                  ^~~~~~~~~~~
/zephyr/include/zephyr/logging/log.h:55:24: note: in expansion of macro 'Z_LOG'
   55 | #define LOG_WRN(...)   Z_LOG(LOG_LEVEL_WRN, __VA_ARGS__)
      |                        ^~~~~
/zephyr/subsys/bluetooth/host/classic/sdp.c:1011:25: note: in expansion of macro 'LOG_WRN'
 1011 |                         LOG_WRN("Exceeded maximum array length %u of %p", max_filters, filter);
      |                         ^~~~~~~
/zephyr/subsys/bluetooth/host/classic/sdp.c:1011:65: note: format string is defined here
 1011 |                         LOG_WRN("Exceeded maximum array length %u of %p", max_filters, filter);
      |                                                                ~^
      |                                                                 |
      |                                                                 unsigned int
      |                                                                %lu

@jhedberg
Copy link
Member Author

jhedberg commented Mar 18, 2025

@nhutnguyenkc I've pushed an update to try to fix all of those. Can you confirm if all the warnings/errors are now gone for you?

@jhedberg jhedberg changed the title Bluetooth: Host: Fix format specifier for size_t Bluetooth: Host: Fix format specifier warnings Mar 18, 2025
Thalley
Thalley previously approved these changes Mar 18, 2025
Comment on lines +1548 to 1549
LOG_ERR("Too small RFCOMM information (%u < %zu)", buf->len,
hdr_len + msg_len + sizeof(fcs));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does adding a non-size_t with a size_t always result in a size_t?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the compiler will round up to the biggest type in the calculation, which in this case is size_t

@nhutnguyenkc
Copy link
Collaborator

I've pushed an update to try to fix all of those. Can you confirm if all the warnings/errors are now gone for you?

It has improved a lot. Only two warnings remain.

/zephyr/subsys/bluetooth/host/shell/bt.c: In function 'data_verbose_cb':
/zephyr/subsys/bluetooth/host/shell/bt.c:364:33: warning: field width specifier '*' expects argument of type 'int', but argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat 
]
  364 |         bt_shell_fprintf_info("%*sType 0x%02x: ", strlen(scan_response_label), "", data->type);
      |                                ~^~                ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                 |                 |
      |                                 int               size_t {aka long unsigned int}
In file included from /zephyr/subsys/bluetooth/host/shell/bt.c:41:
/zephyr/subsys/bluetooth/host/shell/bt.c: In function 'scan_recv':
/zephyr/subsys/bluetooth/host/shell/bt.c:578:31: warning: field width specifier '*' expects argument of type 'int', but argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat 
]
  578 |                 bt_shell_info("%*s[SCAN DATA END]", strlen(scan_response_label), "");
      |                               ^~~~~~~~~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                     |
      |                                                     size_t {aka long unsigned int}
/zephyr/subsys/bluetooth/common/bt_shell_private.h:58:31: note: in definition of macro 'bt_shell_info'
   58 |         bt_shell_fprintf_info(_ft "\n", ##__VA_ARGS__)
      |                               ^~~
/zephyr/subsys/bluetooth/host/shell/bt.c:578:33: note: format string is defined here
  578 |                 bt_shell_info("%*s[SCAN DATA END]", strlen(scan_response_label), "");
      |                                ~^~
      |                                 |
      |                                 int

lylezhu2012
lylezhu2012 previously approved these changes Mar 18, 2025
@jhedberg jhedberg dismissed stale reviews from lylezhu2012 and Thalley via 0030707 March 18, 2025 10:18
Use %zu for size_t and %zd for ssize_t, and don't use those specifiers for
any other type. Also make sure that field width specifiers get typecast to
the expected int type.

Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
@jhedberg
Copy link
Member Author

It has improved a lot. Only two warnings remain.

Those remaining two should be fixed now. Could you verify?

@nhutnguyenkc
Copy link
Collaborator

It has improved a lot. Only two warnings remain.

Those remaining two should be fixed now. Could you verify?

Now all of them have been gone!

@jhedberg
Copy link
Member Author

@hermabe @lylezhu2012 please refresh your reviews (that were dismissed due to fixing a couple more format string issues), thanks!

@Thalley Thalley requested a review from hermabe March 18, 2025 13:11
@kartben kartben merged commit 81e7569 into zephyrproject-rtos:main Mar 18, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Bluetooth Classic Bluetooth Classic (BR/EDR) area: Bluetooth Host Bluetooth Host (excluding BR/EDR) area: Bluetooth size: XS A PR changing only a single line of code Trivial Changes that can be reviewed by anyone, i.e. doc changes, minor build system tweaks, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants