@@ -106,54 +106,33 @@ struct MaxLengthCheck<'a, INPUT> {
106106 max_length : Option < usize > ,
107107 field_type : & ' a str ,
108108 input : & ' a INPUT ,
109- known_input_length : usize ,
109+ actual_length : Option < usize > ,
110110}
111111
112112impl < ' a , INPUT : Input < ' a > > MaxLengthCheck < ' a , INPUT > {
113- fn new ( max_length : Option < usize > , field_type : & ' a str , input : & ' a INPUT , known_input_length : usize ) -> Self {
113+ fn new ( max_length : Option < usize > , field_type : & ' a str , input : & ' a INPUT , actual_length : Option < usize > ) -> Self {
114114 Self {
115115 current_length : 0 ,
116116 max_length,
117117 field_type,
118118 input,
119- known_input_length ,
119+ actual_length ,
120120 }
121121 }
122122
123123 fn incr ( & mut self ) -> ValResult < ' a , ( ) > {
124- match self . max_length {
125- Some ( max_length) => {
126- self . current_length += 1 ;
127- if self . current_length > max_length {
128- let biggest_length = if self . known_input_length > self . current_length {
129- self . known_input_length
130- } else {
131- self . current_length
132- } ;
133- return Err ( ValError :: new (
134- ErrorType :: TooLong {
135- field_type : self . field_type . to_string ( ) ,
136- max_length,
137- actual_length : biggest_length,
138- context : None ,
139- } ,
140- self . input ,
141- ) ) ;
142- }
143- }
144- None => {
145- self . current_length += 1 ;
146- if self . current_length > self . known_input_length {
147- return Err ( ValError :: new (
148- ErrorType :: TooLong {
149- field_type : self . field_type . to_string ( ) ,
150- max_length : self . known_input_length ,
151- actual_length : self . current_length ,
152- context : None ,
153- } ,
154- self . input ,
155- ) ) ;
156- }
124+ if let Some ( max_length) = self . max_length {
125+ self . current_length += 1 ;
126+ if self . current_length > max_length {
127+ return Err ( ValError :: new (
128+ ErrorType :: TooLong {
129+ field_type : self . field_type . to_string ( ) ,
130+ max_length,
131+ actual_length : self . actual_length ,
132+ context : None ,
133+ } ,
134+ self . input ,
135+ ) ) ;
157136 }
158137 }
159138 Ok ( ( ) )
@@ -255,13 +234,15 @@ fn validate_iter_to_set<'a, 's>(
255234 Ok ( item) => {
256235 set. build_add ( item) ?;
257236 if let Some ( max_length) = max_length {
258- let actual_length = set. build_len ( ) ;
259- if actual_length > max_length {
237+ if set. build_len ( ) > max_length {
260238 return Err ( ValError :: new (
261239 ErrorType :: TooLong {
262240 field_type : field_type. to_string ( ) ,
263241 max_length,
264- actual_length,
242+ // The logic here is that it doesn't matter how many elements the
243+ // input actually had; all we know is it had more than the allowed
244+ // number of deduplicated elements.
245+ actual_length : None ,
265246 context : None ,
266247 } ,
267248 input,
@@ -335,10 +316,9 @@ impl<'a> GenericIterable<'a> {
335316 validator : & ' s CombinedValidator ,
336317 state : & mut ValidationState ,
337318 ) -> ValResult < ' a , Vec < PyObject > > {
338- let capacity = self
339- . generic_len ( )
340- . unwrap_or_else ( || max_length. unwrap_or ( DEFAULT_CAPACITY ) ) ;
341- let max_length_check = MaxLengthCheck :: new ( max_length, field_type, input, capacity) ;
319+ let actual_length = self . generic_len ( ) ;
320+ let capacity = actual_length. unwrap_or ( DEFAULT_CAPACITY ) ;
321+ let max_length_check = MaxLengthCheck :: new ( max_length, field_type, input, actual_length) ;
342322
343323 macro_rules! validate {
344324 ( $iter: expr) => {
@@ -394,10 +374,8 @@ impl<'a> GenericIterable<'a> {
394374 field_type : & ' static str ,
395375 max_length : Option < usize > ,
396376 ) -> ValResult < ' a , Vec < PyObject > > {
397- let capacity = self
398- . generic_len ( )
399- . unwrap_or_else ( || max_length. unwrap_or ( DEFAULT_CAPACITY ) ) ;
400- let max_length_check = MaxLengthCheck :: new ( max_length, field_type, input, capacity) ;
377+ let actual_length = self . generic_len ( ) ;
378+ let max_length_check = MaxLengthCheck :: new ( max_length, field_type, input, actual_length) ;
401379
402380 match self {
403381 GenericIterable :: List ( collection) => {
0 commit comments