dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone
7714f9
From 54e3d1414e1a031d6f635f8fcbe273eecfd65560 Mon Sep 17 00:00:00 2001
7714f9
From: Karel Zak <kzak@redhat.com>
7714f9
Date: Tue, 5 Feb 2019 12:06:00 +0100
7714f9
Subject: [PATCH 39/40] col: make flush_line() a little bit robust
7714f9
7714f9
The code is horrible. The core of the problem are signed integers
7714f9
and no check for the limits.
7714f9
7714f9
This patch fixes c->c_column = cur_col; where c_column is "short"
7714f9
and "cur_col" is int. Let's use "int" for all the variables. It's
7714f9
really not perfect as for bigger lines it can segfault again...
7714f9
7714f9
The patch also removes some unnecessary static variables.
7714f9
7714f9
Upstream: http://github.com/karelzak/util-linux/commit/004356f05018e3bfcaddd2652846659a4d8481f3
7714f9
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1803753
7714f9
Addresses: https://github.com/karelzak/util-linux/issues/749
7714f9
Signed-off-by: Karel Zak <kzak@redhat.com>
7714f9
---
7714f9
 text-utils/col.c | 11 ++++++-----
7714f9
 1 file changed, 6 insertions(+), 5 deletions(-)
7714f9
7714f9
diff --git a/text-utils/col.c b/text-utils/col.c
7714f9
index 3d9e15d26..c2f8db64d 100644
7714f9
--- a/text-utils/col.c
7714f9
+++ b/text-utils/col.c
7714f9
@@ -88,7 +88,7 @@ typedef char CSET;
7714f9
 typedef struct char_str {
7714f9
 #define	CS_NORMAL	1
7714f9
 #define	CS_ALTERNATE	2
7714f9
-	short		c_column;	/* column character is in */
7714f9
+	int		c_column;	/* column character is in */
7714f9
 	CSET		c_set;		/* character set (currently only 2) */
7714f9
 	wchar_t		c_char;		/* character in question */
7714f9
 	int		c_width;	/* character width */
7714f9
@@ -476,8 +476,9 @@ void flush_line(LINE *l)
7714f9
 	nchars = l->l_line_len;
7714f9
 
7714f9
 	if (l->l_needs_sort) {
7714f9
-		static CHAR *sorted;
7714f9
-		static int count_size, *count, i, save, sorted_size, tot;
7714f9
+		static CHAR *sorted = NULL;
7714f9
+		static int count_size = 0, *count = NULL, sorted_size = 0;
7714f9
+		int i, tot;
7714f9
 
7714f9
 		/*
7714f9
 		 * Do an O(n) sort on l->l_line by column being careful to
7714f9
@@ -494,7 +495,7 @@ void flush_line(LINE *l)
7714f9
 			    (unsigned)sizeof(int) * count_size);
7714f9
 		}
7714f9
 		memset(count, 0, sizeof(int) * l->l_max_col + 1);
7714f9
-		for (i = nchars, c = l->l_line; --i >= 0; c++)
7714f9
+		for (i = nchars, c = l->l_line; c && --i >= 0; c++)
7714f9
 			count[c->c_column]++;
7714f9
 
7714f9
 		/*
7714f9
@@ -502,7 +503,7 @@ void flush_line(LINE *l)
7714f9
 		 * indices into new line.
7714f9
 		 */
7714f9
 		for (tot = 0, i = 0; i <= l->l_max_col; i++) {
7714f9
-			save = count[i];
7714f9
+			int save = count[i];
7714f9
 			count[i] = tot;
7714f9
 			tot += save;
7714f9
 		}
7714f9
-- 
7714f9
2.25.4
7714f9