From: Amos Jeffries Date: Mon, 29 Jun 2015 14:32:49 +0000 (-0700) Subject: Cleanup HTCP packet construction X-Git-Tag: merge-candidate-3-v1~65 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e67d158e229332a3a6efe7065f4ad76397b6b088;p=thirdparty%2Fsquid.git Cleanup HTCP packet construction Fixes non-zero bits in packet reserved fields. Detected by Coverity Scan. Issue 1256168 Also fixes some bad formatting. --- diff --git a/src/htcp.cc b/src/htcp.cc index 7a762f9745..23f552bd72 100644 --- a/src/htcp.cc +++ b/src/htcp.cc @@ -93,31 +93,21 @@ struct _htcpDataHeader { uint16_t length; #if WORDS_BIGENDIAN -uint8_t opcode: - 4; -uint8_t response: - 4; + uint8_t opcode:4; + uint8_t response:4; #else -uint8_t response: - 4; -uint8_t opcode: - 4; + uint8_t response:4; + uint8_t opcode:4; #endif #if WORDS_BIGENDIAN -uint8_t reserved: - 6; -uint8_t F1: - 1; -uint8_t RR: - 1; + uint8_t reserved:6; + uint8_t F1:1; + uint8_t RR:1; #else -uint8_t RR: - 1; -uint8_t F1: - 1; -uint8_t reserved: - 6; + uint8_t RR:1; + uint8_t F1:1; + uint8_t reserved:6; #endif uint32_t msg_id; @@ -499,7 +489,6 @@ htcpBuildData(char *buf, size_t buflen, htcpStuff * stuff) ssize_t off = 0; ssize_t op_data_sz; size_t hdr_sz = sizeof(htcpDataHeader); - htcpDataHeader hdr; if (buflen < hdr_sz) return -1; @@ -515,33 +504,25 @@ htcpBuildData(char *buf, size_t buflen, htcpStuff * stuff) debugs(31, 3, "htcpBuildData: hdr.length = " << off); - hdr.length = (uint16_t) off; - - hdr.opcode = stuff->op; - - hdr.response = stuff->response; - - hdr.RR = stuff->rr; - - hdr.F1 = stuff->f1; - - hdr.msg_id = stuff->msg_id; - - /* convert multi-byte fields */ - hdr.length = htons(hdr.length); - - hdr.msg_id = htonl(hdr.msg_id); - if (!old_squid_format) { + htcpDataHeader hdr; + memset(&hdr, 0, sizeof(hdr)); + /* convert multi-byte fields */ + hdr.msg_id = htonl(stuff->msg_id); + hdr.length = htons(static_cast(off)); + hdr.opcode = stuff->op; + hdr.response = stuff->response; + hdr.RR = stuff->rr; + hdr.F1 = stuff->f1; memcpy(buf, &hdr, hdr_sz); } else { htcpDataHeaderSquid hdrSquid; memset(&hdrSquid, 0, sizeof(hdrSquid)); - hdrSquid.length = hdr.length; - hdrSquid.opcode = hdr.opcode; - hdrSquid.response = hdr.response; - hdrSquid.F1 = hdr.F1; - hdrSquid.RR = hdr.RR; + hdrSquid.length = htons(static_cast(off)); + hdrSquid.opcode = stuff->op; + hdrSquid.response = stuff->response; + hdrSquid.F1 = stuff->f1; + hdrSquid.RR = stuff->rr; memcpy(buf, &hdrSquid, hdr_sz); }