Assigning it to p->snapshot, and then checking whether the result is
negative, should work in practice, but it gets unsigned-behavior
warnings. Test beforehand whether it's valid, and only assign it to
p->snapshot if it is.
This should address the pcap.c part of GitHub issue
the-tcpdump-group/tcpdump#785.
p->swapped = swapped;
p->version_major = hdr.version_major;
p->version_minor = hdr.version_minor;
- p->snapshot = hdr.snaplen;
- if (p->snapshot <= 0) {
+ if (hdr.snaplen == 0 || hdr.snaplen > INT_MAX) {
/*
* Bogus snapshot length; use the maximum for this
* link-layer type as a fallback.
* unsigned int.
*/
p->snapshot = max_snaplen_for_dlt(hdr.linktype);
- }
+ } else
+ p->snapshot = hdr.snaplen;
p->linktype = linktype_to_dlt(LT_LINKTYPE(hdr.linktype));
p->linktype_ext = LT_LINKTYPE_EXT(hdr.linktype);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <limits.h> /* for INT_MAX */
#include "pcap-int.h"
}
done:
- p->snapshot = idbp->snaplen;
- if (p->snapshot <= 0) {
+ if (idbp->snaplen == 0 || idbp->snaplen > INT_MAX) {
/*
* Bogus snapshot length; use the maximum for this
* link-layer type as a fallback.
* unsigned int.
*/
p->snapshot = max_snaplen_for_dlt(idbp->linktype);
- }
+ } else
+ p->snapshot = idbp->snaplen;
p->linktype = linktype_to_dlt(idbp->linktype);
p->linktype_ext = 0;