expand: check for colno overflow

* src/expand-common.c (get_next_tab_column): Check for tab
stop overflow here.  All callers changed to not check.
* src/expand.c (expand): Use colno for column number.
This commit is contained in:
Paul Eggert 2024-11-07 12:51:04 -08:00
parent b857d66b51
commit 7572abed94
3 changed files with 39 additions and 41 deletions

View File

@ -271,16 +271,21 @@ finalize_tab_stops (void)
}
/* Return number of first tab stop after COLUMN. TAB_INDEX specifies
amny multiple tab-sizes. Set *LAST_TAB depending on whether we are
returning COLUMN + 1 merely because we're past the last tab.
If the number would overflow, diagnose this and exit. */
extern colno
get_next_tab_column (const colno column, idx_t *tab_index,
bool *last_tab)
get_next_tab_column (colno column, idx_t *tab_index, bool *last_tab)
{
*last_tab = false;
colno tab_distance;
/* single tab-size - return multiples of it */
if (tab_size)
return column + (tab_size - column % tab_size);
tab_distance = tab_size - column % tab_size;
else
{
/* multiple tab-sizes - iterate them until the tab position is beyond
the current input column. */
for ( ; *tab_index < first_free_tab ; (*tab_index)++ )
@ -292,21 +297,26 @@ get_next_tab_column (const colno column, idx_t *tab_index,
/* relative last tab - return multiples of it */
if (extend_size)
return column + (extend_size - column % extend_size);
/* incremental last tab - add increment_size to the previous tab stop */
if (increment_size)
tab_distance = extend_size - column % extend_size;
else if (increment_size)
{
/* incremental last tab - add increment_size to the previous
tab stop */
colno end_tab = tab_list[first_free_tab - 1];
return column + (increment_size - ((column - end_tab) % increment_size));
tab_distance = increment_size - ((column - end_tab) % increment_size);
}
else
{
*last_tab = true;
return 0;
tab_distance = 1;
}
}
colno tab_stop;
if (ckd_add (&tab_stop, column, tab_distance))
error (EXIT_FAILURE, 0, _("input line is too long"));
return tab_stop;
}
/* Sets new file-list */

View File

@ -113,7 +113,7 @@ expand (void)
is true: */
/* Column of next input character. */
uintmax_t column = 0;
colno column = 0;
/* Index in TAB_LIST of next tab stop to examine. */
idx_t tab_index = 0;
@ -131,17 +131,9 @@ expand (void)
if (c == '\t')
{
/* Column the next input tab stop is on. */
uintmax_t next_tab_column;
bool last_tab;
next_tab_column = get_next_tab_column (column, &tab_index,
&last_tab);
if (last_tab)
next_tab_column = column + 1;
if (next_tab_column < column)
error (EXIT_FAILURE, 0, _("input line is too long"));
colno next_tab_column
= get_next_tab_column (column, &tab_index, &last_tab);
while (++column < next_tab_column)
if (putchar (' ') < 0)
@ -158,8 +150,7 @@ expand (void)
}
else
{
column++;
if (!column)
if (ckd_add (&column, column, 1))
error (EXIT_FAILURE, 0, _("input line is too long"));
}

View File

@ -175,9 +175,6 @@ unexpand (void)
if (convert)
{
if (next_tab_column < column)
error (EXIT_FAILURE, 0, _("input line is too long"));
if (c == '\t')
{
column = next_tab_column;