authorgravatar for 3405586+schmee@users.noreply.github.comJohn Schmidt <3405586+schmee@users.noreply.github.com> 2022-01-29 12:25:25+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-02 22:01:47-07:00
log5762b6d2183581990c168099f277b77be55addd4
tree279a5296ae264f9fd21a0f6993b6af822172491f
parent8b5e4af78e1a4a75cf6d9225246bf3e8a195282d

std.fmt: fix out-of-bounds array write in float printing

This commit fixes an out of bounds write that can occur when formatting certain float values. The write messes up the stack and causes incorrect results, segfaults, or nothing at all, depending on the optimization mode used. The `errol` function writes the digits of the float into `buffer` starting from index 1, leaving index 0 untouched, and returns `buffer[1..]` and the exponent. This is because `roundToPrecision` relies on index 0 being unused in case the rounding adds a digit (e.g rounding 999.99 to 1000.00). When this happens, pointer arithmetic is used [here](https://github.com/ziglang/zig/blob/0e6d2184cacf2dd1fad7508b2f9ae99d78763148/lib/std/fmt/errol.zig#L61-L65) to access index 0 and put the ones digit in the right place. However, `errol3u` contains two special cases: `errolInt` and `errolFixed`, which return from the function early. For these two special cases index 0 was never reserved, and the return value contains `buffer` instead of `buffer[1..]`. This causes the pointer arithmetic in `roundToPrecision` to write out of bounds, which in the case of `std.fmt.formatFloatDecimal` messes up the stack and causes undefined behavior. The fix is to move the slicing of `buffer` to `buffer[1..]` from `errol3u` to `errol` so that both the default and the special cases operate on the sliced buffer.

2 files changed, 7 insertions(+), 7 deletions(-)

lib/std/fmt.zig+1
......@@ -2259,6 +2259,7 @@ test "float.decimal" {
22592259 try expectFmt("f64: 0.00030000", "f64: {d:.8}", .{@as(f64, 0.0003)});
22602260 try expectFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 1.40130e-45)});
22612261 try expectFmt("f64: 0.00000", "f64: {d:.5}", .{@as(f64, 9.999960e-40)});
2262 try expectFmt("f64: 10000000000000.00", "f64: {d:.2}", .{@as(f64, 9999999999999.999)});
22622263}
22632264
22642265test "float.libc.sanity" {
lib/std/fmt/errol.zig+6-7
......@@ -92,7 +92,10 @@ pub fn errol3(value: f64, buffer: []u8) FloatDecimal {
9292 };
9393 }
9494
95 return errol3u(value, buffer);
95 // We generate digits starting at index 1. If rounding a buffer later then it may be
96 // required to generate a preceding digit in some cases (9.999) in which case we use
97 // the 0-index for this extra digit.
98 return errol3u(value, buffer[1..]);
9699}
97100
98101/// Uncorrected Errol3 double to ASCII conversion.
......@@ -162,11 +165,7 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
162165 }
163166
164167 // digit generation
165
166 // We generate digits starting at index 1. If rounding a buffer later then it may be
167 // required to generate a preceding digit in some cases (9.999) in which case we use
168 // the 0-index for this extra digit.
169 var buf_index: usize = 1;
168 var buf_index: usize = 0;
170169 while (true) {
171170 var hdig = @floatToInt(u8, math.floor(high.val));
172171 if ((high.val == @intToFloat(f64, hdig)) and (high.off < 0)) hdig -= 1;
......@@ -192,7 +191,7 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
192191 buf_index += 1;
193192
194193 return FloatDecimal{
195 .digits = buffer[1..buf_index],
194 .digits = buffer[0..buf_index],
196195 .exp = exp,
197196 };
198197}