RFR: JDK-8296995: ostream should handle snprintf(3) errors in release builds

Thomas Stuefe stuefe at openjdk.org
Tue Nov 15 14:11:29 UTC 2022


Small fix.

All streams in ostream.hpp end up using `outputStream::do_vsnprintf()`, which uses `os::snprintf()`, which uses `::vsnprintf()`.

The latter can fail, returning -1, e.g. in case of an encoding error. In that case, we assert in debug.

In release builds this situation gets misdiagnosed as a buffer overflow because we cast the signedness of the result away and compare it with the output buffer length (see `outputStream::do_vsnprintf()`).

The output buffer will be zero-terminated at its end by `os::snprintf()`, but that leaves the rest of the output buffer undefined. The libc may or may not have written parts of the formatted output into it, and may or may not have zero-terminated it. We then proceed to write whatever happens to be in that buffer to the stream sink (see `outputStream::do_vsnprintf_and_write_with_automatic_buffer()` resp. `outputStream::do_vsnprintf_and_write_with_scratch_buffer()`). 

---

Patch fixes this : in release builds, we now write nothing. A fatal error would be not good here, since I am not sure this cannot be produced via user input. I considered printing a clear marker, e.g. "ENCODING ERROR" instead, and I'm open to suggestions.

-------------

Commit messages:
 - JDK-8296995-ostream-handle-sprintf-errors

Changes: https://git.openjdk.org/jdk/pull/11160/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=11160&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8296995
  Stats: 27 lines in 2 files changed: 27 ins; 0 del; 0 mod
  Patch: https://git.openjdk.org/jdk/pull/11160.diff
  Fetch: git fetch https://git.openjdk.org/jdk pull/11160/head:pull/11160

PR: https://git.openjdk.org/jdk/pull/11160


More information about the hotspot-dev mailing list