https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78608

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
I suspect the same problem should come up with a call to vsprintf.  The patch
below should handle this case but I haven't don't done a bootstrap-ubsan to
verify there are no others like it.

diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c
index eceed3e..2b0781a 100644
--- a/gcc/gimple-ssa-sprintf.c
+++ b/gcc/gimple-ssa-sprintf.c
@@ -565,7 +565,13 @@ tree_digits (tree x, int base, bool plus, bool prefix)
       if (tree_fits_shwi_p (x))
        {
          HOST_WIDE_INT i = tree_to_shwi (x);
-         if (i < 0)
+         if (HOST_WIDE_INT_MIN == i)
+           {
+             /* Avoid undefined behavior due to negating a minimum.  */
+             absval = HOST_WIDE_INT_MAX;
+             res = 1;
+           }
+         else if (i < 0)
            {
              absval = -i;
              res = 1;

Reply via email to