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

            Bug ID: 78696
           Summary: [7 Regression] -fprintf-return-value misoptimizes %.Ng
                    where N is greater than 10
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jgreenhalgh at gcc dot gnu.org
  Target Milestone: ---

For this testcase:

  #include <stdio.h>

  int __attribute__ ((noinline))
  wrap_snprintf (char *buffer, size_t size, double value)
  {
    int ret = snprintf(buffer, size, "%.17g", value);
    return ret;
  }


  int main ()
  {
    char x[100] = {0};
    char y[100] = {0};
    int ret = wrap_snprintf (x, 100,  1.0);
    int ret2 = wrap_snprintf (y, 100, -1.0);
    printf ("%d\t%s\n%d\t%s\n", ret, x, ret2, y);
  }

GCC trunk will create (-O3) a binary which prints:

  19    1
  19    -1

Which is wrong.

GCC 5, and trunk with -fno-printf-return-value prints

  1     1
  2     -1

The choice of 1.0 is not special, GCC always assumes the size of the string
which will be printed by "%.17g" will be 19 characters long, and optimizes the
return value of wrap_snprintf to 19. 19 is the correct maximum length of the
string, but not the minimum, so this optimization is invalid.

Interestingly, for "%.10g", "%.9g" etc. trunk gets this right. It is "%.11g"
and above that cause issues.

Reply via email to