]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/StoreMetaUnpacker.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / StoreMetaUnpacker.cc
index 2fb97f53cd67e301aa55f58e9a06b2cae904cc6d..8c96627d509551eabd4ed5d8b9777c5c0c77f5d4 100644 (file)
@@ -1,41 +1,19 @@
-
 /*
- * DEBUG: section 20    Storage Manager Swapfile Unpacker
- * AUTHOR: Robert Collins
- *
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from
- *  the Internet community; see the CONTRIBUTORS file for full
- *  details.   Many organizations have provided support for Squid's
- *  development; see the SPONSORS file for full details.  Squid is
- *  Copyrighted (C) 2001 by the Regents of the University of
- *  California; see the COPYRIGHT file for full details.  Squid
- *  incorporates software developed and/or copyrighted by other
- *  sources; see the CREDITS file for full details.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
+/* DEBUG: section 20    Storage Manager Swapfile Unpacker */
+
 #include "squid.h"
-#include "defines.h"
+#include "base/TextException.h"
 #include "Debug.h"
-#include "StoreMetaUnpacker.h"
+#include "defines.h"
 #include "StoreMeta.h"
+#include "StoreMetaUnpacker.h"
 
 int const StoreMetaUnpacker::MinimumBufferLength = sizeof(char) + sizeof(int);
 
@@ -57,25 +35,21 @@ StoreMetaUnpacker::isBufferZero()
     return true;
 }
 
-bool
-StoreMetaUnpacker::isBufferSane()
+void
+StoreMetaUnpacker::checkBuffer()
 {
-    if (buf[0] != (char) STORE_META_OK)
-        return false;
-
+    assert(buf); // paranoid; already checked in the constructor
+    if (buf[0] != static_cast<char>(STORE_META_OK))
+        throw TexcHere("store entry metadata is corrupted");
     /*
      * sanity check on 'buflen' value.  It should be at least big
      * enough to hold one type and one length.
      */
     getBufferLength();
-
     if (*hdr_len < MinimumBufferLength)
-        return false;
-
+        throw TexcHere("store entry metadata is too small");
     if (*hdr_len > buflen)
-        return false;
-
-    return true;
+        throw TexcHere("store entry metadata is too big");
 }
 
 void
@@ -84,9 +58,16 @@ StoreMetaUnpacker::getBufferLength()
     memcpy(hdr_len, &buf[1], sizeof(int));
 }
 
-StoreMetaUnpacker::StoreMetaUnpacker (char const *aBuffer, ssize_t aLen, int *anInt) : buf (aBuffer), buflen(aLen), hdr_len(anInt), position(1 + sizeof(int))
+StoreMetaUnpacker::StoreMetaUnpacker(char const *aBuffer, ssize_t aLen, int *anInt) :
+    buf(aBuffer),
+    buflen(aLen),
+    hdr_len(anInt),
+    position(1 + sizeof(int)),
+    type('\0'),
+    length(0),
+    tail(NULL)
 {
-    assert (aBuffer != NULL);
+    assert(aBuffer != NULL);
 }
 
 void
@@ -138,8 +119,7 @@ StoreMetaUnpacker::createStoreMeta ()
     tail = &TLV;
     assert(hdr_len != NULL);
 
-    if (!isBufferSane())
-        return NULL;
+    checkBuffer();
 
     getBufferLength();
 
@@ -150,5 +130,10 @@ StoreMetaUnpacker::createStoreMeta ()
             break;
     }
 
+    if (!TLV)
+        throw TexcHere("store entry metadata is empty");
+
+    assert(TLV);
     return TLV;
 }
+