diff mbox

Fix UB in diagnostic.c

Message ID 20140813133422.GC28291@redhat.com
State New
Headers show

Commit Message

Marek Polacek Aug. 13, 2014, 1:34 p.m. UTC
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.


	Marek
diff mbox

Patch

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;