improve map search error codes
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index d1be74d..4d31b69 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -2951,14 +2951,14 @@
                uReturn = QCBOR_ERR_DUPLICATE_LABEL;
                goto Done;
             }
-            /* Also try to match its type */
-            if(!MatchType(Item, pItemArray[nIndex])) {
-               uReturn = QCBOR_ERR_UNEXPECTED_TYPE;
+            if(uResult != QCBOR_SUCCESS) {
+               /* The label matches, but the data item is in error */
+               uReturn = uResult;
                goto Done;
             }
-
-            if(uResult != QCBOR_SUCCESS) {
-               uReturn = uResult;
+            if(!MatchType(Item, pItemArray[nIndex])) {
+               /* The data item is not of the type(s) requested */
+               uReturn = QCBOR_ERR_UNEXPECTED_TYPE;
                goto Done;
             }
 
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index 01a87e5..5ce62d0 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -5193,6 +5193,9 @@
 };
 #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
 
+const unsigned char not_well_formed_submod_section[] = {
+   0xa1, 0x14, 0x1f,
+};
 
 int32_t EnterMapTest()
 {
@@ -5533,6 +5536,14 @@
 
    nReturn = DecodeNestedIterate();
 
+
+   QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
+   QCBORDecode_EnterMap(&DCtx, NULL);
+   QCBORDecode_EnterMapFromMapN(&DCtx, 20);
+   if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
+      return 2500;
+   }
+
    return nReturn;
 }