Skip to content

Commit 3270a27

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

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

components/mdns/mdns_debug.c

+40-7
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,57 @@ 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);
61+
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+
// Update position based on what fit this time
71+
s_mdns_dbg_pos = (len >= MDNS_DBG_MAX_LINE - 1) ?
72+
(MDNS_DBG_MAX_LINE - 1) : len;
73+
}
5074

75+
// If buffer is nearly full after this operation, flush it
5176
if (s_mdns_dbg_pos >= MDNS_DBG_MAX_LINE - 1) {
5277
mdns_dbg_flush();
5378
}
5479
}
5580

81+
static void mdns_dbg_printf(const char *fmt, ...)
82+
{
83+
va_list ap;
84+
va_start(ap, fmt);
85+
mdns_dbg_vprintf(fmt, ap);
86+
va_end(ap);
87+
}
88+
5689
#define dbg_printf(...) mdns_dbg_printf(__VA_ARGS__)
5790
#else
5891
#define dbg_printf(...) printf(__VA_ARGS__)

0 commit comments

Comments
 (0)