From 487d6b09f1925546684d4f32da9c00d141434cd4 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Tue, 21 Jan 2025 18:16:27 +0100 Subject: [PATCH] MINOR: tevt: Improve function to convert a termination events log to string The function is now responsible to handle empty log because no event was reported. In that case, an empty string is returned. It is also responsible to handle case where termination events log is not supported for an given entity (for instance the quic mux for now). In that case, a dash ("-") is returned. --- include/haproxy/connection.h | 15 +++++++++++++-- src/stream.c | 14 +++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/haproxy/connection.h b/include/haproxy/connection.h index 33607ea3a..a76f0ac13 100644 --- a/include/haproxy/connection.h +++ b/include/haproxy/connection.h @@ -765,9 +765,19 @@ static inline const char *tevt_evts2str(uint32_t evts) { uint32_t evt_msk = 0xff000000; unsigned int evt_bits = 24; - int idx; + int idx = 0; - for (idx = 0; evt_msk; evt_msk >>= 8, evt_bits -= 8) { + /* no events: do nothing */ + if (!evts) + goto end; + + /* -1 means the feature is not supported for the location or the entity does not exist. print a dash */ + if (evts == UINT_MAX) { + tevt_evts_str[idx++] = '-'; + goto end; + } + + for (; evt_msk; evt_msk >>= 8, evt_bits -= 8) { unsigned char evt = (evts & evt_msk) >> evt_bits; unsigned int is_back; @@ -788,6 +798,7 @@ static inline const char *tevt_evts2str(uint32_t evts) tevt_evts_str[idx++] = hextab[evt & 0xf]; } + end: tevt_evts_str[idx] = '\0'; return tevt_evts_str; } diff --git a/src/stream.c b/src/stream.c index e85c2f612..d0a4a96d4 100644 --- a/src/stream.c +++ b/src/stream.c @@ -4286,13 +4286,13 @@ static int smp_fetch_tevts(const struct arg *args, struct sample *smp, const cha if (bconn && bconn->mux && bconn->mux->ctl) bc_mux_ret = bconn->mux->ctl(bconn, MUX_CTL_TEVTS, NULL); - chunk_printf(trash, "{%s,", fconn ? tevt_evts2str(fconn->term_evts_log) : "-"); - chunk_appendf(trash, "%s,", (fc_mux_ret != -1) ? tevt_evts2str(fc_mux_ret) : "-"); - chunk_appendf(trash, "%s,", smp->strm ? tevt_evts2str(smp->strm->scf->sedesc->term_evts_log) : "-"); - chunk_appendf(trash, "%s,", smp->strm ? tevt_evts2str(smp->strm->term_evts_log) : "-"); - chunk_appendf(trash, "%s,", smp->strm ? tevt_evts2str(smp->strm->scb->sedesc->term_evts_log) : "-"); - chunk_appendf(trash, "%s,", (bc_mux_ret != -1) ? tevt_evts2str(bc_mux_ret) : "-"); - chunk_appendf(trash, "%s}", bconn ? tevt_evts2str(bconn->term_evts_log) : "-"); + chunk_printf(trash, "{%s,", tevt_evts2str(fconn ? fconn->term_evts_log : -1)); + chunk_appendf(trash, "%s,", tevt_evts2str(fc_mux_ret)); + chunk_appendf(trash, "%s,", tevt_evts2str(smp->strm ? smp->strm->scf->sedesc->term_evts_log : -1)); + chunk_appendf(trash, "%s,", tevt_evts2str(smp->strm ? smp->strm->term_evts_log : -1)); + chunk_appendf(trash, "%s,", tevt_evts2str(smp->strm ? smp->strm->scb->sedesc->term_evts_log : -1)); + chunk_appendf(trash, "%s,", tevt_evts2str(bc_mux_ret)); + chunk_appendf(trash, "%s}", tevt_evts2str(bconn ? bconn->term_evts_log : -1)); smp->data.u.str = *trash; smp->data.type = SMP_T_STR;