]> Cypherpunks repositories - keks.git/commitdiff
More assertions against an empty buffers
authorSergey Matveev <stargrave@stargrave.org>
Thu, 19 Jun 2025 13:28:02 +0000 (16:28 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 19 Jun 2025 13:30:07 +0000 (16:30 +0300)
c/lib/cm/pub.c
c/lib/dec.c
c/lib/dectai.c
c/lib/enc.c
c/lib/enctai.c
c/lib/frombe.c
c/lib/items.c
c/lib/schema.c
c/lib/tobe.c

index 06206b5e935a77329b5edff7c807308b0639ac2b15a603bb6b1ca21924656c54..25b9601c0bef37f2603ab2cca2456cd5b3d61d9d4a14b73bf01294ba930a3791 100644 (file)
@@ -20,6 +20,9 @@ KEKSCMPubParse(
     const unsigned char *buf,
     const size_t len)
 {
+    assert(cer != NULL);
+    assert(off != NULL);
+    assert(failReason != NULL);
     (*failReason) = "unspecified";
     struct KEKSItems schema;
     enum KEKSErr err = KEKSItemsInit(&schema, 512);
@@ -105,6 +108,10 @@ KEKSCMPubVerify(
     const size_t poolLen,
     const struct KEKSCMPubVerifyOpts opts)
 {
+    assert(failReason != NULL);
+    assert(verifier != NULL);
+    assert(cer != NULL);
+    assert(pool != NULL);
     (*failReason) = "unspecified";
     if (opts.t.tv_sec <= cer->since.tv_sec) {
         (*failReason) = "unsatisfied since";
index 408f83e7098dedfa4c84c41d80cfd01329dda28ad00290281306bd0124843d88..8df815fd274f06d837691f0adf5ed204924c99691d10573e2ddc85529373840a 100644 (file)
@@ -32,12 +32,15 @@ KEKSAtomDecode( // NOLINT(misc-no-recursion)
     const unsigned char *buf,
     const size_t len)
 {
+    assert(atom != NULL);
+    assert(got != NULL);
     atom->v.str.ptr = NULL;
     atom->v.str.len = 0;
     (*got) = 1;
     if (len < 1) {
         return KEKSErrNotEnough;
     }
+    assert(buf != NULL);
     unsigned char tag = buf[0];
 
     if ((tag & (uint8_t)KEKSAtomStrings) > 0) {
index b302d536c5f7b4ab5e3b3b062d3b2b3a5a1ce88b6a68765cef4bbe0febb7f4d3..7da053741cb7029afa733da2f21c4481b6b61e6c1794ec74ef938330d478b778 100644 (file)
@@ -13,6 +13,7 @@
 // You should have received a copy of the GNU Lesser General Public
 // License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+#include <assert.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <time.h>
index 237a1520e2f5aa7d854323a07d5006dc7b8a3f9f8e9e0395da91370cb63d3bd8..5d2cabc2a75389c081874a60b91306994e8714e28d91fb66669e3d07b3259dcf 100644 (file)
@@ -13,6 +13,7 @@
 // You should have received a copy of the GNU Lesser General Public
 // License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+#include <assert.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 bool
 KEKSAtomEOCEncode(size_t *len, unsigned char *buf, const size_t cap)
 {
+    assert(len != NULL);
     (*len) = 1;
     if (cap < 1) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = KEKSAtomEOC;
     return true;
 }
@@ -36,10 +39,12 @@ KEKSAtomEOCEncode(size_t *len, unsigned char *buf, const size_t cap)
 bool
 KEKSAtomNILEncode(size_t *len, unsigned char *buf, const size_t cap)
 {
+    assert(len != NULL);
     (*len) = 1;
     if (cap < 1) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = KEKSAtomNIL;
     return true;
 }
@@ -47,10 +52,12 @@ KEKSAtomNILEncode(size_t *len, unsigned char *buf, const size_t cap)
 bool
 KEKSAtomBoolEncode(size_t *len, unsigned char *buf, const size_t cap, const bool v)
 {
+    assert(len != NULL);
     (*len) = 1;
     if (cap < 1) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = v ? KEKSAtomTrue : KEKSAtomFalse;
     return true;
 }
@@ -62,10 +69,12 @@ KEKSAtomHexletEncode(
     const size_t cap,
     const unsigned char v[16])
 {
+    assert(len != NULL);
     (*len) = 1 + 16;
     if (cap < (1 + 16)) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = KEKSAtomHexlet;
     memcpy(buf + 1, v, 16);
     return true;
@@ -82,10 +91,12 @@ KEKSAtomMagicEncode(
     if (vlen > 12) {
         return false;
     }
+    assert(len != NULL);
     (*len) = 1 + 15;
     if (cap < (1 + 15)) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = 'K';
     buf[1] = 'E';
     buf[2] = 'K';
@@ -101,6 +112,8 @@ keksAtomIntEncode(size_t *len, unsigned char *buf, const size_t cap, const uint6
     if (cap < 1) {
         return false;
     }
+    assert(len != NULL);
+    assert(buf != NULL);
     if (v == 0) {
         const bool ok =
             KEKSAtomBinEncode(len, buf + 1, cap - 1, (const unsigned char *)"", 0);
@@ -148,10 +161,12 @@ KEKSAtomSintEncode(size_t *len, unsigned char *buf, const size_t cap, const int6
 bool
 KEKSAtomListEncode(size_t *len, unsigned char *buf, const size_t cap)
 {
+    assert(len != NULL);
     (*len) = 1;
     if (cap < 1) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = KEKSAtomList;
     return true;
 }
@@ -159,10 +174,12 @@ KEKSAtomListEncode(size_t *len, unsigned char *buf, const size_t cap)
 bool
 KEKSAtomMapEncode(size_t *len, unsigned char *buf, const size_t cap)
 {
+    assert(len != NULL);
     (*len) = 1;
     if (cap < 1) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = KEKSAtomMap;
     return true;
 }
@@ -174,10 +191,12 @@ KEKSAtomBlobEncode(
     const size_t cap,
     const size_t chunkLen)
 {
+    assert(len != NULL);
     (*len) = 1 + 8;
     if (cap < 1 + 8) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = KEKSAtomBlob;
     keksToBE(buf + 1, 8, (uint64_t)chunkLen - 1);
     return true;
@@ -210,6 +229,7 @@ keksAtomStrEncode(
     } else {
         lVal = (unsigned char)srcLen;
     }
+    assert(len != NULL);
     (*len) = 1 + lLen + srcLen;
     if ((*len) <= srcLen) {
         (*len) = 0;
@@ -218,6 +238,7 @@ keksAtomStrEncode(
     if (cap < (*len)) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = (uint8_t)KEKSAtomStrings | lVal;
     if (utf8) {
         buf[0] |= (unsigned char)KEKSAtomIsUTF8;
@@ -271,10 +292,12 @@ KEKSAtomTAI64Encode(
     default:
         return false;
     }
+    assert(len != NULL);
     (*len) = 1 + srcLen;
     if (cap < (*len)) {
         return false;
     }
+    assert(buf != NULL);
     buf[0] = tag;
     memcpy(buf + 1, src, srcLen);
     return true;
index f695f25ddebb0fc814f634d7327017a3561b3d8f5a715f2cb19daffae706743b..7ac2ec18ef3edd4e326b6cdb256e7f8337d491924d77b92aa7994989bbb98029 100644 (file)
@@ -13,6 +13,7 @@
 // You should have received a copy of the GNU Lesser General Public
 // License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+#include <assert.h>
 #include <stdint.h>
 #include <time.h>
 
 bool
 KEKSTimespecToTAI64(unsigned char *buf, const struct timespec *ts)
 {
+    assert(ts != NULL);
     int64_t v = (int64_t)(ts->tv_sec);
     uint64_t val = (uint64_t)v + 0x4000000000000000;
     if (val <= (uint64_t)v) {
         return false;
     }
+    assert(buf != NULL);
     keksToBE(buf, 8, val);
     if (ts->tv_nsec != 0) {
         keksToBE(buf + 8, 4, (uint64_t)(ts->tv_nsec));
index b535b393ac25db88fabbbd3e8b2fe34ee08c7089bb643d60daed0dc76c8e680a..efefe2a77ba2d245ed32bc56d2233e1a66a079e1f2138ae154e794ca5a5dd3d5 100644 (file)
@@ -1,3 +1,19 @@
+// KEKS -- C99 KEKS encoder implementation
+// Copyright (C) 2024-2025 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, version 3 of the License.
+//
+// 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 Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <assert.h>
 #include <stddef.h>
 #include <stdint.h>
 
@@ -6,6 +22,7 @@
 uint64_t
 keksFromBE(const unsigned char *buf, const size_t len)
 {
+    assert(buf != NULL);
     uint64_t v = 0;
     switch (len) {
     case 1:
index e0285b9f4ff1c9851d8da1747b56e515a881240199f25a965bf13a1b0e19447e..64ca4058ec12ccd5c63de1df3dfba91d280c2ed83010bed11e038cb4f4b1a4cd 100644 (file)
@@ -30,6 +30,7 @@ static const size_t parseMaxRecursionDepth = 1024;
 enum KEKSErr
 KEKSItemsInit(struct KEKSItems *items, const ptrdiff_t initialLen)
 {
+    assert(items != NULL);
     items->len = 0;
     items->cap = initialLen;
     items->reallocs = 0;
@@ -47,6 +48,7 @@ KEKSItemsInit(struct KEKSItems *items, const ptrdiff_t initialLen)
 void
 KEKSItemsNoOffsets(struct KEKSItems *items)
 {
+    assert(items != NULL);
     if (items->offsets == NULL) {
         return;
     }
@@ -57,6 +59,7 @@ KEKSItemsNoOffsets(struct KEKSItems *items)
 void
 KEKSItemsFree(struct KEKSItems *items)
 {
+    assert(items != NULL);
     items->len = 0;
     items->cap = 0;
     if (items->list != NULL) {
@@ -69,6 +72,7 @@ KEKSItemsFree(struct KEKSItems *items)
 enum KEKSErr
 KEKSItemsGrow(struct KEKSItems *items)
 {
+    assert(items != NULL);
     if (items->cap == -1) {
         return KEKSErrNoMem;
     }
@@ -111,6 +115,9 @@ keksItemsAdd(
     const unsigned char *buf,
     const size_t len)
 {
+    assert(items != NULL);
+    assert(off != NULL);
+    assert(buf != NULL);
     enum KEKSErr err = KEKSErrInvalid;
     if (items->len == (size_t)(items->cap)) {
         err = KEKSItemsGrow(items);
@@ -146,6 +153,9 @@ keksItemsParse( // NOLINT(misc-no-recursion)
     const bool allowContainers,
     const size_t recursionDepth)
 {
+    assert(items != NULL);
+    assert(off != NULL);
+    assert(buf != NULL);
     if (recursionDepth >= parseMaxRecursionDepth) {
         return KEKSErrDeepRecursion;
     }
@@ -312,6 +322,9 @@ KEKSItemsEncode( // NOLINT(misc-no-recursion)
     unsigned char *buf,
     const size_t cap)
 {
+    assert(items != NULL);
+    assert(off != NULL);
+    assert(buf != NULL);
     const struct KEKSItem *item = &(items->list[idx]);
     size_t got = 0;
     bool ok = false;
@@ -460,6 +473,7 @@ KEKSItemsGetByKeyLen(
     const char *key,
     const size_t keyLen)
 {
+    assert(items != NULL);
     const struct KEKSItem *item = &(items->list[itemIdx]);
     if (item->atom.typ != KEKSItemMap) {
         return 0;
@@ -485,6 +499,7 @@ KEKSItemsGetByKey(const struct KEKSItems *items, const size_t itemIdx, const cha
 bool
 KEKSStrEqual(const struct KEKSAtom *atom, const char *s)
 {
+    assert(atom != NULL);
     return (atom->v.str.len == strlen(s)) &&
            (memcmp(atom->v.str.ptr, s, atom->v.str.len) == 0);
 }
index bfe5734554ad31a5c5570d867130172f1d7b492d316724ac67cbe14f9c2fb801..5b2474c2321a74d992b09ca9219686d9be313bfdebf5417936d1f49ca7f0add8 100644 (file)
@@ -13,6 +13,7 @@
 // You should have received a copy of the GNU Lesser General Public
 // License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+#include <assert.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
@@ -69,6 +70,10 @@ keksSchemaLens(
     size_t idxSchema,
     size_t idxData)
 {
+    assert(our != NULL);
+    assert(their != NULL);
+    assert(schema != NULL);
+    assert(data != NULL);
     switch (schema->list[idxSchema].atom.typ) {
     case KEKSItemPint:
         (*our) = (int64_t)(schema->list[idxSchema].atom.v.pint);
@@ -150,6 +155,11 @@ keksSchemaCmd( // NOLINT(misc-no-recursion)
     size_t idxSchema,
     size_t idxData)
 {
+    assert(taken != NULL);
+    assert(eachInList != NULL);
+    assert(eachInMap != NULL);
+    assert(schema != NULL);
+    assert(data != NULL);
     size_t origIdxSchema = idxSchema;
     struct KEKSSchemaErr err;
     size_t v = *taken;
@@ -629,6 +639,8 @@ KEKSSchemaValidate( // NOLINT(misc-no-recursion)
     size_t idxSchema,
     size_t idxData)
 {
+    assert(schema != NULL);
+    assert(data != NULL);
     struct KEKSSchemaErr err = (struct KEKSSchemaErr){
         .offSchema = schema->offsets[idxSchema],
         .offData = data->offsets[idxData],
index d079e2ae1a0197bb1ca3d9982af892c97fb12292b710e343d14273f91ff755c4..7ff337ae47a504c3618e4f1ff68f55637d979e8d2e162485b54fbd33e61b18ca 100644 (file)
@@ -1,3 +1,19 @@
+// KEKS -- C99 KEKS encoder implementation
+// Copyright (C) 2024-2025 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, version 3 of the License.
+//
+// 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 Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <assert.h>
 #include <stddef.h>
 #include <stdint.h>
 
@@ -6,6 +22,7 @@
 void
 keksToBE(unsigned char *buf, const size_t len, const uint64_t v)
 {
+    assert(buf != NULL);
     switch (len) {
     case 1:
         buf[0] = (v & (uint64_t)0x00000000000000FF);