Refine integer signedness use for static analyizers (#24)


diff --git a/src/UsefulBuf.c b/src/UsefulBuf.c
index 1d13f10..a96f74e 100644
--- a/src/UsefulBuf.c
+++ b/src/UsefulBuf.c
@@ -41,7 +41,8 @@
 
  when        who          what, where, why
  --------    ----         ---------------------------------------------------
- 01/08/2020 llundblade    Documentation corrections & improved code formatting.
+ 01/28/2020  llundblade   Refine integer signedness to quiet static analysis.
+ 01/08/2020  llundblade   Documentation corrections & improved code formatting.
  11/08/2019  llundblade   Re check pointer math and update comments
  3/6/2019    llundblade   Add UsefulBuf_IsValue()
  09/07/17    llundbla     Fix critical bug in UsefulBuf_Find() -- a read off
@@ -109,7 +110,8 @@
    for(const uint8_t *p = UB.ptr; p < pEnd; p++) {
       if(*p != uValue) {
          /* Byte didn't match */
-         return p - (uint8_t *)UB.ptr;
+         /* Cast from signed  to unsigned . Safe because the loop increments.*/
+         return (size_t)(p - (uint8_t *)UB.ptr);
       }
    }
 
diff --git a/src/ieee754.c b/src/ieee754.c
index ef0adef..41f60cf 100644
--- a/src/ieee754.c
+++ b/src/ieee754.c
@@ -65,10 +65,10 @@
 #define HALF_EXPONENT_SHIFT       (HALF_NUM_SIGNIFICAND_BITS)
 #define HALF_SIGN_SHIFT           (HALF_NUM_SIGNIFICAND_BITS + HALF_NUM_EXPONENT_BITS)
 
-#define HALF_SIGNIFICAND_MASK     (0x3ff) // The lower 10 bits  // 0x03ff
-#define HALF_EXPONENT_MASK        (0x1f << HALF_EXPONENT_SHIFT) // 0x7c00 5 bits of exponent
-#define HALF_SIGN_MASK            (0x01 << HALF_SIGN_SHIFT) //  // 0x80001 bit of sign
-#define HALF_QUIET_NAN_BIT        (0x01 << (HALF_NUM_SIGNIFICAND_BITS-1)) // 0x0200
+#define HALF_SIGNIFICAND_MASK     (0x3ffU) // The lower 10 bits  // 0x03ff
+#define HALF_EXPONENT_MASK        (0x1fU << HALF_EXPONENT_SHIFT) // 0x7c00 5 bits of exponent
+#define HALF_SIGN_MASK            (0x01U << HALF_SIGN_SHIFT) //  // 0x8000 1 bit of sign
+#define HALF_QUIET_NAN_BIT        (0x01U << (HALF_NUM_SIGNIFICAND_BITS-1)) // 0x0200
 
 /* Biased    Biased    Unbiased   Use
     0x00       0        -15       0 and subnormal
@@ -91,10 +91,10 @@
 #define SINGLE_EXPONENT_SHIFT       (SINGLE_NUM_SIGNIFICAND_BITS)
 #define SINGLE_SIGN_SHIFT           (SINGLE_NUM_SIGNIFICAND_BITS + SINGLE_NUM_EXPONENT_BITS)
 
-#define SINGLE_SIGNIFICAND_MASK     (0x7fffffUL) // The lower 23 bits
-#define SINGLE_EXPONENT_MASK        (0xffUL << SINGLE_EXPONENT_SHIFT) // 8 bits of exponent
-#define SINGLE_SIGN_MASK            (0x01UL << SINGLE_SIGN_SHIFT) // 1 bit of sign
-#define SINGLE_QUIET_NAN_BIT        (0x01UL << (SINGLE_NUM_SIGNIFICAND_BITS-1))
+#define SINGLE_SIGNIFICAND_MASK     (0x7fffffU) // The lower 23 bits
+#define SINGLE_EXPONENT_MASK        (0xffU << SINGLE_EXPONENT_SHIFT) // 8 bits of exponent
+#define SINGLE_SIGN_MASK            (0x01U << SINGLE_SIGN_SHIFT) // 1 bit of sign
+#define SINGLE_QUIET_NAN_BIT        (0x01U << (SINGLE_NUM_SIGNIFICAND_BITS-1))
 
 /* Biased  Biased   Unbiased  Use
     0x0000     0     -127      0 and subnormal
@@ -229,7 +229,7 @@
         // Also have to shift the significand by the difference in number of bits between a single and a half significand
         const uint32_t uSignificandBitsDiff = SINGLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS;
         // Add in the 1 that is implied in the significand of a normal number; it needs to be present in a subnormal
-        const uint32_t uSingleSignificandSubnormal = uSingleSignificand + (0x01UL << SINGLE_NUM_SIGNIFICAND_BITS);
+        const uint32_t uSingleSignificandSubnormal = uSingleSignificand + (0x01U << SINGLE_NUM_SIGNIFICAND_BITS);
         uHalfSignificand = uSingleSignificandSubnormal >> (uExpDiff + uSignificandBitsDiff);
     } else {
         // The normal case, exponent is in range for half-precision
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 661264e..1b6ff3e 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -42,6 +42,7 @@
 
  when       who             what, where, why
  --------   ----            ---------------------------------------------------
+ 01/28/2020 llundblade      Refine integer signedness to quiet static analysis.
  01/25/2020 llundblade      Cleaner handling of too-long encoded string input.
  01/25/2020 llundblade      Refine use of integer types to quiet static analysis
  01/08/2020 llundblade      Documentation corrections & improved code formatting
@@ -1199,7 +1200,7 @@
  The epoch formatted date. Turns lots of different forms of encoding
  date into uniform one
  */
-static int DecodeDateEpoch(QCBORItem *pDecodedItem)
+static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem)
 {
    // Stack usage: 1
    QCBORError nReturn = QCBOR_SUCCESS;
@@ -1217,7 +1218,7 @@
             nReturn = QCBOR_ERR_DATE_OVERFLOW;
             goto Done;
          }
-         pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.uint64;
+         pDecodedItem->val.epochDate.nSeconds = (int64_t)pDecodedItem->val.uint64;
          break;
 
       case QCBOR_TYPE_DOUBLE:
@@ -1341,7 +1342,9 @@
       // Got a good big num mantissa
       pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum;
       // Depends on numbering of QCBOR_TYPE_XXX
-      pDecodedItem->uDataType += 1 + mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM;
+      pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType +
+                                          mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM +
+                                          1);
    } else {
       // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX
       nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
@@ -1508,7 +1511,7 @@
  */
 QCBORError QCBORDecode_Finish(QCBORDecodeContext *me)
 {
-   int nReturn = QCBOR_SUCCESS;
+   QCBORError nReturn = QCBOR_SUCCESS;
 
    // Error out if all the maps/arrays are not closed out
    if(DecodeNesting_IsNested(&(me->nesting))) {
@@ -1688,7 +1691,7 @@
          if(uNewSize <= uPoolSize - uFreeOffset) {
             ReturnValue.len = uNewSize;
             ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset;
-            uFreeOffset    += uNewSize;
+            uFreeOffset    += (uint32_t)uNewSize;
          }
       }
    } else {