This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix UB in diagnostic.c


This should fix an undefined behavior in diagnostics.c.
Under certain circumstances, max_width is (INT_MAX - 1),
and right_margin is -4 -> the subtraction overflows.
Changing the types to unsigned would involve changing
much more code than just one cast.

BTW, the diagnostics we output for the testcase in the PR
is crap - but I'm not fixing it now.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2014-08-13  Marek Polacek  <polacek@redhat.com>

	PR c/62059
	* diagnostic.c (adjust_line): Perform the subtraction in unsigned.

diff --git gcc/diagnostic.c gcc/diagnostic.c
index 0cc7593..6bc9a2c 100644
--- gcc/diagnostic.c
+++ gcc/diagnostic.c
@@ -271,7 +271,7 @@ adjust_line (const char *line, int line_width,
   int column = *column_p;
 
   right_margin = MIN (line_width - column, right_margin);
-  right_margin = max_width - right_margin;
+  right_margin = max_width - (unsigned int) right_margin;
   if (line_width >= max_width && column > right_margin)
     {
       line += column - right_margin;

	Marek


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]