Skip to content

Commit

Permalink
Merge pull request #20194 from fzi-haxel/pr/size_t_print_modifier
Browse files Browse the repository at this point in the history
print: Added size_t print format specifier
  • Loading branch information
maribu authored Dec 21, 2023
2 parents 724e6e0 + 475a551 commit 418bc2f
Show file tree
Hide file tree
Showing 115 changed files with 520 additions and 446 deletions.
4 changes: 1 addition & 3 deletions CODING_CONVENTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ Some solutions to correctly handle compilation warnings.
Solution for string formatting errors:

* When printing a `size_t`
* use `%u` and cast the variable to `(unsigned)` because `newlib-nano` does
not support `%zu`
[example](https://github.com/RIOT-OS/RIOT/blob/e19f6463c09fc22c76c5b855799054cf27a697f1/tests/sizeof_tcb/main.c#L34)
* use `PRIuSIZE` from `architecture.h` because `newlib-nano` does not support `%zu`
* When printing an `unsigned char/uint8_t`
* Use `%u` because `newlib-nano` does not support `%hu/PRIu8`
[example](https://github.com/RIOT-OS/RIOT/pull/4851)
Expand Down
4 changes: 2 additions & 2 deletions drivers/at86rf215/at86rf215_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ static int _send(netdev_t *netdev, const iolist_t *iolist)

/* current packet data + FCS too long */
if ((len + iol->iol_len + IEEE802154_FCS_LEN) > AT86RF215_MAX_PKT_LENGTH) {
DEBUG("[at86rf215] error: packet too large (%u byte) to be send\n",
(unsigned)len + IEEE802154_FCS_LEN);
DEBUG("[at86rf215] error: packet too large (%" PRIuSIZE
" byte) to be send\n", len + IEEE802154_FCS_LEN);
at86rf215_tx_abort(dev);
return -EOVERFLOW;
}
Expand Down
5 changes: 3 additions & 2 deletions drivers/at86rf2xx/at86rf2xx_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <assert.h>
#include <errno.h>

#include "architecture.h"
#include "iolist.h"

#include "net/eui64.h"
Expand Down Expand Up @@ -216,8 +217,8 @@ static int _send(netdev_t *netdev, const iolist_t *iolist)
for (const iolist_t *iol = iolist; iol; iol = iol->iol_next) {
/* current packet data + FCS too long */
if ((len + iol->iol_len + 2) > AT86RF2XX_MAX_PKT_LENGTH) {
DEBUG("[at86rf2xx] error: packet too large (%u byte) to be send\n",
(unsigned)len + 2);
DEBUG("[at86rf2xx] error: packet too large (%" PRIuSIZE
" byte) to be send\n", len + 2);
return -EOVERFLOW;
}
if (iol->iol_len) {
Expand Down
7 changes: 3 additions & 4 deletions drivers/cc110x/cc110x_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,9 @@ static int cc110x_send(netdev_t *netdev, const iolist_t *iolist)
if (iol->iol_len) {
if (size + iol->iol_len > CC110X_MAX_FRAME_SIZE) {
cc110x_release(dev);
DEBUG("[cc110x] netdev_driver_t::send(): Frame size of %uB "
"exceeds maximum supported size of %uB\n",
(unsigned)(size + iol->iol_len),
(unsigned)CC110X_MAX_FRAME_SIZE);
DEBUG("[cc110x] netdev_driver_t::send(): Frame size of %"
PRIuSIZE "B exceeds maximum supported size of %uB\n",
size + iol->iol_len, (unsigned)CC110X_MAX_FRAME_SIZE);
return -1;
}
memcpy(dev->buf.data + size, iol->iol_base, iol->iol_len);
Expand Down
2 changes: 1 addition & 1 deletion drivers/cc1xxx_common/gnrc_netif_cc1xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static int cc1xxx_adpt_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)

/* get the payload size and the dst address details */
size = gnrc_pkt_len(pkt->next);
DEBUG("[cc1xxx-gnrc] send: payload of packet is %i\n", (int)size);
DEBUG("[cc1xxx-gnrc] send: payload of packet is %" PRIuSIZE "\n", size);
netif_hdr = (gnrc_netif_hdr_t *)pkt->data;

l2hdr.src_addr = cc1xxx_dev->addr;
Expand Down
2 changes: 1 addition & 1 deletion drivers/cc2420/cc2420.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ size_t cc2420_tx_prepare(cc2420_t *dev, const iolist_t *iolist)
cc2420_fifo_write(dev, iol->iol_base, iol->iol_len);
}
}
DEBUG("cc2420: tx_prep: loaded %i byte into the TX FIFO\n", (int)pkt_len);
DEBUG("cc2420: tx_prep: loaded %" PRIuSIZE " byte into the TX FIFO\n", pkt_len);

return pkt_len;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/dose/dose.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ static int _recv(netdev_t *dev, void *buf, size_t len, void *info)

size_t dummy;
if (crb_get_chunk_size(&ctx->rb, &dummy)) {
DEBUG("dose: %u byte pkt in rx queue\n", (unsigned)dummy);
DEBUG("dose: %" PRIuSIZE " byte pkt in rx queue\n", dummy);
netdev_trigger_event_isr(&ctx->netdev);
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/enc28j60/enc28j60.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ static int nd_recv(netdev_t *netdev, void *buf, size_t max_len, void *info)
next = (uint16_t)((head[1] << 8) | head[0]);
size = (uint16_t)((head[3] << 8) | head[2]) - 4; /* discard CRC */

DEBUG("[enc28j60] recv: size=%i next=%i buf=%p len=%d\n",
(int)size, (int)next, buf, max_len);
DEBUG("[enc28j60] recv: size=%u next=%u buf=%p len=%" PRIuSIZE "\n",
size, next, buf, max_len);

if (buf != NULL) {
/* read packet content into the supplied buffer */
Expand Down
6 changes: 3 additions & 3 deletions drivers/kw41zrf/kw41zrf_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ static int kw41zrf_netdev_send(netdev_t *netdev, const iolist_t *iolist)
for (const iolist_t *iol = iolist; iol; iol = iol->iol_next) {
/* current packet data + FCS too long */
if ((len + iol->iol_len) > (KW41ZRF_MAX_PKT_LENGTH - IEEE802154_FCS_LEN)) {
LOG_ERROR("[kw41zrf] packet too large (%u byte) to fit\n",
(unsigned)len + IEEE802154_FCS_LEN);
LOG_ERROR("[kw41zrf] packet too large (%" PRIuSIZE " byte) to fit\n",
len + IEEE802154_FCS_LEN);
return -EOVERFLOW;
}
len = kw41zrf_tx_load(iol->iol_base, iol->iol_len, len);
}

DEBUG("[kw41zrf] TX %u bytes\n", len);
DEBUG("[kw41zrf] TX %" PRIuSIZE " bytes\n", len);

/*
* First octet in the TX buffer contains the frame length.
Expand Down
4 changes: 2 additions & 2 deletions drivers/lcd/lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ void lcd_pixmap(lcd_t *dev, uint16_t x1, uint16_t x2,
size_t num_pix = (x2 - x1 + 1) * (y2 - y1 + 1);

DEBUG("[lcd]: Write x1: %" PRIu16 ", x2: %" PRIu16 ", "
"y1: %" PRIu16 ", y2: %" PRIu16 ". Num pixels: %lu\n",
x1, x2, y1, y2, (unsigned long)num_pix);
"y1: %" PRIu16 ", y2: %" PRIu16 ". Num pixels: %" PRIuSIZE "\n",
x1, x2, y1, y2, num_pix);

lcd_ll_acquire(dev);

Expand Down
5 changes: 3 additions & 2 deletions drivers/usbdev_mock/usbdev_mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdio.h>
#include <string.h>

#include "architecture.h"
#include "periph/usbdev.h"
#include "test_utils/expect.h"
#include "usbdev_mock.h"
Expand Down Expand Up @@ -200,8 +201,8 @@ void _ep_esr(usbdev_ep_t *ep)

int _xmit(usbdev_ep_t *ep, uint8_t *buf, size_t len)
{
DEBUG("[mock]: Readying EP %u, dir %s, len %u\n",
ep->num, ep->dir == USB_EP_DIR_OUT ? "out" : "in", (unsigned)len);
DEBUG("[mock]: Readying EP %u, dir %s, len %" PRIuSIZE "\n",
ep->num, ep->dir == USB_EP_DIR_OUT ? "out" : "in", len);
if (ep->num == 0) {
usbdev_mock_t *usbdev_mock = _ep2dev(ep);
usbdev_mock_ep_t *mock_ep = (usbdev_mock_ep_t *)ep;
Expand Down
2 changes: 1 addition & 1 deletion drivers/xbee/gnrc_xbee.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static int xbee_adpt_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)

/* get the payload size and the dst address details */
size = gnrc_pkt_len(pkt->next);
DEBUG("[xbee-gnrc] send: payload of packet is %i\n", (int)size);
DEBUG("[xbee-gnrc] send: payload of packet is %" PRIuSIZE "\n", size);
hdr = (gnrc_netif_hdr_t *)pkt->data;
if (hdr->flags & BCAST) {
uint16_t addr = 0xffff;
Expand Down
8 changes: 4 additions & 4 deletions drivers/xbee/xbee.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ static int xbee_send(netdev_t *dev, const iolist_t *iolist)
}

/* send the actual data packet */
DEBUG("[xbee] send: now sending out %i byte\n", (int)size);
DEBUG("[xbee] send: now sending out %" PRIuSIZE " byte\n", size);
mutex_lock(&(xbee->tx_lock));
for (const iolist_t *iol = iolist; iol; iol = iol->iol_next) {
if (iol->iol_len > 0) {
Expand Down Expand Up @@ -705,16 +705,16 @@ static int xbee_recv(netdev_t *dev, void *buf, size_t len, void *info)
size = (size_t)(xbee->rx_limit - 1);
if (buf == NULL) {
if (len > 0) {
DEBUG("[xbee] recv: reading size and dropping: %u\n", (unsigned)size);
DEBUG("[xbee] recv: reading size and dropping: %" PRIuSIZE "\n", size);
xbee->rx_count = 0;
}
else {
DEBUG("[xbee] recv: reading size without dropping: %u\n", (unsigned)size);
DEBUG("[xbee] recv: reading size without dropping: %" PRIuSIZE "\n", size);
}
}
else {
size = (size > len) ? len : size;
DEBUG("[xbee] recv: consuming packet: reading %u byte\n", (unsigned)size);
DEBUG("[xbee] recv: consuming packet: reading %" PRIuSIZE " byte\n", size);
memcpy(buf, xbee->rx_buf, size);
xbee->rx_count = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/dtls-echo/dtls-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static int dtls_handle_read(dtls_context_t *ctx)

if (res <= 0) {
if ((ENABLE_DEBUG) && (res != -EAGAIN) && (res != -ETIMEDOUT)) {
DEBUG("sock_udp_recv unexpected code error: %i\n", (int)res);
DEBUG("sock_udp_recv unexpected code error: %" PRIiSIZE "\n", res);
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/dtls-echo/dtls-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static int dtls_handle_read(dtls_context_t *ctx)

if (res <= 0) {
if ((ENABLE_DEBUG) && (res != -EAGAIN) && (res != -ETIMEDOUT)) {
DEBUG("sock_udp_recv unexpected code error: %i\n", (int)res);
DEBUG("sock_udp_recv unexpected code error: %" PRIiSIZE "\n", res);
}
return 0;
}
Expand Down
10 changes: 5 additions & 5 deletions examples/dtls-sock/dtls-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static int client_send(char *addr_str, char *data, size_t datalen)
res = credman_add(&credential0);
if (res < 0 && res != CREDMAN_EXIST) {
/* ignore duplicate credentials */
printf("Error cannot add credential to system: %d\n", (int)res);
printf("Error cannot add credential to system: %" PRIdSIZE "\n", res);
return -1;
}

Expand All @@ -170,13 +170,13 @@ static int client_send(char *addr_str, char *data, size_t datalen)
res = credman_add(&credential1);
if (res < 0 && res != CREDMAN_EXIST) {
/* ignore duplicate credentials */
printf("Error cannot add credential to system: %d\n", (int)res);
printf("Error cannot add credential to system: %" PRIdSIZE "\n", res);
return -1;
}

/* make the new credential available to the sock */
if (sock_dtls_add_credential(&dtls_sock, SOCK_DTLS_CLIENT_TAG_1) < 0) {
printf("Error cannot add credential to the sock: %d\n", (int)res);
printf("Error cannot add credential to the sock: %" PRIdSIZE "\n", res);
return -1;
}

Expand All @@ -192,7 +192,7 @@ static int client_send(char *addr_str, char *data, size_t datalen)
res = sock_dtls_recv(&dtls_sock, &session, buf, sizeof(buf),
SOCK_NO_TIMEOUT);
if (res != -SOCK_DTLS_HANDSHAKE) {
printf("Error creating session: %d\n", (int)res);
printf("Error creating session: %" PRIdSIZE "\n", res);
sock_dtls_close(&dtls_sock);
sock_udp_close(&udp_sock);
return -1;
Expand All @@ -208,7 +208,7 @@ static int client_send(char *addr_str, char *data, size_t datalen)
uint8_t rcv[512];
if ((res = sock_dtls_recv(&dtls_sock, &session, rcv, sizeof(rcv),
SOCK_NO_TIMEOUT)) >= 0) {
printf("Received %d bytes: \"%.*s\"\n", (int)res, (int)res,
printf("Received %" PRIdSIZE " bytes: \"%.*s\"\n", res, (int)res,
(char *)rcv);
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/dtls-sock/dtls-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void *dtls_server_wrapper(void *arg)
res = credman_add(&credential0);
if (res < 0 && res != CREDMAN_EXIST) {
/* ignore duplicate credentials */
printf("Error cannot add credential to system: %d\n", (int)res);
printf("Error cannot add credential to system: %" PRIdSIZE "\n", res);
return NULL;
}

Expand All @@ -163,13 +163,13 @@ void *dtls_server_wrapper(void *arg)
res = credman_add(&credential1);
if (res < 0 && res != CREDMAN_EXIST) {
/* ignore duplicate credentials */
printf("Error cannot add credential to system: %d\n", (int)res);
printf("Error cannot add credential to system: %" PRIdSIZE "\n", res);
return NULL;
}

/* make the new credential available to the sock */
if (sock_dtls_add_credential(&sock, SOCK_DTLS_SERVER_TAG_1) < 0) {
printf("Error cannot add credential to the sock: %d\n", (int)res);
printf("Error cannot add credential to the sock: %" PRIdSIZE "\n", res);
return NULL;
}

Expand All @@ -187,10 +187,10 @@ void *dtls_server_wrapper(void *arg)
res = sock_dtls_recv(&sock, &session, rcv, sizeof(rcv),
10 * US_PER_SEC);
if (res >= 0) {
printf("Received %d bytes -- (echo)\n", (int)res);
printf("Received %" PRIdSIZE " bytes -- (echo)\n", res);
res = sock_dtls_send(&sock, &session, rcv, (size_t)res, 0);
if (res < 0) {
printf("Error resending DTLS message: %d", (int)res);
printf("Error resending DTLS message: %" PRIdSIZE, res);
}
sock_dtls_session_destroy(&sock, &session);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/emcute_mqttsn/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ static int cmd_pub(int argc, char **argv)
return 1;
}

printf("Published %i bytes to topic '%s [%i]'\n",
(int)strlen(argv[2]), t.name, t.id);
printf("Published %" PRIuSIZE " bytes to topic '%s [%i]'\n",
strlen(argv[2]), t.name, t.id);

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/gcoap/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ int gcoap_cli_cmd(int argc, char **argv)
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_NONE);
}

printf("gcoap_cli: sending msg ID %u, %u bytes\n", coap_get_id(&pdu),
(unsigned) len);
printf("gcoap_cli: sending msg ID %u, %" PRIuSIZE " bytes\n",
coap_get_id(&pdu), len);
if (!_send(&buf[0], len, argv[apos])) {
puts("gcoap_cli: msg send failed");
}
Expand Down
4 changes: 2 additions & 2 deletions examples/gcoap_block_server/gcoap_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ static ssize_t _sha256_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_r

int blockwise = coap_get_block1(pdu, &block1);

printf("_sha256_handler: received data: offset=%u len=%u blockwise=%i more=%i\n",
(unsigned)block1.offset, pdu->payload_len, blockwise, block1.more);
printf("_sha256_handler: received data: offset=%" PRIuSIZE " len=%u blockwise=%i more=%i\n",
block1.offset, pdu->payload_len, blockwise, block1.more);

/* initialize sha256 calculation and add payload bytes */
if (block1.blknum == 0) {
Expand Down
3 changes: 2 additions & 1 deletion examples/lua_REPL/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdio.h>
#include <string.h>

#include "architecture.h"
#include "lua_run.h"
#include "lua_builtin.h"
#include "blob/repl.lua.h"
Expand All @@ -44,7 +45,7 @@ const size_t lua_riot_builtin_lua_table_len = 1;

int main(void)
{
printf("Using memory range for Lua heap: %p - %p, %zu bytes\n",
printf("Using memory range for Lua heap: %p - %p, %" PRIuSIZE " bytes\n",
(void *)lua_memory, (void *)(lua_memory + MAIN_LUA_MEM_SIZE), sizeof(void *));

while (1) {
Expand Down
4 changes: 2 additions & 2 deletions examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ ssize_t _sha256_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, coap_request_
coap_block1_t block1;
int blockwise = coap_get_block1(pkt, &block1);

printf("_sha256_handler(): received data: offset=%u len=%u blockwise=%i more=%i\n", \
(unsigned)block1.offset, pkt->payload_len, blockwise, block1.more);
printf("_sha256_handler(): received data: offset=%" PRIuSIZE " len=%u blockwise=%i more=%i\n",
block1.offset, pkt->payload_len, blockwise, block1.more);

if (block1.offset == 0) {
puts("_sha256_handler(): init");
Expand Down
2 changes: 1 addition & 1 deletion examples/posix_sockets/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static int udp_send(char *addr_str, char *port_str, char *data, unsigned int num
puts("could not send");
}
else {
printf("Success: send %u byte to %s:%u\n", (unsigned)data_len, addr_str, port);
printf("Success: send %" PRIuSIZE " byte to %s:%u\n", data_len, addr_str, port);
}

usleep(delay);
Expand Down
3 changes: 2 additions & 1 deletion examples/riot_and_cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @author DangNhat Pham-Huu <[email protected]>
*/

#include "architecture.h"
#include "thread.h"

#include "c_functions.h"
Expand Down Expand Up @@ -64,7 +65,7 @@ int main()
vInts.push_back(1);
vInts.push_back(3);
vInts.push_back(2);
printf("The vector vInts has been filled with %d numbers.\n", (int)vInts.size());
printf("The vector vInts has been filled with %" PRIuSIZE " numbers.\n", vInts.size());

printf("\n-= Test iterator =-\n");
printf("The content of vInts = { ");
Expand Down
8 changes: 4 additions & 4 deletions pkg/littlefs/fs/littlefs_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ static ssize_t _write(vfs_file_t *filp, const void *src, size_t nbytes)

mutex_lock(&fs->lock);

DEBUG("littlefs: write: filp=%p, fp=%p, src=%p, nbytes=%u\n",
(void *)filp, (void *)fp, (void *)src, (unsigned)nbytes);
DEBUG("littlefs: write: filp=%p, fp=%p, src=%p, nbytes=%" PRIuSIZE "\n",
(void *)filp, (void *)fp, (void *)src, nbytes);

ssize_t ret = lfs_file_write(&fs->fs, fp, src, nbytes);
mutex_unlock(&fs->lock);
Expand All @@ -365,8 +365,8 @@ static ssize_t _read(vfs_file_t *filp, void *dest, size_t nbytes)

mutex_lock(&fs->lock);

DEBUG("littlefs: read: filp=%p, fp=%p, dest=%p, nbytes=%u\n",
(void *)filp, (void *)fp, (void *)dest, (unsigned)nbytes);
DEBUG("littlefs: read: filp=%p, fp=%p, dest=%p, nbytes=%" PRIuSIZE "\n",
(void *)filp, (void *)fp, (void *)dest, nbytes);

ssize_t ret = lfs_file_read(&fs->fs, fp, dest, nbytes);
mutex_unlock(&fs->lock);
Expand Down
Loading

0 comments on commit 418bc2f

Please sign in to comment.