coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] maint: avoid signed overflow warning with -O3


From: Pádraig Brady
Subject: [PATCH] maint: avoid signed overflow warning with -O3
Date: Tue, 2 Dec 2014 00:36:15 +0000

Prompted by the implicit -O3 added by american-fuzzy-lop,
seen with GCC 4.9.2 on x86_64.

  src/pr.c: In function 'print_files.part.5':
  src/pr.c:1781:6: error: assuming signed overflow does not occur
  when simplifying conditional to constant [-Werror=strict-overflow]
     if (cols_ready_to_print () == 0)

This happens because cols_ready_to_print() is inlined
thus reducing the comparison to the N variable in print_page().
Now this can't overflow due to the protection when parsing the
specified column, but use an unsigned type to avoid the
apparent signed overflow.

* src/pr.c (cols_ready_to_print): Increment an unsigned type to
avoid the subsequent signed overflow warning.
---
 src/pr.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/pr.c b/src/pr.c
index 39be8be..53655a3 100644
--- a/src/pr.c
+++ b/src/pr.c
@@ -602,7 +602,7 @@ static bool failed_opens = false;
 #define POS_AFTER_TAB(c_, h_) ((h_) + TAB_WIDTH (c_, h_))
 
 /* (-NNN) Number of columns of text to print. */
-static int columns = 1;
+static size_t columns = 1;
 
 /* (+NNN:MMM) Page numbers on which to begin and stop printing.
    first_page_number = 0  will be used to check input only. */
@@ -768,12 +768,12 @@ static struct option const long_options[] =
 /* Return the number of columns that have either an open file or
    stored lines. */
 
-static int _GL_ATTRIBUTE_PURE
+static size_t _GL_ATTRIBUTE_PURE
 cols_ready_to_print (void)
 {
   COLUMN *q;
-  int i;
-  int n;
+  size_t i;
+  size_t n;
 
   n = 0;
   for (q = column_vector, i = 0; i < columns; ++q, ++i)
@@ -827,7 +827,7 @@ parse_column_count (char const *s)
 {
   long int tmp_long;
   if (xstrtol (s, NULL, 10, &tmp_long, "") != LONGINT_OK
-      || !(1 <= tmp_long && tmp_long <= INT_MAX))
+      || !(1 <= tmp_long && tmp_long <= SIZE_MAX))
     error (EXIT_FAILURE, 0,
            _("invalid number of columns: %s"), quote (s));
 
@@ -1815,7 +1815,7 @@ print_page (void)
               --p->lines_to_print;
               if (p->lines_to_print <= 0)
                 {
-                  if (cols_ready_to_print () <= 0)
+                  if (cols_ready_to_print () == 0)
                     break;
                 }
 
@@ -1849,7 +1849,7 @@ print_page (void)
           --lines_left_on_page;
         }
 
-      if (cols_ready_to_print () <= 0 && !extremities)
+      if (cols_ready_to_print () == 0 && !extremities)
         break;
 
       if (double_space && pv)
-- 
2.1.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]