]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
HTCP: fix memory initialization errors
authorAmos Jeffries <squid3@treenet.co.nz>
Fri, 21 Nov 2014 18:09:06 +0000 (10:09 -0800)
committerAmos Jeffries <squid3@treenet.co.nz>
Fri, 21 Nov 2014 18:09:06 +0000 (10:09 -0800)
memset() used to initialize HTCP objects made sense when they were
structs. But now they are classes proper constructors need to be used
to avoid memset() erasing vtable and other important areas. It also
helps to reduce code and improve performance during init a tiny bit.

memset() errors found by Clang 3.5

src/htcp.cc

index 9519d702b85e1767178309828e402e421849cb65..1639f6db2fa27ac962f43d32faa92bcad1409fca 100644 (file)
@@ -47,8 +47,6 @@ typedef struct _htcpDataHeaderSquid htcpDataHeaderSquid;
 
 typedef struct _htcpAuthHeader htcpAuthHeader;
 
-typedef struct _htcpStuff htcpStuff;
-
 typedef struct _htcpDetail htcpDetail;
 
 struct _Countstr {
@@ -138,6 +136,18 @@ class htcpSpecifier : public StoreClient
     MEMPROXY_CLASS(htcpSpecifier);
 
 public:
+    htcpSpecifier() :
+         method(NULL),
+         uri(NULL),
+         version(NULL),
+         req_hdrs(NULL),
+         reqHdrsSz(0),
+         request(NULL),
+         checkHitRequest(NULL),
+         dhdr(NULL)
+    {}
+    // XXX: destructor?
+
     void created (StoreEntry *newEntry);
     void checkHit();
     void checkedHit(StoreEntry *e);
@@ -169,7 +179,20 @@ struct _htcpDetail {
     size_t cacheHdrsSz;
 };
 
-struct _htcpStuff {
+class htcpStuff
+{
+public:
+    htcpStuff(uint32_t id, int o, int r, int f) :
+        op(o),
+        rr(r),
+        f1(f),
+        response(0),
+        reason(0),
+        msg_id(id)
+    {
+        memset(&D, 0, sizeof(D));
+    }
+
     int op;
     int rr;
     int f1;
@@ -828,19 +851,15 @@ htcpAccessAllowed(acl_access * acl, htcpSpecifier * s, Ip::Address &from)
 static void
 htcpTstReply(htcpDataHeader * dhdr, StoreEntry * e, htcpSpecifier * spec, Ip::Address &from)
 {
-    htcpStuff stuff;
     static char pkt[8192];
     HttpHeader hdr(hoHtcpReply);
     MemBuf mb;
     Packer p;
     ssize_t pktlen;
-    memset(&stuff, '\0', sizeof(stuff));
-    stuff.op = HTCP_TST;
-    stuff.rr = RR_RESPONSE;
-    stuff.f1 = 0;
+
+    htcpStuff stuff(dhdr->msg_id, HTCP_TST, RR_RESPONSE, 0);
     stuff.response = e ? 0 : 1;
     debugs(31, 3, "htcpTstReply: response = " << stuff.response);
-    stuff.msg_id = dhdr->msg_id;
 
     if (spec) {
         mb.init();
@@ -924,7 +943,6 @@ static void
 
 htcpClrReply(htcpDataHeader * dhdr, int purgeSucceeded, Ip::Address &from)
 {
-    htcpStuff stuff;
     static char pkt[8192];
     ssize_t pktlen;
 
@@ -933,20 +951,12 @@ htcpClrReply(htcpDataHeader * dhdr, int purgeSucceeded, Ip::Address &from)
     if (dhdr->F1 == 0)
         return;
 
-    memset(&stuff, '\0', sizeof(stuff));
-
-    stuff.op = HTCP_CLR;
-
-    stuff.rr = RR_RESPONSE;
-
-    stuff.f1 = 0;
+    htcpStuff stuff(dhdr->msg_id, HTCP_CLR, RR_RESPONSE, 0);
 
     stuff.response = purgeSucceeded ? 0 : 2;
 
     debugs(31, 3, "htcpClrReply: response = " << stuff.response);
 
-    stuff.msg_id = dhdr->msg_id;
-
     pktlen = htcpBuildPacket(pkt, sizeof(pkt), &stuff);
 
     if (pktlen == 0) {
@@ -1533,7 +1543,6 @@ htcpQuery(StoreEntry * e, HttpRequest * req, CachePeer * p)
     static char pkt[8192];
     ssize_t pktlen;
     char vbuf[32];
-    htcpStuff stuff;
     HttpHeader hdr(hoRequest);
     Packer pa;
     MemBuf mb;
@@ -1546,11 +1555,8 @@ htcpQuery(StoreEntry * e, HttpRequest * req, CachePeer * p)
     memset(&flags, '\0', sizeof(flags));
     snprintf(vbuf, sizeof(vbuf), "%d/%d",
              req->http_ver.major, req->http_ver.minor);
-    stuff.op = HTCP_TST;
-    stuff.rr = RR_REQUEST;
-    stuff.f1 = 1;
-    stuff.response = 0;
-    stuff.msg_id = ++msg_id_counter;
+
+    htcpStuff stuff(++msg_id_counter, HTCP_TST, RR_REQUEST, 1);
     SBuf sb = req->method.image();
     stuff.S.method = sb.c_str();
     stuff.S.uri = (char *) e->url();
@@ -1589,7 +1595,6 @@ htcpClear(StoreEntry * e, const char *uri, HttpRequest * req, const HttpRequestM
     static char pkt[8192];
     ssize_t pktlen;
     char vbuf[32];
-    htcpStuff stuff;
     HttpHeader hdr(hoRequest);
     Packer pa;
     MemBuf mb;
@@ -1602,19 +1607,11 @@ htcpClear(StoreEntry * e, const char *uri, HttpRequest * req, const HttpRequestM
     memset(&flags, '\0', sizeof(flags));
     snprintf(vbuf, sizeof(vbuf), "%d/%d",
              req->http_ver.major, req->http_ver.minor);
-    stuff.op = HTCP_CLR;
-    stuff.rr = RR_REQUEST;
-    stuff.f1 = 0;
-    stuff.response = 0;
-    stuff.msg_id = ++msg_id_counter;
-    switch (reason) {
-    case HTCP_CLR_INVALIDATION:
+
+    htcpStuff stuff(++msg_id_counter, HTCP_CLR, RR_REQUEST, 0);
+    if (reason == HTCP_CLR_INVALIDATION)
         stuff.reason = 1;
-        break;
-    default:
-        stuff.reason = 0;
-        break;
-    }
+
     SBuf sb = req->method.image();
     stuff.S.method = sb.c_str();
     if (e == NULL || e->mem_obj == NULL) {