From: Chris Luke Date: Thu, 19 May 2016 18:23:25 +0000 (-0400) Subject: VPP-74 Fix signedness issue when terminal resizes X-Git-Tag: v16.09-rc1~384 X-Git-Url: https://gerrit.fd.io/r/gitweb?a=commitdiff_plain;h=270b6dee8c1f7686ee4c13d55d3aa2cd6c788c3f;p=vpp.git VPP-74 Fix signedness issue when terminal resizes When re-locating our current viewport into the pager buffer we need to verify that the new viewport is within the boundaries of the index. This condition is considered very rare, but nontheless the check is needed. Unfortunately I assumed the variable was signed; it is not, and the subtraction can in some cases cause the value to be negative. This is therefore a bonafide semantic error that may cause problems. This patch reworks the logic to avoid having to change it to be signed. Change-Id: I26f0747d38dcc43dd9c092d50f2489b122009e7b Signed-off-by: Chris Luke --- diff --git a/vlib/vlib/unix/cli.c b/vlib/vlib/unix/cli.c index c34ae219297..92c6941ad41 100644 --- a/vlib/vlib/unix/cli.c +++ b/vlib/vlib/unix/cli.c @@ -629,11 +629,16 @@ static void unix_cli_cli_prompt(unix_cli_file_t * cf, unix_file_t * uf) static void unix_cli_pager_prompt(unix_cli_file_t * cf, unix_file_t * uf) { u8 * prompt; + u32 h; + + h = cf->pager_start + (cf->height - 1); + if (h > vec_len (cf->pager_index)) + h = vec_len (cf->pager_index); prompt = format(0, "\r%s-- more -- (%d-%d/%d)%s", cf->ansi_capable ? ANSI_BOLD : "", cf->pager_start + 1, - cf->pager_start + cf->height, + h, vec_len (cf->pager_index), cf->ansi_capable ? ANSI_RESET: ""); @@ -866,10 +871,10 @@ static void unix_cli_pager_reindex (unix_cli_file_t * cf) */ if (cf->pager_start >= vec_len (cf->pager_index)) { - cf->pager_start = vec_len (cf->pager_index) - cf->height + 1; - - if (cf->pager_start < 0) + if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1)) cf->pager_start = 0; + else + cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1); } }