This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix UB in diagnostic.c
- From: Marek Polacek <polacek at redhat dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Wed, 13 Aug 2014 15:34:22 +0200
- Subject: [PATCH] Fix UB in diagnostic.c
- Authentication-results: sourceware.org; auth=none
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