sprintf
adds extra buffer slots when precision arg truncates string slices
#6137
Milestone
sprintf
adds extra buffer slots when precision arg truncates string slices
#6137
Magisus commentedMar 24, 2020
Environment Information
Expected Behavior
The
sprintf
function should correctly truncate string slices when a precision is specified that is smaller than the slice length.Actual Behavior
The
sprintf
function misbehaves when using a precision number with a string argument, if that string is a slice of a larger string. It will add the wrong number of spaces to the buffer and then fill it with extra characters from the larger string, or leave the buffer spaces uninitialized if there are too few characters in the source string to fill it.Script to repro:
I was able to fix this bug by adding this simple patch at https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/util/Sprintf.java#L622:
The text was updated successfully, but these errors were encountered: