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

Remove unnecessary explicit buffer nul-termination after using os::snprintf

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • 26
    • 26
    • hotspot

      Historically snprintf did not guarantee nul-termination in case of truncation so we have code like:

      snprintf(buf, sizeof(buf), "%s", name);
      buf[sizeof(buf) - 1] = '\0';

      but this explicit nul-termination is no longer necessary.

      Additionally we have code that unnecessarily reserves space for an extra nul-terminator e.g [1].

        if (ebuf != nullptr && ebuflen > 0) {
            os::snprintf_checked(ebuf, ebuflen - 1, "%s", error_report);
          }

      These should also be cleaned up.

      [1] https://github.com/openjdk/jdk/blob/38003a227a55dbd6adb89dcb10dc619f08bb0187/src/hotspot/os/bsd/os_bsd.cpp#L2493

            phubner Paul Hübner
            dholmes David Holmes
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: