@@ -40,6 +40,9 @@ const URLENCODE_STRICT_SET: &AsciiSet = &NON_ALPHANUMERIC
40
40
// Same as URLENCODE_STRICT_SET, but preserves forward slashes for encoding paths
41
41
const URLENCODE_SET : & AsciiSet = & URLENCODE_STRICT_SET . remove ( b'/' ) ;
42
42
43
+ // MAX_LEN is maximum allowed length for filters.
44
+ const MAX_LEN : usize = 10_000 ;
45
+
43
46
/// Marks a string (or other `Display` type) as safe
44
47
///
45
48
/// Use this is you want to allow markup in an expression, or if you know
@@ -374,6 +377,9 @@ impl<S: fmt::Display> fmt::Display for TruncateFilter<S> {
374
377
#[ inline]
375
378
pub fn indent ( s : impl ToString , width : usize ) -> Result < impl fmt:: Display , Infallible > {
376
379
fn indent ( s : String , width : usize ) -> Result < String , Infallible > {
380
+ if width >= MAX_LEN {
381
+ return Ok ( s) ;
382
+ }
377
383
let mut indented = String :: new ( ) ;
378
384
for ( i, c) in s. char_indices ( ) {
379
385
indented. push ( c) ;
@@ -483,7 +489,7 @@ pub fn capitalize(s: impl ToString) -> Result<impl fmt::Display, Infallible> {
483
489
pub fn center ( src : impl ToString , dst_len : usize ) -> Result < impl fmt:: Display , Infallible > {
484
490
fn center ( src : String , dst_len : usize ) -> Result < String , Infallible > {
485
491
let len = src. len ( ) ;
486
- if dst_len <= len {
492
+ if dst_len <= len || dst_len >= MAX_LEN {
487
493
Ok ( src)
488
494
} else {
489
495
let diff = dst_len - len;
@@ -704,6 +710,10 @@ mod tests {
704
710
indent( "hello\n foo\n bar" , 4 ) . unwrap( ) . to_string( ) ,
705
711
"hello\n foo\n bar"
706
712
) ;
713
+ assert_eq ! (
714
+ indent( "hello" , 267_332_238_858 ) . unwrap( ) . to_string( ) ,
715
+ "hello"
716
+ ) ;
707
717
}
708
718
709
719
#[ cfg( feature = "num-traits" ) ]
@@ -806,6 +816,10 @@ mod tests {
806
816
center( "foo bar" , 8 ) . unwrap( ) . to_string( ) ,
807
817
"foo bar " . to_string( )
808
818
) ;
819
+ assert_eq ! (
820
+ center( "foo" , 111_669_149_696 ) . unwrap( ) . to_string( ) ,
821
+ "foo" . to_string( )
822
+ ) ;
809
823
}
810
824
811
825
#[ test]
0 commit comments