Skip to content

Commit c42b578

Browse files
committed
fix(mdns): Debug prints with small buffer
1 parent 4379c7e commit c42b578

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

components/mdns/mdns_debug.c

+51-7
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,68 @@ static inline void mdns_dbg_flush(void)
3535
}
3636
}
3737

38-
static void mdns_dbg_printf(const char *fmt, ...)
38+
static void mdns_dbg_vprintf(const char *fmt, va_list args)
3939
{
40-
va_list ap;
41-
va_start(ap, fmt);
42-
int len = vsnprintf(s_mdns_dbg_buf + s_mdns_dbg_pos, MDNS_DBG_MAX_LINE - s_mdns_dbg_pos, fmt, ap);
43-
va_end(ap);
40+
// Try to format directly into the buffer
41+
int len = vsnprintf(s_mdns_dbg_buf + s_mdns_dbg_pos,
42+
MDNS_DBG_MAX_LINE - s_mdns_dbg_pos,
43+
fmt, args);
4444

4545
if (len < 0) {
46-
return;
46+
return; // Error in formatting
4747
}
4848

49-
s_mdns_dbg_pos += len;
49+
// Check if the entire formatted string fit in the buffer
50+
if (len < (MDNS_DBG_MAX_LINE - s_mdns_dbg_pos)) {
51+
// If it fit, just update the position
52+
s_mdns_dbg_pos += len;
53+
} else {
54+
// The formatted string was truncated because it didn't fit
55+
// First, flush what we have (the partial string)
56+
mdns_dbg_flush();
57+
58+
// Create a new va_list copy and try again with the full buffer
59+
va_list args_copy;
60+
va_copy(args_copy, args);
5061

62+
// Format again with the entire buffer available
63+
len = vsnprintf(s_mdns_dbg_buf, MDNS_DBG_MAX_LINE - 1, fmt, args_copy);
64+
va_end(args_copy);
65+
66+
if (len < 0) {
67+
return; // Error
68+
}
69+
70+
// Check if content will be lost (true truncation)
71+
if (len >= MDNS_DBG_MAX_LINE - 1) {
72+
// This is when actual content will be lost - log a warning
73+
ESP_LOGW("mdns", "Message truncated: length (%d) exceeds buffer size (%d). Consider increasing CONFIG_MDNS_DEBUG_BUFFER_SIZE.",
74+
len, MDNS_DBG_MAX_LINE - 1);
75+
76+
// Display what we could fit, then flush and return
77+
s_mdns_dbg_pos = MDNS_DBG_MAX_LINE - 1;
78+
mdns_dbg_flush();
79+
return;
80+
}
81+
82+
// If we get here, the whole message fit this time
83+
s_mdns_dbg_pos = len;
84+
}
85+
86+
// If buffer is nearly full after this operation, flush it
5187
if (s_mdns_dbg_pos >= MDNS_DBG_MAX_LINE - 1) {
5288
mdns_dbg_flush();
5389
}
5490
}
5591

92+
static void mdns_dbg_printf(const char *fmt, ...)
93+
{
94+
va_list ap;
95+
va_start(ap, fmt);
96+
mdns_dbg_vprintf(fmt, ap);
97+
va_end(ap);
98+
}
99+
56100
#define dbg_printf(...) mdns_dbg_printf(__VA_ARGS__)
57101
#else
58102
#define dbg_printf(...) printf(__VA_ARGS__)

0 commit comments

Comments
 (0)