@@ -87,11 +87,15 @@ class ListField(FieldBase[List[_TL]], Generic[_TL]):
8787 A list whose octets hold fewer items than ``count`` ended with the data
8888 before the rest, and reports them as :attr:`shortfall`, so that
8989 :meth:`Schema.unpack <pcapkit.protocols.schema.Schema.unpack>`
90- records the cut; the items absent are not made up.
90+ records the cut; the items absent are not made up. A schema item it counts
91+ is read with ``__data_end__`` in its packet, since its octets end where the
92+ data does, so an item the data ends inside records the cut as its short
93+ read rather than warn that its fields run past the length it was given.
9194
9295 .. versionchanged:: 1.6.0
9396
94- Takes ``count`` (:issue:`2711`).
97+ Takes ``count`` (:issue:`2711`), and reads a schema item it counts as
98+ ending with the data (:issue:`2749`).
9599
96100 """
97101
@@ -209,15 +213,18 @@ def unpack(self, buffer: 'bytes | IO[bytes]', packet: 'dict[str, Any]') -> 'byte
209213 consumes nothing from ``buffer``, or if schema items short of the
210214 field's length cannot be zero-filled.
211215
212- When ``buffer`` falls short of the field's length, schema items are read
213- from its octets zero-filled to that length , as
216+ When ``buffer`` falls short of the field's length, the schema item it
217+ ends inside is read as though its missing octets were zeros , as
214218 :meth:`FieldBase.unpack <pcapkit.corekit.fields.field.FieldBase.unpack>`
215- reads any other field, so the items cover the length declared.
219+ reads any other field, and the list ends with that item: none is read
220+ from the zeros past it, which hold no octet of the data.
216221
217222 .. versionchanged:: 1.6.0
218223
219- Schema items short of the field's length are zero-filled to it, where
220- an item past the data was refused as consuming nothing (:issue:`2686`).
224+ A list of schema items the data ends inside ends with the data, the
225+ item it ends inside read with its missing octets as zeros, where an
226+ item past the data was refused as consuming nothing (:issue:`2686`,
227+ :issue:`2756`).
221228
222229 """
223230 length = self ._length
@@ -263,16 +270,20 @@ def unpack(self, buffer: 'bytes | IO[bytes]', packet: 'dict[str, Any]') -> 'byte
263270 file .seek (start , io .SEEK_SET )
264271 if present < length :
265272 with _zero_filled_items (self .name , length , file .read ()) as filled :
266- return self ._unpack_items (io . BytesIO ( filled ) , packet , is_schema )
273+ return self ._unpack_items (filled , packet , is_schema , present )
267274 return self ._unpack_items (file , packet , is_schema )
268275
269- def _unpack_items (self , file : 'IO[bytes]' , packet : 'dict[str, Any]' , is_schema : 'bool' ) -> 'list[_TL]' :
276+ def _unpack_items (self , file : 'IO[bytes]' , packet : 'dict[str, Any]' , is_schema : 'bool' ,
277+ present : 'int' = - 1 ) -> 'list[_TL]' :
270278 """Unpack the field's items from ``file``.
271279
272280 Args:
273281 file: Stream the items are read from.
274282 packet: Packet data.
275283 is_schema: Whether the items are schemas.
284+ present: Octets of ``file`` the data holds, past which it holds the
285+ zeros :meth:`unpack` filled it with; negative where it holds
286+ no zeros.
276287
277288 Returns:
278289 Unpacked items.
@@ -289,8 +300,9 @@ def _unpack_items(self, file: 'IO[bytes]', packet: 'dict[str, Any]', is_schema:
289300 # which is what it read, but the schema branch sizes it by ``len(data)``,
290301 # which is only what the schema *recorded*. A schema that reads nothing
291302 # records nothing, so ``length -= len(data)`` makes no progress and the
292- # loop spins forever. The zero-fill in ``unpack`` gives every item octets,
293- # but an item schema may still consume none of them. Remembering where the
303+ # loop spins forever. The zero-fill in ``unpack`` gives the item the data
304+ # ends inside octets to read, and every item before it reads octets of the
305+ # data, but an item schema may still consume none of them. Remembering where the
294306 # previous item ended is what bounds the iteration count, since it does
295307 # not depend on what the schema reports. Cf. #431, which is the same
296308 # defect in the ``OptionField`` subclass.
@@ -304,10 +316,17 @@ def _unpack_items(self, file: 'IO[bytes]', packet: 'dict[str, Any]', is_schema:
304316
305317 temp = [] # type: list[_TL]
306318 while length > 0 :
319+ # NOTE: An item starting past the octets present would be made of the zero-fill alone,
320+ # one the capture never held, so the list ends with the data (#2756).
321+ if 0 <= present <= offset - start :
322+ break
307323 field = item_type (packet )
308324
309325 if is_schema :
310- data = cast ('SchemaField' , field ).unpack (file , packet )
326+ # NOTE: A counted list runs to the end of the data, so an item it counts
327+ # running past the octets left is cut, not overrunning a length (#2749).
328+ context = dict (packet , __data_end__ = True ) if len (temp ) < self ._count else packet
329+ data = cast ('SchemaField' , field ).unpack (file , context )
311330
312331 end = file .tell ()
313332 if end <= offset :
0 commit comments