Skip to content

Commit adbd8a4

Browse files
appender: fix max_files integer underflow when set to zero (#3348)
## Motivation When `max_files` set to zero, [rolling.rs:695](https://github.com/tokio-rs/tracing/blob/c01d4fd9def2fb061669a310598095c789ca0a32/tracing-appender/src/rolling.rs#L695) will cause integer underflow and panicking in debug build. In my opinion the API should prevent this from happening as "zero" is a valid `usize` value to pass in. ## Solution Currently, in release builds, if `max_files` is set to zero, it behaves just like `u64::MAX` on `x86_64` platforms. Therefore, I decided to simply make `zero == None`, which should align with the existing behavior but without the panicking. I believe this can be considered as "non-breaking".
1 parent 2a8b040 commit adbd8a4

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

tracing-appender/src/rolling/builder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl Builder {
195195
/// the maximum, so if you need to retain `m` log files, specify a max of
196196
/// `m + 1`.
197197
///
198-
/// If no value is supplied, the `RollingAppender` will not remove any files.
198+
/// If `0` is supplied, the `RollingAppender` will not remove any files.
199199
///
200200
/// Files are considered candidates for deletion based on the following
201201
/// criteria:
@@ -232,7 +232,8 @@ impl Builder {
232232
#[must_use]
233233
pub fn max_log_files(self, n: usize) -> Self {
234234
Self {
235-
max_files: Some(n),
235+
// Setting `n` to 0 will disable the max files (effectively make it infinite).
236+
max_files: Some(n).filter(|&n| n > 0),
236237
..self
237238
}
238239
}

0 commit comments

Comments
 (0)