Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8289512

Fix GCC 12 warnings for adlc output_c.cpp



    • b05



        GCC 12 warns about output buffer size:

        /shared/projects/openjdk/jdk-jdk/source/src/hotspot/share/adlc/output_c.cpp: In function 'int pipeline_res_mask_initializer(FILE*, PipelineForm*, NameList&, NameList&, PipeClassForm*)':
        /shared/projects/openjdk/jdk-jdk/source/src/hotspot/share/adlc/output_c.cpp:529:36: error: '%*d' directive writing between 1 and 2147483647 bytes into a region of size between 0 and 2147483637 [-Werror=format-overflow=]
          529 | sprintf(args, "0x%0*x, 0x%0*x, %*d",
              | ^~~

        GCC complains that the output buffer for sprintf is too short. We allocate a buffer fitting the fixed parts of the format string, the terminating zero, and the variable widths, then print into it:

            char* args = new char [9 + 2*masklen + maskdigit];

            sprintf(args, "0x%0*x, 0x%0*x, %*d",
              masklen, resources_used,
              masklen, resources_used_exclusively,
              maskdigit, element_count);

        masklen is a constant and known to the compiler. maskdigit is not. For all the compiler knows, it could be 0. Format specified is "%*s", which we feed maskdigit as width, element_count as argument to print.

        If maskdigit is too small, sprintf will not truncate. The result is that for all the compiler knows, the output array could be 10 characters too small (since element_count is unsigned 32bit, 10 digits).

        There are similar errors like that:

        /shared/projects/openjdk/jdk-jdk/source/src/hotspot/share/adlc/output_c.cpp: In function 'void check_peepconstraints(FILE*, FormDict&, PeepMatch*, PeepConstraint*)':
        /shared/projects/openjdk/jdk-jdk/source/src/hotspot/share/adlc/output_c.cpp:1171:42: error: '_idx' directive writing 4 bytes into a region of size between 2 and 12 [-Werror=format-overflow=]
         1171 | sprintf(left_reg_index,",inst%d_idx%d", (int)left_index, left_op_index);
        /shared/projects/openjdk/jdk-jdk/source/src/hotspot/share/adlc/output_c.cpp:1184:45: error: '_idx' directive writing 4 bytes into a region of size between 2 and 12 [-Werror=format-overflow=]
         1184 | sprintf(right_reg_index,",inst%d_idx%d", (int)right_index, right_op_index);

        these are a bit simpler, since they don't have variable sized widths. The compiler just complains about the buffer. Buffer does not allow the parameters to be beyond 9999, which is asserted, but the GCC does not understand this.


          Issue Links



                stuefe Thomas Stuefe
                stuefe Thomas Stuefe
                0 Vote for this issue
                7 Start watching this issue