From 69622ff37db03ed818aa9141cdbe874267a6d61d Mon Sep 17 00:00:00 2001 From: x2018 Date: Wed, 5 Nov 2025 23:50:51 +0800 Subject: [PATCH] tool_help: add checks to avoid unsigned wrap around Closes #19377 --- src/tool_help.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/tool_help.c b/src/tool_help.c index 7a3a4a3bf4..4509fa2b94 100644 --- a/src/tool_help.c +++ b/src/tool_help.c @@ -87,14 +87,18 @@ static void print_category(unsigned int category, unsigned int cols) if(len > longdesc) longdesc = len; } - if(longopt + longdesc > cols) + + if(longdesc > cols) + longopt = 0; /* avoid wrap-around */ + else if(longopt + longdesc > cols) longopt = cols - longdesc; for(i = 0; helptext[i].opt; ++i) if(helptext[i].categories & category) { size_t opt = longopt; size_t desclen = strlen(helptext[i].desc); - if(opt + desclen >= (cols - 2)) { + /* avoid wrap-around */ + if(cols >= 2 && opt + desclen >= (cols - 2)) { if(desclen < (cols - 2)) opt = (cols - 3) - desclen; else -- 2.47.3