aboutsummaryrefslogtreecommitdiffstats
path: root/src/expand-common.c
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2025-10-02 12:24:20 +0100
committerPádraig Brady <P@draigBrady.com>2025-10-02 15:26:02 +0100
commit75e3888bd3e6787f066f23b3c606d0e8f49fa5cc (patch)
treef256f07ee905fdf0496b1184e6bda1eda90065e0 /src/expand-common.c
parentdoc: man: consistently format -X[OPTIONAL] form (diff)
downloadcoreutils-75e3888bd3e6787f066f23b3c606d0e8f49fa5cc.tar.gz
coreutils-75e3888bd3e6787f066f23b3c606d0e8f49fa5cc.zip
unexpand: fix heap buffer overflow with --tabs=[+/]NUM
This avoids CWE-122: Heap-based Buffer Overflow where we could write blank characters beyond the allocated heap buffer. * src/expand-common.c (set_max_column_width): Refactor function from ... (add_tab_stop): ... here. (set_extend_size): Call new function. (set_increment_size): Likewise. * NEWS: Mention the bug fix. Fixes https://bugs.gnu.org/79555
Diffstat (limited to 'src/expand-common.c')
-rw-r--r--src/expand-common.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/expand-common.c b/src/expand-common.c
index ca2ad4d67..14dd804f9 100644
--- a/src/expand-common.c
+++ b/src/expand-common.c
@@ -70,6 +70,15 @@ static bool have_read_stdin = false;
int exit_status = EXIT_SUCCESS;
+static void
+set_max_column_width (colno width)
+{
+ if (max_column_width < width)
+ {
+ if (ckd_add (&max_column_width, width, 0))
+ error (EXIT_FAILURE, 0, _("tabs are too far apart"));
+ }
+}
/* Add tab stop TABVAL to the end of 'tab_list'. */
extern void
@@ -82,11 +91,7 @@ add_tab_stop (colno tabval)
tab_list = xpalloc (tab_list, &n_tabs_allocated, 1, -1, sizeof *tab_list);
tab_list[first_free_tab++] = tabval;
- if (max_column_width < column_width)
- {
- if (ckd_add (&max_column_width, column_width, 0))
- error (EXIT_FAILURE, 0, _("tabs are too far apart"));
- }
+ set_max_column_width (column_width);
}
static bool
@@ -103,6 +108,8 @@ set_extend_size (colno tabval)
}
extend_size = tabval;
+ set_max_column_width (extend_size);
+
return ok;
}
@@ -120,6 +127,8 @@ set_increment_size (colno tabval)
}
increment_size = tabval;
+ set_max_column_width (increment_size);
+
return ok;
}