cf4a81
From e5f96e79d69a1d295f19130da00ec6514d28a8ae Mon Sep 17 00:00:00 2001
cf4a81
From: Lianbo Jiang <lijiang@redhat.com>
cf4a81
Date: Tue, 6 Mar 2018 19:07:00 +0900
cf4a81
Subject: [PATCH] Fix array index out of bound exception
cf4a81
cf4a81
A data overflow may lead to a reversal, which may turn a positive
cf4a81
number into a large negative number, in this case, the string's
cf4a81
length will exceed the array size(for example, eta: -2147483648s),
cf4a81
here the array size is defined 16 characters. So, it is nessasary
cf4a81
to consider some exceptions.
cf4a81
cf4a81
Signed-off-by: Lianbo Jiang <lijiang@redhat.com>
cf4a81
---
cf4a81
 print_info.c | 21 +++++++++++++--------
cf4a81
 1 file changed, 13 insertions(+), 8 deletions(-)
cf4a81
cf4a81
diff --git a/makedumpfile-1.6.2/print_info.c b/makedumpfile-1.6.2/print_info.c
cf4a81
index e0e6a27..09e215a 100644
cf4a81
--- a/makedumpfile-1.6.2/print_info.c
cf4a81
+++ b/makedumpfile-1.6.2/print_info.c
cf4a81
@@ -16,6 +16,8 @@
cf4a81
 #include "print_info.h"
cf4a81
 #include <time.h>
cf4a81
 #include <string.h>
cf4a81
+#include <stdint.h>
cf4a81
+#include <inttypes.h>
cf4a81
 
cf4a81
 #define PROGRESS_MAXLEN		"50"
cf4a81
 
cf4a81
@@ -352,18 +354,21 @@ static void calc_delta(struct timeval *tv_start, struct timeval *delta)
cf4a81
 }
cf4a81
 
cf4a81
 /* produce less than 12 bytes on msg */
cf4a81
-static int eta_to_human_short (int secs, char* msg)
cf4a81
+static int eta_to_human_short (int64_t secs, char* msg, int maxsize)
cf4a81
 {
cf4a81
 	strcpy(msg, "eta: ");
cf4a81
 	msg += strlen("eta: ");
cf4a81
 	if (secs < 100)
cf4a81
-		sprintf(msg, "%ds", secs);
cf4a81
+		snprintf(msg, maxsize, "%"PRId64"s", secs);
cf4a81
 	else if (secs < 100 * 60)
cf4a81
-		sprintf(msg, "%dm%ds", secs / 60, secs % 60);
cf4a81
+		snprintf(msg, maxsize, "%"PRId64"m""%"PRId64"s",
cf4a81
+			secs / 60, secs % 60);
cf4a81
 	else if (secs < 48 * 3600)
cf4a81
-		sprintf(msg, "%dh%dm", secs / 3600, (secs / 60) % 60);
cf4a81
+		snprintf(msg, maxsize, "%"PRId64"h""%"PRId64"m",
cf4a81
+			secs / 3600, (secs / 60) % 60);
cf4a81
 	else if (secs < 100 * 86400)
cf4a81
-		sprintf(msg, "%dd%dh", secs / 86400, (secs / 3600) % 24);
cf4a81
+		snprintf(msg, maxsize, "%"PRId64"d""%"PRId64"h",
cf4a81
+			secs / 86400, (secs / 3600) % 24);
cf4a81
 	else
cf4a81
 		sprintf(msg, ">2day");
cf4a81
 	return 0;
cf4a81
@@ -379,8 +384,8 @@ print_progress(const char *msg, unsigned long current, unsigned long end, struct
cf4a81
 	static unsigned int lapse = 0;
cf4a81
 	static const char *spinner = "/|\\-";
cf4a81
 	struct timeval delta;
cf4a81
-	double eta;
cf4a81
-	char eta_msg[16] = " ";
cf4a81
+	int64_t eta;
cf4a81
+	char eta_msg[32] = " ";
cf4a81
 
cf4a81
 	if (current < end) {
cf4a81
 		tm = time(NULL);
cf4a81
@@ -395,7 +400,7 @@ print_progress(const char *msg, unsigned long current, unsigned long end, struct
cf4a81
 		calc_delta(start, &delta);
cf4a81
 		eta = delta.tv_sec + delta.tv_usec / 1e6;
cf4a81
 		eta = (100 - progress) * eta / progress;
cf4a81
-		eta_to_human_short(eta, eta_msg);
cf4a81
+		eta_to_human_short(eta, eta_msg, sizeof(eta_msg));
cf4a81
 	}
cf4a81
 	if (flag_ignore_r_char) {
cf4a81
 		PROGRESS_MSG("%-" PROGRESS_MAXLEN "s: [%5.1f %%] %c  %16s\n",
cf4a81
-- 
cf4a81
2.9.5
cf4a81