Skip to content

Commit a36bad1

Browse files
authored
Fix column width calculation issue with certain terminal widths
If the reported terminal width is 0 or less than 42, the signed variable width was set to a negative number that was then assigned to the unsigned column width becoming a huge number. Add comments and change logic to better explain what's happening. Reviewed-by: Tony Hutter <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Philip Pokorny <[email protected]> Closes #10247
1 parent 1b66495 commit a36bad1

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

cmd/zpool/zpool_main.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,22 +5148,48 @@ print_zpool_script_list(char *subcommand)
51485148
/*
51495149
* Set the minimum pool/vdev name column width. The width must be at least 10,
51505150
* but may be as large as the column width - 42 so it still fits on one line.
5151+
* NOTE: 42 is the width of the default capacity/operations/bandwidth output
51515152
*/
51525153
static int
51535154
get_namewidth_iostat(zpool_handle_t *zhp, void *data)
51545155
{
51555156
iostat_cbdata_t *cb = data;
5156-
int width, columns;
5157+
int width, available_width;
51575158

5159+
/*
5160+
* get_namewidth() returns the maximum width of any name in that column
5161+
* for any pool/vdev/device line that will be output.
5162+
*/
51585163
width = get_namewidth(zhp, cb->cb_namewidth, cb->cb_name_flags,
51595164
cb->cb_verbose);
5160-
columns = get_columns();
51615165

5166+
/*
5167+
* The width we are calculating is the width of the header and also the
5168+
* padding width for names that are less than maximum width. The stats
5169+
* take up 42 characters, so the width available for names is:
5170+
*/
5171+
available_width = get_columns() - 42;
5172+
5173+
/*
5174+
* If the maximum width fits on a screen, then great! Make everything
5175+
* line up by justifying all lines to the same width. If that max
5176+
* width is larger than what's available, the name plus stats won't fit
5177+
* on one line, and justifying to that width would cause every line to
5178+
* wrap on the screen. We only want lines with long names to wrap.
5179+
* Limit the padding to what won't wrap.
5180+
*/
5181+
if (width > available_width)
5182+
width = available_width;
5183+
5184+
/*
5185+
* And regardless of whatever the screen width is (get_columns can
5186+
* return 0 if the width is not known or less than 42 for a narrow
5187+
* terminal) have the width be a minimum of 10.
5188+
*/
51625189
if (width < 10)
51635190
width = 10;
5164-
if (width > columns - 42)
5165-
width = columns - 42;
51665191

5192+
/* Save the calculated width */
51675193
cb->cb_namewidth = width;
51685194

51695195
return (0);

0 commit comments

Comments
 (0)