Skip to content

Commit

Permalink
Limit the maximum recursion depth
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Atamanov <data-man@users.noreply.github.com>
  • Loading branch information
LemonBoy and data-man committed Oct 30, 2020
1 parent dc058e6 commit bc605c0
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/std/fmt.zig
Expand Up @@ -480,13 +480,16 @@ pub fn formatType(
return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) });
},
.Slice => {
if (max_depth == 0) {
return writer.writeAll("[ ... ]");
}
if (ptr_info.child == u8) {
return formatText(value, fmt, options, writer);
}

try writer.writeAll("[");
for (value) |elem, i| {
try formatType(elem, fmt, options, writer, max_depth);
try formatType(elem, fmt, options, writer, max_depth - 1);
if (i != value.len - 1) {
try writer.writeAll(", ");
}
Expand Down

0 comments on commit bc605c0

Please sign in to comment.