fix: sometime pcap_next returns 0 which lead to segment fault #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Meet a segmentation fault situation:
Thread 2 "boopkit" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff7cc2700 (LWP 936627)]
0x00000000003100d9 in xcap (v_dev_name=0x315f68 <cfg+12292>) at dpi.c:217
217 ether_type = ntohs(ep->ether_type);
(gdb) bt
#0 0x00000000003100d9 in xcap (v_dev_name=0x315f68 <cfg+12292>) at dpi.c:217
#1 0x00007ffff7ede609 in start_thread (arg=) at pthread_create.c:477
#2 0x00007ffff7e03133 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
(gdb) p ep
$1 = (struct ether_header *) 0x0
(gdb) p packet
$2 = (const u_char *) 0x0
According to man pcap_next():
pcap_next() returns a pointer to the packet data on success, and
returns NULL if an error occured, or if no packets were read from a
live capture (if, for example, they were discarded because they didn't
pass the packet filter, or if, on platforms that support a read timeout
that starts before any packets arrive, the timeout expires before any
packets arrive, or if the file descriptor for the capture device is in
non-blocking mode and no packets were available to be read), or if no
more packets are available in a ``savefile.'' Unfortunately, there is
no way to determine whether an error occured or not.
pcap_next() may returns NULL. So bybass it to avoid segmentation fault.