]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-ascii.c
Don't run past the snapshot length when doing hex/ASCII dumps.
[tcpdump] / print-ascii.c
index fa8793cb0641b6227506d9875f78a77593576dc8..65815bde54bb676bbbd4870fc476f1137f379ad0 100644 (file)
@@ -59,8 +59,12 @@ static const char rcsid[] _U_ =
 void
 ascii_print(register const u_char *cp, register u_int length)
 {
+       u_int caplength;
        register int s;
 
+       caplength = (snapend >= cp) ? snapend - cp : 0;
+       if (length > caplength)
+               length = caplength;
        putchar('\n');
        while (length > 0) {
                s = *cp++;
@@ -77,12 +81,16 @@ void
 hex_and_ascii_print_with_offset(register const char *ident,
     register const u_char *cp, register u_int length, register u_int oset)
 {
+       u_int caplength;
        register u_int i;
        register int s1, s2;
        register int nshorts;
        char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
        char asciistuff[ASCII_LINELENGTH+1], *asp;
 
+       caplength = (snapend >= cp) ? snapend - cp : 0;
+       if (length > caplength)
+               length = caplength;
        nshorts = length / sizeof(u_short);
        i = 0;
        hsp = hexstuff; asp = asciistuff;
@@ -134,9 +142,13 @@ void
 hex_print_with_offset(register const char *ident, register const u_char *cp, register u_int length,
                      register u_int oset)
 {
+       u_int caplength;
        register u_int i, s;
        register int nshorts;
 
+       caplength = (snapend >= cp) ? snapend - cp : 0;
+       if (length > caplength)
+               length = caplength;
        nshorts = (u_int) length / sizeof(u_short);
        i = 0;
        while (--nshorts >= 0) {