]> granicus.if.org Git - postgresql/commitdiff
Predict integer overflow to avoid buffer overruns.
authorNoah Misch <noah@leadboat.com>
Mon, 17 Feb 2014 14:33:31 +0000 (09:33 -0500)
committerNoah Misch <noah@leadboat.com>
Mon, 17 Feb 2014 14:33:38 +0000 (09:33 -0500)
Several functions, mostly type input functions, calculated an allocation
size such that the calculation wrapped to a small positive value when
arguments implied a sufficiently-large requirement.  Writes past the end
of the inadvertent small allocation followed shortly thereafter.
Coverity identified the path_in() vulnerability; code inspection led to
the rest.  In passing, add check_stack_depth() to prevent stack overflow
in related functions.

Back-patch to 8.4 (all supported versions).  The non-comment hstore
changes touch code that did not exist in 8.4, so that part stops at 9.0.

Noah Misch and Heikki Linnakangas, reviewed by Tom Lane.

Security: CVE-2014-0064

15 files changed:
contrib/hstore/hstore.h
contrib/hstore/hstore_io.c
contrib/hstore/hstore_op.c
contrib/intarray/_int.h
contrib/intarray/_int_bool.c
contrib/ltree/ltree.h
contrib/ltree/ltree_io.c
contrib/ltree/ltxtquery_io.c
src/backend/utils/adt/geo_ops.c
src/backend/utils/adt/tsquery.c
src/backend/utils/adt/tsquery_util.c
src/backend/utils/adt/txid.c
src/backend/utils/adt/varbit.c
src/include/tsearch/ts_type.h
src/include/utils/varbit.h

index 796dce575e52a79570a95da1596a9eeb813b55f7..93449885d478d8fa901d5aae46434a6da3754173 100644 (file)
@@ -49,9 +49,12 @@ typedef struct
 } HStore;
 
 /*
- * it's not possible to get more than 2^28 items into an hstore,
- * so we reserve the top few bits of the size field. See hstore_compat.c
- * for one reason why. Some bits are left for future use here.
+ * It's not possible to get more than 2^28 items into an hstore, so we reserve
+ * the top few bits of the size field.  See hstore_compat.c for one reason
+ * why.  Some bits are left for future use here.  MaxAllocSize makes the
+ * practical count limit slightly more than 2^28 / 3, or INT_MAX / 24, the
+ * limit for an hstore full of 4-byte keys and null values.  Therefore, we
+ * don't explicitly check the format-imposed limit.
  */
 #define HS_FLAG_NEWVERSION 0x80000000
 
@@ -59,6 +62,12 @@ typedef struct
 #define HS_SETCOUNT(hsp_,c_) ((hsp_)->size_ = (c_) | HS_FLAG_NEWVERSION)
 
 
+/*
+ * "x" comes from an existing HS_COUNT() (as discussed, <= INT_MAX/24) or a
+ * Pairs array length (due to MaxAllocSize, <= INT_MAX/40).  "lenstr" is no
+ * more than INT_MAX, that extreme case arising in hstore_from_arrays().
+ * Therefore, this calculation is limited to about INT_MAX / 5 + INT_MAX.
+ */
 #define HSHRDSIZE      (sizeof(HStore))
 #define CALCDATASIZE(x, lenstr) ( (x) * 2 * sizeof(HEntry) + HSHRDSIZE + (lenstr) )
 
index fa6da693e94eef81ccde3b99d0c26f50f745aeb1..bd4673dad4f870d8390879bb47f75098232b0e14 100644 (file)
@@ -11,6 +11,7 @@
 #include "funcapi.h"
 #include "libpq/pqformat.h"
 #include "utils/lsyscache.h"
+#include "utils/memutils.h"
 #include "utils/typcache.h"
 
 #include "hstore.h"
@@ -438,6 +439,11 @@ hstore_recv(PG_FUNCTION_ARGS)
                PG_RETURN_POINTER(out);
        }
 
+       if (pcount < 0 || pcount > MaxAllocSize / sizeof(Pairs))
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                         errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
+                                        pcount, (int) (MaxAllocSize / sizeof(Pairs)))));
        pairs = palloc(pcount * sizeof(Pairs));
 
        for (i = 0; i < pcount; ++i)
@@ -553,6 +559,13 @@ hstore_from_arrays(PG_FUNCTION_ARGS)
                                          TEXTOID, -1, false, 'i',
                                          &key_datums, &key_nulls, &key_count);
 
+       /* see discussion in hstoreArrayToPairs() */
+       if (key_count > MaxAllocSize / sizeof(Pairs))
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                         errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
+                                        key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
+
        /* value_array might be NULL */
 
        if (PG_ARGISNULL(1))
@@ -675,6 +688,13 @@ hstore_from_array(PG_FUNCTION_ARGS)
 
        count = in_count / 2;
 
+       /* see discussion in hstoreArrayToPairs() */
+       if (count > MaxAllocSize / sizeof(Pairs))
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                         errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
+                                        count, (int) (MaxAllocSize / sizeof(Pairs)))));
+
        pairs = palloc(count * sizeof(Pairs));
 
        for (i = 0; i < count; ++i)
@@ -806,6 +826,7 @@ hstore_from_record(PG_FUNCTION_ARGS)
                my_extra->ncolumns = ncolumns;
        }
 
+       Assert(ncolumns <= MaxTupleAttributeNumber);            /* thus, no overflow */
        pairs = palloc(ncolumns * sizeof(Pairs));
 
        if (rec)
index ebee60a1dbe5df5fb211f0c5c3a8eed84c1e3fba..35fcf7ddae143bd531b48523f19fb8b240940e49 100644 (file)
@@ -9,6 +9,7 @@
 #include "catalog/pg_type.h"
 #include "funcapi.h"
 #include "utils/builtins.h"
+#include "utils/memutils.h"
 
 #include "hstore.h"
 
@@ -91,6 +92,19 @@ hstoreArrayToPairs(ArrayType *a, int *npairs)
                return NULL;
        }
 
+       /*
+        * A text array uses at least eight bytes per element, so any overflow in
+        * "key_count * sizeof(Pairs)" is small enough for palloc() to catch.
+        * However, credible improvements to the array format could invalidate
+        * that assumption.  Therefore, use an explicit check rather than relying
+        * on palloc() to complain.
+        */
+       if (key_count > MaxAllocSize / sizeof(Pairs))
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                         errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
+                                        key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
+
        key_pairs = palloc(sizeof(Pairs) * key_count);
 
        for (i = 0, j = 0; i < key_count; i++)
@@ -645,6 +659,7 @@ hstore_slice_to_hstore(PG_FUNCTION_ARGS)
                PG_RETURN_POINTER(out);
        }
 
+       /* hstoreArrayToPairs() checked overflow */
        out_pairs = palloc(sizeof(Pairs) * nkeys);
        bufsiz = 0;
 
index 35dbb54796f36e2ece52fb6c90118b885cdca8e1..f448ef11aada794d90f4ee294cdf25081266c023 100644 (file)
@@ -5,6 +5,7 @@
 #define ___INT_H__
 
 #include "utils/array.h"
+#include "utils/memutils.h"
 
 /* number ranges for compression */
 #define MAXNUMRANGE 100
@@ -142,6 +143,7 @@ typedef struct
 
 #define HDRSIZEQT      (VARHDRSZ + sizeof(int4))
 #define COMPUTESIZE(size)      ( HDRSIZEQT + size * sizeof(ITEM) )
+#define QUERYTYPEMAXITEMS      ((MaxAllocSize - HDRSIZEQT) / sizeof(ITEM))
 #define GETQUERY(x)  (ITEM*)( (char*)(x)+HDRSIZEQT )
 
 #define END            0
index 7a478faa83bacd23170b3d0b46857c7d3670d7ab..468a53a7bbbf536c6ab6212c3d70430499993f1c 100644 (file)
@@ -416,6 +416,9 @@ boolop(PG_FUNCTION_ARGS)
 static void
 findoprnd(ITEM *ptr, int4 *pos)
 {
+       /* since this function recurses, it could be driven to stack overflow. */
+       check_stack_depth();
+
 #ifdef BS_DEBUG
        elog(DEBUG3, (ptr[*pos].type == OPR) ?
                 "%d  %c" : "%d  %d", *pos, ptr[*pos].val);
@@ -476,7 +479,13 @@ bqarr_in(PG_FUNCTION_ARGS)
                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 errmsg("empty query")));
 
+       if (state.num > QUERYTYPEMAXITEMS)
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+               errmsg("number of query items (%d) exceeds the maximum allowed (%d)",
+                          state.num, (int) QUERYTYPEMAXITEMS)));
        commonlen = COMPUTESIZE(state.num);
+
        query = (QUERYTYPE *) palloc(commonlen);
        SET_VARSIZE(query, commonlen);
        query->size = state.num;
index f16c6f9a32a06930267a9385087a6d6a8a72cc05..c3644e63ea0a4f524011d139b0b7d46d745bf27e 100644 (file)
@@ -6,6 +6,7 @@
 #include "postgres.h"
 #include "fmgr.h"
 #include "tsearch/ts_locale.h"
+#include "utils/memutils.h"
 
 typedef struct
 {
@@ -112,6 +113,8 @@ typedef struct
 
 #define HDRSIZEQT              MAXALIGN(VARHDRSZ + sizeof(int4))
 #define COMPUTESIZE(size,lenofoperand) ( HDRSIZEQT + (size) * sizeof(ITEM) + (lenofoperand) )
+#define LTXTQUERY_TOO_BIG(size,lenofoperand) \
+       ((size) > (MaxAllocSize - HDRSIZEQT - (lenofoperand)) / sizeof(ITEM))
 #define GETQUERY(x)  (ITEM*)( (char*)(x)+HDRSIZEQT )
 #define GETOPERAND(x)  ( (char*)GETQUERY(x) + ((ltxtquery*)x)->size * sizeof(ITEM) )
 
index a88eb16cb97d506df3a9f87eb73d6a56fc9b9e65..f92e9aaa7ea55b7e5f28a7eea8cc1247053ac222 100644 (file)
@@ -8,6 +8,7 @@
 #include <ctype.h>
 
 #include "ltree.h"
+#include "utils/memutils.h"
 #include "crc32.h"
 
 PG_FUNCTION_INFO_V1(ltree_in);
@@ -64,6 +65,11 @@ ltree_in(PG_FUNCTION_ARGS)
                ptr += charlen;
        }
 
+       if (num + 1 > MaxAllocSize / sizeof(nodeitem))
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                        errmsg("number of levels (%d) exceeds the maximum allowed (%d)",
+                                       num + 1, (int) (MaxAllocSize / sizeof(nodeitem)))));
        list = lptr = (nodeitem *) palloc(sizeof(nodeitem) * (num + 1));
        ptr = buf;
        while (*ptr)
@@ -228,6 +234,11 @@ lquery_in(PG_FUNCTION_ARGS)
        }
 
        num++;
+       if (num > MaxAllocSize / ITEMSIZE)
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                        errmsg("number of levels (%d) exceeds the maximum allowed (%d)",
+                                       num, (int) (MaxAllocSize / ITEMSIZE))));
        curqlevel = tmpql = (lquery_level *) palloc0(ITEMSIZE * num);
        ptr = buf;
        while (*ptr)
index d9163babf74807dbe72cd0b04b15d5d5e40b62eb..2cbcc89f5077d3b2d6577f3e4b8f5315d20ec202 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "crc32.h"
 #include "ltree.h"
+#include "miscadmin.h"
 
 PG_FUNCTION_INFO_V1(ltxtq_in);
 Datum          ltxtq_in(PG_FUNCTION_ARGS);
@@ -213,6 +214,9 @@ makepol(QPRS_STATE *state)
        int4            lenstack = 0;
        uint16          flag = 0;
 
+       /* since this function recurses, it could be driven to stack overflow */
+       check_stack_depth();
+
        while ((type = gettoken_query(state, &val, &lenval, &strval, &flag)) != END)
        {
                switch (type)
@@ -277,6 +281,9 @@ makepol(QPRS_STATE *state)
 static void
 findoprnd(ITEM *ptr, int4 *pos)
 {
+       /* since this function recurses, it could be driven to stack overflow. */
+       check_stack_depth();
+
        if (ptr[*pos].type == VAL || ptr[*pos].type == VALTRUE)
        {
                ptr[*pos].left = 0;
@@ -341,8 +348,12 @@ queryin(char *buf)
                                 errmsg("syntax error"),
                                 errdetail("Empty query.")));
 
-       /* make finish struct */
+       if (LTXTQUERY_TOO_BIG(state.num, state.sumlen))
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("ltxtquery is too large")));
        commonlen = COMPUTESIZE(state.num, state.sumlen);
+
        query = (ltxtquery *) palloc(commonlen);
        SET_VARSIZE(query, commonlen);
        query->size = state.num;
index 9fe9cef2042d6f9f3dbfa551a549d6bb52e0b25c..8a7f0812a77e62fe0c9012275b6f4a050b980d7e 100644 (file)
@@ -1403,6 +1403,7 @@ path_in(PG_FUNCTION_ARGS)
        char       *s;
        int                     npts;
        int                     size;
+       int                     base_size;
        int                     depth = 0;
 
        if ((npts = pair_count(str, ',')) <= 0)
@@ -1421,7 +1422,15 @@ path_in(PG_FUNCTION_ARGS)
                depth++;
        }
 
-       size = offsetof(PATH, p[0]) +sizeof(path->p[0]) * npts;
+       base_size = sizeof(path->p[0]) * npts;
+       size = offsetof(PATH, p[0]) + base_size;
+
+       /* Check for integer overflow */
+       if (base_size / npts != sizeof(path->p[0]) || size <= base_size)
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("too many points requested")));
+
        path = (PATH *) palloc(size);
 
        SET_VARSIZE(path, size);
@@ -3465,6 +3474,7 @@ poly_in(PG_FUNCTION_ARGS)
        POLYGON    *poly;
        int                     npts;
        int                     size;
+       int                     base_size;
        int                     isopen;
        char       *s;
 
@@ -3473,7 +3483,15 @@ poly_in(PG_FUNCTION_ARGS)
                                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                          errmsg("invalid input syntax for type polygon: \"%s\"", str)));
 
-       size = offsetof(POLYGON, p[0]) +sizeof(poly->p[0]) * npts;
+       base_size = sizeof(poly->p[0]) * npts;
+       size = offsetof(POLYGON, p[0]) + base_size;
+
+       /* Check for integer overflow */
+       if (base_size / npts != sizeof(poly->p[0]) || size <= base_size)
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("too many points requested")));
+
        poly = (POLYGON *) palloc0(size);       /* zero any holes */
 
        SET_VARSIZE(poly, size);
@@ -4379,6 +4397,10 @@ path_poly(PG_FUNCTION_ARGS)
                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 errmsg("open path cannot be converted to polygon")));
 
+       /*
+        * Never overflows: the old size fit in MaxAllocSize, and the new size is
+        * just a small constant larger.
+        */
        size = offsetof(POLYGON, p[0]) +sizeof(poly->p[0]) * path->npts;
        poly = (POLYGON *) palloc(size);
 
@@ -4484,6 +4506,10 @@ poly_path(PG_FUNCTION_ARGS)
        int                     size;
        int                     i;
 
+       /*
+        * Never overflows: the old size fit in MaxAllocSize, and the new size is
+        * smaller by a small constant.
+        */
        size = offsetof(PATH, p[0]) +sizeof(path->p[0]) * poly->npts;
        path = (PATH *) palloc(size);
 
index f5c1650a10ef1e71cdbff5c923e3abf614559f31..40e8c014cf683256b081fbc8ff1947962884e596 100644 (file)
@@ -517,8 +517,13 @@ parse_tsquery(char *buf,
                return query;
        }
 
-       /* Pack the QueryItems in the final TSQuery struct to return to caller */
+       if (TSQUERY_TOO_BIG(list_length(state.polstr), state.sumlen))
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("tsquery is too large")));
        commonlen = COMPUTESIZE(list_length(state.polstr), state.sumlen);
+
+       /* Pack the QueryItems in the final TSQuery struct to return to caller */
        query = (TSQuery) palloc0(commonlen);
        SET_VARSIZE(query, commonlen);
        query->size = list_length(state.polstr);
index 0313a85f8a3bf8be8293b88f1b2b28d76ac1967c..e14d18ce88283808b3f373007b43fc8d4bc84e6b 100644 (file)
@@ -334,6 +334,11 @@ QTN2QT(QTNode *in)
        QTN2QTState state;
 
        cntsize(in, &sumlen, &nnode);
+
+       if (TSQUERY_TOO_BIG(nnode, sumlen))
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("tsquery is too large")));
        len = COMPUTESIZE(nnode, sumlen);
 
        out = (TSQuery) palloc0(len);
index db7ecd14b26ce965fea629016f924d8603e03dca..6a8a8803836aa922d6b9a6528f42d6cea16837e3 100644 (file)
@@ -27,6 +27,7 @@
 #include "miscadmin.h"
 #include "libpq/pqformat.h"
 #include "utils/builtins.h"
+#include "utils/memutils.h"
 #include "utils/snapmgr.h"
 
 
@@ -66,6 +67,8 @@ typedef struct
 
 #define TXID_SNAPSHOT_SIZE(nxip) \
        (offsetof(TxidSnapshot, xip) + sizeof(txid) * (nxip))
+#define TXID_SNAPSHOT_MAX_NXIP \
+       ((MaxAllocSize - offsetof(TxidSnapshot, xip)) / sizeof(txid))
 
 /*
  * Epoch values from xact.c
@@ -444,20 +447,12 @@ txid_snapshot_recv(PG_FUNCTION_ARGS)
        txid            last = 0;
        int                     nxip;
        int                     i;
-       int                     avail;
-       int                     expect;
        txid            xmin,
                                xmax;
 
-       /*
-        * load nxip and check for nonsense.
-        *
-        * (nxip > avail) check is against int overflows in 'expect'.
-        */
+       /* load and validate nxip */
        nxip = pq_getmsgint(buf, 4);
-       avail = buf->len - buf->cursor;
-       expect = 8 + 8 + nxip * 8;
-       if (nxip < 0 || nxip > avail || expect > avail)
+       if (nxip < 0 || nxip > TXID_SNAPSHOT_MAX_NXIP)
                goto bad_format;
 
        xmin = pq_getmsgint64(buf);
index d0cd0eba9064ea21d6690d519d5d72d1eb94bae7..82e4a2bec327607d3d58375edf05fb9106bd1c24 100644 (file)
@@ -143,12 +143,22 @@ bit_in(PG_FUNCTION_ARGS)
                sp = input_string;
        }
 
+       /*
+        * Determine bitlength from input string.  MaxAllocSize ensures a regular
+        * input is small enough, but we must check hex input.
+        */
        slen = strlen(sp);
-       /* Determine bitlength from input string */
        if (bit_not_hex)
                bitlen = slen;
        else
+       {
+               if (slen > VARBITMAXLEN / 4)
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("bit string length exceeds the maximum allowed (%d)",
+                                               VARBITMAXLEN)));
                bitlen = slen * 4;
+       }
 
        /*
         * Sometimes atttypmod is not supplied. If it is supplied we need to make
@@ -441,12 +451,22 @@ varbit_in(PG_FUNCTION_ARGS)
                sp = input_string;
        }
 
+       /*
+        * Determine bitlength from input string.  MaxAllocSize ensures a regular
+        * input is small enough, but we must check hex input.
+        */
        slen = strlen(sp);
-       /* Determine bitlength from input string */
        if (bit_not_hex)
                bitlen = slen;
        else
+       {
+               if (slen > VARBITMAXLEN / 4)
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("bit string length exceeds the maximum allowed (%d)",
+                                               VARBITMAXLEN)));
                bitlen = slen * 4;
+       }
 
        /*
         * Sometimes atttypmod is not supplied. If it is supplied we need to make
@@ -525,6 +545,9 @@ varbit_in(PG_FUNCTION_ARGS)
 
 /* varbit_out -
  *       Prints the string as bits to preserve length accurately
+ *
+ * XXX varbit_recv() and hex input to varbit_in() can load a value that this
+ * cannot emit.  Consider using hex output for such values.
  */
 Datum
 varbit_out(PG_FUNCTION_ARGS)
@@ -898,6 +921,11 @@ bit_catenate(VarBit *arg1, VarBit *arg2)
        bitlen1 = VARBITLEN(arg1);
        bitlen2 = VARBITLEN(arg2);
 
+       if (bitlen1 > VARBITMAXLEN - bitlen2)
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("bit string length exceeds the maximum allowed (%d)",
+                                               VARBITMAXLEN)));
        bytelen = VARBITTOTALLEN(bitlen1 + bitlen2);
 
        result = (VarBit *) palloc(bytelen);
index ca553df2decda043b2f57cfe13f6c001daa7d4fa..56cf51d5abf38d1318767e0eabd7ba692eeab15e 100644 (file)
@@ -13,6 +13,7 @@
 #define _PG_TSTYPE_H_
 
 #include "fmgr.h"
+#include "utils/memutils.h"
 #include "utils/pg_crc.h"
 
 
@@ -242,6 +243,8 @@ typedef TSQueryData *TSQuery;
  * QueryItems, and lenofoperand is the total length of all operands
  */
 #define COMPUTESIZE(size, lenofoperand) ( HDRSIZETQ + (size) * sizeof(QueryItem) + (lenofoperand) )
+#define TSQUERY_TOO_BIG(size, lenofoperand) \
+       ((size) > (MaxAllocSize - HDRSIZETQ - (lenofoperand)) / sizeof(QueryItem))
 
 /* Returns a pointer to the first QueryItem in a TSQuery */
 #define GETQUERY(x)  ((QueryItem*)( (char*)(x)+HDRSIZETQ ))
index 02ed5f795e485b5dc61b2f8e37966fa0825f833c..ec4bd134ef44d50746a46e01c09362162658b8a5 100644 (file)
@@ -15,6 +15,8 @@
 #ifndef VARBIT_H
 #define VARBIT_H
 
+#include <limits.h>
+
 #include "fmgr.h"
 
 /*
@@ -53,6 +55,11 @@ typedef struct
 /* Number of bytes needed to store a bit string of a given length */
 #define VARBITTOTALLEN(BITLEN) (((BITLEN) + BITS_PER_BYTE-1)/BITS_PER_BYTE + \
                                                                 VARHDRSZ + VARBITHDRSZ)
+/*
+ * Maximum number of bits.  Several code sites assume no overflow from
+ * computing bitlen + X; VARBITTOTALLEN() has the largest such X.
+ */
+#define VARBITMAXLEN           (INT_MAX - BITS_PER_BYTE + 1)
 /* pointer beyond the end of the bit string (like end() in STL containers) */
 #define VARBITEND(PTR)         (((bits8 *) (PTR)) + VARSIZE(PTR))
 /* Mask that will cover exactly one byte, i.e. BITS_PER_BYTE bits */