On Thu, 5 Nov 2020 19:41:25 GMT, Claes Redestad <redes...@openjdk.org> wrote:
>> ... so the problem would be that the compiler does not believe us that >> millisecs will be always <1000. And there is no way to truncate output for >> numerical format specifiers. >> >> One solution could be to first print the millisecs to a buffer large enough >> to hold MAX_INT. And then print that buffer as a string, which can be >> truncated with precision. >> >> char tmp[10 + 1]; >> snprintf(tmp, sizeof(tmp), "%d", millisecs); >> snprintf(tbuf, ltbuf, "%s.%.3s %s", timestamp_date_time, tmp, >> timestamp_timezone); >> >> That may be enough to shut the compiler up. > >> ... so the problem would be that the compiler does not believe us that >> millisecs will be always <1000. And there is no way to truncate output for >> numerical format specifiers. > > Interesting, and a reasonable explanation. Odd this doesn't get caught by the > 64-bit build > >> >> One solution could be to first print the millisecs to a buffer large enough >> to hold MAX_INT. And then print that buffer as a string, which can be >> truncated with precision. >> >> ``` >> char tmp[10 + 1]; >> snprintf(tmp, sizeof(tmp), "%d", millisecs); >> snprintf(tbuf, ltbuf, "%s.%.3s %s", timestamp_date_time, tmp, >> timestamp_timezone); >> ``` >> >> That may be enough to shut the compiler up. > > It'd be interesting to see if statically limiting the value printed to be < > 1000 would also convince the compiler, e.g. > `snprintf(tbuf, ltbuf, "%s.%.3d %s", timestamp_date_time, millisecs % 1000, > timestamp_timezone);` > > (I'm not sure a perfect solution is worth our while here, but seeing we've > gotten this far down the rabbit hole..) I didn't see your comment @cl4es and I tested out Thomas's solution and that compiles and seems to make sense. ------------- PR: https://git.openjdk.java.net/jdk/pull/1067