get_dhcp_opt() didx argument should be passed as a reference rather

than relying on the caller re-assigning to didx.  The previous
didx += get_dhcp_opt(...) was wrong and should have used =.
This commit is contained in:
Nicholas J. Kain 2014-05-10 21:32:15 -04:00
parent 99e21004ea
commit 4a083d3367

View File

@ -74,8 +74,8 @@ static int overload_value(const struct dhcpmsg *packet)
return ol; // ol == 0 return ol; // ol == 0
} }
static ssize_t do_get_dhcp_opt(const uint8_t *sbuf, ssize_t slen, uint8_t code, static void do_get_dhcp_opt(const uint8_t *sbuf, ssize_t slen, uint8_t code,
uint8_t *dbuf, ssize_t dlen, ssize_t didx) uint8_t *dbuf, ssize_t dlen, ssize_t *didx)
{ {
ssize_t i = 0; ssize_t i = 0;
while (i < slen) { while (i < slen) {
@ -89,30 +89,30 @@ static ssize_t do_get_dhcp_opt(const uint8_t *sbuf, ssize_t slen, uint8_t code,
break; break;
ssize_t soptsiz = sbuf[i+1]; ssize_t soptsiz = sbuf[i+1];
if (sbuf[i] == code) { if (sbuf[i] == code) {
if (dlen - didx < soptsiz) if (dlen - *didx < soptsiz)
return didx; return;
if (slen - i - 2 < soptsiz) if (slen - i - 2 < soptsiz)
return didx; return;
memcpy(dbuf+didx, sbuf+i+2, soptsiz); memcpy(dbuf + *didx, sbuf+i+2, soptsiz);
didx += soptsiz; *didx += soptsiz;
} }
i += soptsiz + 2; i += soptsiz + 2;
} }
return didx;
} }
ssize_t get_dhcp_opt(const struct dhcpmsg *packet, uint8_t code, uint8_t *dbuf, ssize_t get_dhcp_opt(const struct dhcpmsg *packet, uint8_t code, uint8_t *dbuf,
ssize_t dlen) ssize_t dlen)
{ {
int ol = overload_value(packet); int ol = overload_value(packet);
ssize_t didx = do_get_dhcp_opt(packet->options, sizeof packet->options, ssize_t didx = 0;
code, dbuf, dlen, 0); do_get_dhcp_opt(packet->options, sizeof packet->options, code,
dbuf, dlen, &didx);
if (ol & 1) if (ol & 1)
didx += do_get_dhcp_opt(packet->file, sizeof packet->file, code, do_get_dhcp_opt(packet->file, sizeof packet->file, code,
dbuf, dlen, didx); dbuf, dlen, &didx);
if (ol & 2) if (ol & 2)
didx += do_get_dhcp_opt(packet->sname, sizeof packet->sname, code, do_get_dhcp_opt(packet->sname, sizeof packet->sname, code,
dbuf, dlen, didx); dbuf, dlen, &didx);
return didx; return didx;
} }