Skip to content

Commit 6267e64

Browse files
authored
sys/log: add format attribute to modlog_printf prototype (#3530)
- Add format(printf) attribute to modlog_printf to enable compile-time checking that format strings match arguments. By default the attribute is disabled; it can be enabled by setting MODLOG_USE_PRINTF_ATTRIBUTE to 1. Note: Enabling the attribute will cause incorrect modlog_printf usage which previously compiled successfully, to no longer compile.
1 parent f69738b commit 6267e64

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

net/oic/src/port/mynewt/ip_adaptor.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ oc_send_buffer_ip6_int(struct os_mbuf *m, int is_mcast)
165165
to.msin6_scope_id = itf2.mif_idx;
166166
rc = mn_sendto(oc_ucast6, n, (struct mn_sockaddr *) &to);
167167
if (rc != 0) {
168-
OC_LOG_ERROR("Failed to send buffer %u on itf %d\n",
169-
OS_MBUF_PKTHDR(m)->omp_len, to.msin6_scope_id);
168+
OC_LOG_ERROR("Failed to send buffer %u on itf %u\n",
169+
OS_MBUF_PKTHDR(m)->omp_len,
170+
(unsigned int)to.msin6_scope_id);
170171
STATS_INC(oc_ip_stats, oerr);
171172
os_mbuf_free_chain(n);
172173
}
@@ -176,8 +177,9 @@ oc_send_buffer_ip6_int(struct os_mbuf *m, int is_mcast)
176177
to.msin6_scope_id = itf2.mif_idx;
177178
rc = mn_sendto(oc_ucast6, m, (struct mn_sockaddr *) &to);
178179
if (rc != 0) {
179-
OC_LOG_ERROR("Failed sending buffer %u on itf %d\n",
180-
OS_MBUF_PKTHDR(m)->omp_len, to.msin6_scope_id);
180+
OC_LOG_ERROR("Failed sending buffer %u on itf %u\n",
181+
OS_MBUF_PKTHDR(m)->omp_len,
182+
(unsigned int)to.msin6_scope_id);
181183
STATS_INC(oc_ip_stats, oerr);
182184
os_mbuf_free_chain(m);
183185
}
@@ -187,8 +189,8 @@ oc_send_buffer_ip6_int(struct os_mbuf *m, int is_mcast)
187189
} else {
188190
rc = mn_sendto(oc_ucast6, m, (struct mn_sockaddr *) &to);
189191
if (rc != 0) {
190-
OC_LOG_ERROR("Failed to send buffer %u on itf %d\n",
191-
OS_MBUF_PKTHDR(m)->omp_len, to.msin6_scope_id);
192+
OC_LOG_ERROR("Failed to send buffer %u on itf %u\n",
193+
OS_MBUF_PKTHDR(m)->omp_len, (unsigned int)to.msin6_scope_id);
192194
STATS_INC(oc_ip_stats, oerr);
193195
os_mbuf_free_chain(m);
194196
}

net/osdp/src/osdp_phy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ osdp_phy_check_packet(struct osdp_pd *pd, uint8_t *buf, int len,
282282

283283
if (!ISSET_FLAG(pd, PD_FLAG_PD_MODE) &&
284284
!(pkt->pd_address & 0x80)) {
285-
OSDP_LOG_ERROR("Reply without address MSB set!\n", pkt->pd_address);
285+
OSDP_LOG_ERROR("Reply without address MSB set 0x%02x!\n", pkt->pd_address);
286286
return OSDP_ERR_PKT_FMT;
287287
}
288288

sys/log/modlog/include/modlog/modlog.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ int modlog_foreach(modlog_foreach_fn *fn, void *arg);
190190
* @param level The severity of the log entry to write.
191191
* @param msg The "printf" formatted string to write.
192192
*/
193-
void modlog_printf(uint8_t module, uint8_t level, const char *msg, ...);
193+
194+
void modlog_printf(uint8_t module, uint8_t level, const char *msg, ...)
195+
#if MYNEWT_VAL(MODLOG_USE_PRINTF_ATTRIBUTE)
196+
__attribute__((format(printf, 3, 4)))
197+
#endif
198+
;
194199

195200
/**
196201
* @brief Writes a specified number of bytes as a text entry to the specified log module.

sys/log/modlog/syscfg.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ syscfg.defs:
4545
description: >
4646
Sysinit stage for modular logging functionality.
4747
value: 100
48+
MODLOG_USE_PRINTF_ATTRIBUTE:
49+
description: >
50+
Use the `format(printf, ...)` attribute on the modlog_printf
51+
prototype. Enables compile-time checking of modlog_printf arguments
52+
against the format string.
53+
value: 0

0 commit comments

Comments
 (0)