Skip to content

net: mqtt: Fix error of size_t formatting with PRIu16 #81803

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

Conversation

xingrz
Copy link
Member

@xingrz xingrz commented Nov 23, 2024

Similar to #81626, size_t is defined as long unsigned int on 64-bit platforms, which makes it incompatible with PRIu16. This results in PRs targeting 64-bit platforms failing.

This issue can be reproduced on 64-bit boards such as rpi_4b using the following command:

$ west build -s tests/net/lib/mqtt_sn_packet -p -b rpi_4b

[...]

[112/150] Building C object zephyr/subsys/net/lib/mqtt_sn/CMakeFiles/subsys__net__lib__mqtt_sn.dir/mqtt_sn.c.obj
In file included from [...]/zephyr/include/zephyr/logging/log.h:11,
                 from [...]/zephyr/subsys/net/lib/mqtt_sn/mqtt_sn.c:14:
[...]/zephyr/subsys/net/lib/mqtt_sn/mqtt_sn.c: In function 'mqtt_sn_publish_create':
[...]/zephyr/subsys/net/lib/mqtt_sn/mqtt_sn.c:187:33: warning: format '%hu' expects argument of type 'int', but argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]
  187 |                         LOG_ERR("Can't create PUB: Too much data (%" PRIu16 ")", data->size);
      |                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~             ~~~~~~~~~~
      |                                                                                      |
      |                                                                                      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:44:25: note: in expansion of macro 'Z_LOG'
   44 | #define LOG_ERR(...)    Z_LOG(LOG_LEVEL_ERR, __VA_ARGS__)
      |                         ^~~~~
[...]/zephyr/subsys/net/lib/mqtt_sn/mqtt_sn.c:187:25: note: in expansion of macro 'LOG_ERR'
  187 |                         LOG_ERR("Can't create PUB: Too much data (%" PRIu16 ")", data->size);
      |                         ^~~~~~~
[...]/zephyr/subsys/net/lib/mqtt_sn/mqtt_sn.c: In function 'mqtt_sn_topic_create':
[...]/zephyr/subsys/net/lib/mqtt_sn/mqtt_sn.c:245:25: warning: format '%hu' expects argument of type 'int', but argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]
  245 |                 LOG_ERR("Can't create topic: name too long (%" PRIu16 ")", name->size);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~             ~~~~~~~~~~
      |                                                                                |
      |                                                                                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:44:25: note: in expansion of macro 'Z_LOG'
   44 | #define LOG_ERR(...)    Z_LOG(LOG_LEVEL_ERR, __VA_ARGS__)
      |                         ^~~~~
[...]/zephyr/subsys/net/lib/mqtt_sn/mqtt_sn.c:245:17: note: in expansion of macro 'LOG_ERR'
  245 |                 LOG_ERR("Can't create topic: name too long (%" PRIu16 ")", name->size);
      |                 ^~~~~~~

This addresses the following warning building with `CONFIG_64BIT=y`:

    error: format '%hu' expects argument of type 'int', but argument X has
    type 'size_t' {aka 'long unsigned int'}

Signed-off-by: Chen Xingyu <hi@xingrz.me>
@jukkar
Copy link
Member

jukkar commented Nov 26, 2024

Fixes #82003

@pdgendt pdgendt added the Trivial Changes that can be reviewed by anyone, i.e. doc changes, minor build system tweaks, etc. label Nov 26, 2024
@kartben kartben linked an issue Nov 26, 2024 that may be closed by this pull request
@fabiobaltieri fabiobaltieri merged commit de1643d into zephyrproject-rtos:main Nov 26, 2024
30 checks passed
@xingrz xingrz deleted the fix-sizet-64bit-printf/pr branch November 26, 2024 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Networking 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.

CI failure for net.mqtt_sn.packet on some platforms
9 participants