8347707: Standardise the use of os::snprintf and os::snprintf_checked #26470
+192
−182
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
EDIT: I've just realized that the
[[nodiscard]]
attribute is not currently permitted. So I may have to revise this aspect of the changes.This is a proposal to standardize on the use of os::snprintf and os::snprintf_checked across the hotspot code base, and to disallow use of the C library variants. (It does not touch use of jio_printf at all.)
From: https://bugs.openjdk.org/browse/JDK-8347707
The platform
snprintf/vsnprintf
returns -1 on error, else if the buffer is large enough returns the number of bytes written (excluding the null byte), else (buffer is too small) the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated.To provide a consistent approach to error handling and truncation management, we provide
os::xxx
wrapper functions as described below and forbid the use of the library::vsnprintf
and::snprintf
.The potential errors are, generally speaking, not something we should encounter in our own well-written code:
int
).As these should simply never occur, we handle the checks for -1 at the lowest-level (
os::vsnprintf
) with an assertion, and accompanying precondition assertions.The potential clients of this API then fall into a number of camps:
For these clients we have
void os::snprintf_checked
- which returns nothing and asserts on truncation.snprintf
where you advance the buffer pointer based on previous writes), but again for whom truncation should never happen.For these clients we have
os::snprintf
, but they have to add their own assertion for no truncation.These clients also use
os::snprintf_checked
. The truncation assertion can be useful for guiding buffer sizing decisions, but in product mode truncation is not an error.These clients are also directed to use
os::snprintf
.In summary we provide the following API:
[[nodiscard]] int os::vsnprintf
is the building block for the other methods, it:[[nodiscard[]]
so that callers cannot ignore the return value (they can explicitly cast tovoid
to indicate they don't need it)void os::snprintf_checked
[[nodiscard]] int os::snprintf
os::vnsprintf
so asserts on errorsIn terms of the effects on the existing code we:
::snprintf
/os::snprintf
that ignore the return value and ensure the buffer is large enough to useos::snprintf_checked
os::snprintf
.::snprintf
/os::snprintf
that use the return value to useos::snprintf
, plus any additional assertions neededos::snprintf_checked
that do use the return value, to useos::snprintf
with their own assertions addedos::vnsprintf
are adjusted as neededThe PR is comprising multiple dependent commits so that you can view things in stages. There are 46 modified files. The bulk of the changes replace calls to
snprintf
/os::snprintf
with calls toos::snprintf_checked
.Progress
Issue
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26470/head:pull/26470
$ git checkout pull/26470
Update a local copy of the PR:
$ git checkout pull/26470
$ git pull https://git.openjdk.org/jdk.git pull/26470/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 26470
View PR using the GUI difftool:
$ git pr show -t 26470
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26470.diff
Using Webrev
Link to Webrev Comment