Merge remote-tracking branch 'cabo/master' into PrettyPrint
diff --git a/build.sh b/build.sh
index a07f37f..69dd2e9 100755
--- a/build.sh
+++ b/build.sh
@@ -1,3 +1,4 @@
+#!/bin/sh
 if [ ! -d "build" ]; then
     mkdir build
 fi
diff --git a/include/cn-cbor/cn-cbor.h b/include/cn-cbor/cn-cbor.h
index 2a16f0a..b759c22 100644
--- a/include/cn-cbor/cn-cbor.h
+++ b/include/cn-cbor/cn-cbor.h
@@ -221,7 +221,7 @@
  * @param[out] errp         Error, if NULL is returned
  * @return                  The parsed CBOR structure, or NULL on error
  */
-const cn_cbor* cn_cbor_decode(const uint8_t *buf, size_t len CBOR_CONTEXT, cn_cbor_errback *errp);
+cn_cbor* cn_cbor_decode(const uint8_t *buf, size_t len CBOR_CONTEXT, cn_cbor_errback *errp);
 
 /**
  * Get a value from a CBOR map that has the given string as a key.
@@ -230,7 +230,7 @@
  * @param[in]  key          The string to look up in the map
  * @return                  The matching value, or NULL if the key is not found
  */
-const cn_cbor* cn_cbor_mapget_string(const cn_cbor* cb, const char* key);
+cn_cbor* cn_cbor_mapget_string(const cn_cbor* cb, const char* key);
 
 /**
  * Get a value from a CBOR map that has the given integer as a key.
@@ -239,7 +239,7 @@
  * @param[in]  key          The int to look up in the map
  * @return                  The matching value, or NULL if the key is not found
  */
-const cn_cbor* cn_cbor_mapget_int(const cn_cbor* cb, int key);
+cn_cbor* cn_cbor_mapget_int(const cn_cbor* cb, int key);
 
 /**
  * Get the item with the given index from a CBOR array.
@@ -248,15 +248,17 @@
  * @param[in]  idx          The array index
  * @return                  The matching value, or NULL if the index is invalid
  */
-const cn_cbor* cn_cbor_index(const cn_cbor* cb, unsigned int idx);
+cn_cbor* cn_cbor_index(const cn_cbor* cb, unsigned int idx);
 
 /**
  * Free the given CBOR structure.
+ * You MUST NOT try to free a cn_cbor structure with a parent (i.e., one
+ * that is not a root in the tree).
  *
- * @param[in]  cb           The CBOR value to free
+ * @param[in]  cb           The CBOR value to free.  May be NULL, or a root object.
  * @param[in]  CBOR_CONTEXT Allocation context (only if USE_CBOR_CONTEXT is defined)
  */
-void cn_cbor_free(const cn_cbor* cb CBOR_CONTEXT);
+void cn_cbor_free(cn_cbor* cb CBOR_CONTEXT);
 
 /**
  * Write a CBOR value and all of the child values.
diff --git a/src/cn-cbor.c b/src/cn-cbor.c
index 83f576d..4947e63 100644
--- a/src/cn-cbor.c
+++ b/src/cn-cbor.c
@@ -24,8 +24,9 @@
 
 #define CN_CBOR_FAIL(code) do { pb->err = code;  goto fail; } while(0)
 
-void cn_cbor_free(const cn_cbor* cb CBOR_CONTEXT) {
-  cn_cbor* p = (cn_cbor*) cb;
+void cn_cbor_free(cn_cbor* cb CBOR_CONTEXT) {
+  cn_cbor* p = cb;
+  assert(!p || !p->parent);
   while (p) {
     cn_cbor* p1;
     while ((p1 = p->first_child)) { /* go down */
@@ -241,7 +242,7 @@
   return 0;
 }
 
-const cn_cbor* cn_cbor_decode(const unsigned char* buf, size_t len CBOR_CONTEXT, cn_cbor_errback *errp) {
+cn_cbor* cn_cbor_decode(const unsigned char* buf, size_t len CBOR_CONTEXT, cn_cbor_errback *errp) {
   cn_cbor catcher = {CN_CBOR_INVALID, 0, {0}, 0, NULL, NULL, NULL, NULL};
   struct parse_buf pb;
   cn_cbor* ret;
diff --git a/src/cn-get.c b/src/cn-get.c
index f4585b9..cc276a5 100644
--- a/src/cn-get.c
+++ b/src/cn-get.c
@@ -4,7 +4,7 @@
 
 #include "cn-cbor/cn-cbor.h"
 
-const cn_cbor* cn_cbor_mapget_int(const cn_cbor* cb, int key) {
+cn_cbor* cn_cbor_mapget_int(const cn_cbor* cb, int key) {
   cn_cbor* cp;
   assert(cb);
   for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) {
@@ -25,7 +25,7 @@
   return NULL;
 }
 
-const cn_cbor* cn_cbor_mapget_string(const cn_cbor* cb, const char* key) {
+cn_cbor* cn_cbor_mapget_string(const cn_cbor* cb, const char* key) {
   cn_cbor *cp;
   int keylen;
   assert(cb);
@@ -48,7 +48,7 @@
   return NULL;
 }
 
-const cn_cbor* cn_cbor_index(const cn_cbor* cb, unsigned int idx) {
+cn_cbor* cn_cbor_index(const cn_cbor* cb, unsigned int idx) {
   cn_cbor *cp;
   unsigned int i = 0;
   assert(cb);
diff --git a/test/cbor_test.c b/test/cbor_test.c
index ebd5edd..3326497 100644
--- a/test/cbor_test.c
+++ b/test/cbor_test.c
@@ -111,7 +111,7 @@
         "9f009f00ff00ff",         // [_ 0, [_ 0], 0]
         "bf61610161629f0203ffff", // {_ "a": 1, "b": [_ 2, 3]}
     };
-    const cn_cbor *cb;
+    cn_cbor *cb;
     buffer b;
     size_t i;
     unsigned char encoded[1024];
@@ -159,7 +159,7 @@
       "fb3e78000000000000", "fa33c00000", //  8.940696716308594e-08
       "fb3e80000000000000", "f90002",   // 1.1920928955078125e-07
     };
-    const cn_cbor *cb;
+    cn_cbor *cb;
     buffer b, b2;
     size_t i;
     unsigned char encoded[1024];
@@ -221,7 +221,7 @@
         {"1c", CN_CBOR_ERR_RESERVED_AI},
         {"7f4100", CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING},
     };
-    const cn_cbor *cb;
+    cn_cbor *cb;
     buffer b;
     size_t i;
     uint8_t buf[10];
@@ -253,7 +253,7 @@
         "f9fc00", // -Inf
         "f97c00", // Inf
     };
-    const cn_cbor *cb;
+    cn_cbor *cb;
     buffer b;
     size_t i;
     unsigned char encoded[1024];
@@ -276,8 +276,8 @@
 CTEST(cbor, getset)
 {
     buffer b;
-    const cn_cbor *cb;
-    const cn_cbor *val;
+    cn_cbor *cb;
+    cn_cbor *val;
     cn_cbor_errback err;
 
     ASSERT_TRUE(parse_hex("a40000436363630262626201616100", &b));
diff --git a/test/test.c b/test/test.c
index 263f9fe..d24992f 100644
--- a/test/test.c
+++ b/test/test.c
@@ -115,7 +115,7 @@
   char *bufend;
   unsigned char *s = load_file("cases.cbor", &end);
   printf("%zd\n", end-s);
-  const cn_cbor *cb = cn_cbor_decode(s, end-s CBOR_CONTEXT_PARAM, 0);
+  cn_cbor *cb = cn_cbor_decode(s, end-s CBOR_CONTEXT_PARAM, 0);
   if (cb) {
     dump(cb, buf, &bufend, 0);
     *bufend = 0;