u32 version;
 };
 
+#define tri int
+#define TRI_TRUE 1
+#define TRI_NONE 0
+#define TRI_FALSE -1
+
 /* audit callback for unpack fields */
 static void audit_cb(struct audit_buffer *ab, void *va)
 {
        return false;
 }
 
-static size_t unpack_array(struct aa_ext *e, const char *name)
+static tri unpack_array(struct aa_ext *e, const char *name, u16 *size)
 {
        void *pos = e->pos;
 
        if (unpack_nameX(e, AA_ARRAY, name)) {
-               int size;
                if (!inbounds(e, sizeof(u16)))
                        goto fail;
-               size = (int)le16_to_cpu(get_unaligned((__le16 *) e->pos));
+               *size = le16_to_cpu(get_unaligned((__le16 *) e->pos));
                e->pos += sizeof(u16);
-               return size;
+               return TRI_TRUE;
        }
 
+       return TRI_NONE;
 fail:
        e->pos = pos;
-       return 0;
+       return TRI_FALSE;
 }
 
 static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name)
 
        /* exec table is optional */
        if (unpack_nameX(e, AA_STRUCT, "xtable")) {
-               int i, size;
+               u16 size;
+               int i;
 
-               size = unpack_array(e, NULL);
-               /* currently 2^24 bits entries 0-3 */
-               if (size > (1 << 24))
+               if (unpack_array(e, NULL, &size) != TRI_TRUE ||
+                   size > (1 << 24))
+                 /* currently 2^24 bits entries 0-3 */
                        goto fail;
                table = kcalloc(size, sizeof(char *), GFP_KERNEL);
                if (!table)
        void *pos = e->pos;
 
        if (unpack_nameX(e, AA_STRUCT, "xattrs")) {
-               int i, size;
+               u16 size;
+               int i;
 
-               size = unpack_array(e, NULL);
+               if (unpack_array(e, NULL, &size) != TRI_TRUE)
+                       goto fail;
                profile->xattr_count = size;
                profile->xattrs = kcalloc(size, sizeof(char *), GFP_KERNEL);
                if (!profile->xattrs)
 static bool unpack_secmark(struct aa_ext *e, struct aa_profile *profile)
 {
        void *pos = e->pos;
-       int i, size;
+       u16 size;
+       int i;
 
        if (unpack_nameX(e, AA_STRUCT, "secmark")) {
-               size = unpack_array(e, NULL);
+               if (unpack_array(e, NULL, &size) != TRI_TRUE)
+                       goto fail;
 
                profile->secmark = kcalloc(size, sizeof(struct aa_secmark),
                                           GFP_KERNEL);
 
        /* rlimits are optional */
        if (unpack_nameX(e, AA_STRUCT, "rlimits")) {
-               int i, size;
+               u16 size;
+               int i;
                u32 tmp = 0;
                if (!unpack_u32(e, &tmp, NULL))
                        goto fail;
                profile->rlimits.mask = tmp;
 
-               size = unpack_array(e, NULL);
-               if (size > RLIM_NLIMITS)
+               if (unpack_array(e, NULL, &size) != TRI_TRUE ||
+                   size > RLIM_NLIMITS)
                        goto fail;
                for (i = 0; i < size; i++) {
                        u64 tmp2 = 0;
 
 
        puf->e->pos += TEST_ARRAY_BUF_OFFSET;
 
-       array_size = unpack_array(puf->e, NULL);
-
+       KUNIT_EXPECT_EQ(test, unpack_array(puf->e, NULL, &array_size),
+                       TRI_TRUE);
        KUNIT_EXPECT_EQ(test, array_size, (u16)TEST_ARRAY_SIZE);
        KUNIT_EXPECT_PTR_EQ(test, puf->e->pos,
                puf->e->start + TEST_ARRAY_BUF_OFFSET + sizeof(u16) + 1);
 
        puf->e->pos += TEST_NAMED_ARRAY_BUF_OFFSET;
 
-       array_size = unpack_array(puf->e, name);
-
+       KUNIT_EXPECT_EQ(test, unpack_array(puf->e, name, &array_size),
+                       TRI_TRUE);
        KUNIT_EXPECT_EQ(test, array_size, (u16)TEST_ARRAY_SIZE);
        KUNIT_EXPECT_PTR_EQ(test, puf->e->pos,
                puf->e->start + TEST_ARRAY_BUF_OFFSET + sizeof(u16) + 1);
        puf->e->pos += TEST_NAMED_ARRAY_BUF_OFFSET;
        puf->e->end = puf->e->start + TEST_ARRAY_BUF_OFFSET + sizeof(u16);
 
-       array_size = unpack_array(puf->e, name);
-
+       KUNIT_EXPECT_EQ(test, unpack_array(puf->e, name, &array_size),
+                       TRI_TRUE);
        KUNIT_EXPECT_EQ(test, array_size, 0);
        KUNIT_EXPECT_PTR_EQ(test, puf->e->pos,
                puf->e->start + TEST_NAMED_ARRAY_BUF_OFFSET);