Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some error checking to pcap_dump(). #494

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions sf-pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,39 @@ pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
struct pcap_sf_pkthdr sf_hdr;

f = (FILE *)user;
/*
* If the output file handle is in an error state, don't write
* anything.
*
* While in principle a file handle can return from an error state
* to a normal state (for example if a disk that is full has space
* freed), we have possibly left a broken file already, and won't
* be able to clean it up. The safest option is to do nothing.
*
* Note that if we could guarantee that fwrite() was atomic we
* might be able to insure that we don't produce a corrupted file,
* but the standard defines fwrite() as a series of fputc() calls,
* so we really have no insurance that things are not fubared.
*
* http://pubs.opengroup.org/onlinepubs/009695399/functions/fwrite.html
*/
if (ferror(f))
return;
sf_hdr.ts.tv_sec = h->ts.tv_sec;
sf_hdr.ts.tv_usec = h->ts.tv_usec;
sf_hdr.caplen = h->caplen;
sf_hdr.len = h->len;
/* XXX we should check the return status */
(void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
(void)fwrite(sp, h->caplen, 1, f);
/*
* We only write the packet if we can write the header properly.
*
* This doesn't prevent us from having corrupted output, and if we
* for some reason don't get a complete write we don't have any
* way to set ferror() to prevent future writes from being
* attempted, but it is better than nothing.
*/
if (fwrite(&sf_hdr, sizeof(sf_hdr), 1, f) == 1) {
(void)fwrite(sp, h->caplen, 1, f);
}
}

static pcap_dumper_t *
Expand Down