diff --git a/dhcp.c b/dhcp.c index b350fe9..460c5fc 100644 --- a/dhcp.c +++ b/dhcp.c @@ -383,13 +383,15 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, * to just determine output length. */ static unsigned int decode_search (const unsigned char *p, int len, char *out) { + const char *start; const unsigned char *r, *q = p; unsigned int count = 0, l, hops; + start = out; while (q - p < len) { r = NULL; hops = 0; - while ((l = *q++)) { + while ((l = *q++) && q - p < len ) { unsigned int label_type = l & 0xc0; if (label_type == 0x80 || label_type == 0x40) return 0; @@ -421,7 +423,7 @@ static unsigned int decode_search (const unsigned char *p, int len, char *out) } /* change last dot to space */ - if (out) + if (out && out != start ) *(out - 1) = ' '; if (r) @@ -430,7 +432,12 @@ static unsigned int decode_search (const unsigned char *p, int len, char *out) /* change last space to zero terminator */ if (out) - *(out - 1) = 0; + { + if ( out != start ) + { + *(out - 1) = 0; + } + } return count; } @@ -848,9 +855,9 @@ parse_start: case DHCP_DNSSEARCH: MIN_LENGTH (1); - free (dhcp->dnssearch); len = decode_search (p, length, NULL); if (len > 0) { + free (dhcp->dnssearch); dhcp->dnssearch = xmalloc (len); decode_search (p, length, dhcp->dnssearch);