core(persistence): fix types format handling
authorAlexander Alekhin <alexander.a.alekhin@gmail.com>
Tue, 29 Jun 2021 09:00:10 +0000 (09:00 +0000)
committerAlexander Alekhin <alexander.a.alekhin@gmail.com>
Tue, 29 Jun 2021 21:54:52 +0000 (21:54 +0000)
partial backport of 4eac198270783d8924ed26ecfb82f8aa54d9e67d

modules/core/src/persistence.cpp

index 40d1cdf..7e9d107 100644 (file)
@@ -6,6 +6,8 @@
 #include "precomp.hpp"
 #include "persistence.hpp"
 
+using namespace cv;
+
 char* icv_itoa( int _val, char* buffer, int /*radix*/ )
 {
     const int radix = 10;
@@ -519,12 +521,16 @@ static const char symbols[9] = "ucwsifdr";
 
 char icvTypeSymbol(int depth)
 {
-    CV_Assert(depth >=0 && depth < 9);
+    CV_StaticAssert(CV_64F == 6, "");
+    CV_Assert(depth >=0 && depth <= CV_64F);
+    CV_CheckDepth(depth, depth >=0 && depth <= CV_64F, "");
     return symbols[depth];
 }
 
 static int icvSymbolToType(char c)
 {
+    if (c == 'r')
+        return CV_SEQ_ELTYPE_PTR;
     const char* pos = strchr( symbols, c );
     if( !pos )
         CV_Error( CV_StsBadArg, "Invalid data type specification" );
@@ -618,8 +624,12 @@ int icvCalcStructSize( const char* dt, int initial_size )
 {
     int size = icvCalcElemSize( dt, initial_size );
     size_t elem_max_size = 0;
-    for ( const char * type = dt; *type != '\0'; type++ ) {
-        switch ( *type )
+    for ( const char * type = dt; *type != '\0'; type++ )
+    {
+        char v = *type;
+        if (v >= '0' && v <= '9')
+            continue;  // skip vector size
+        switch (v)
         {
         case 'u': { elem_max_size = std::max( elem_max_size, sizeof(uchar ) ); break; }
         case 'c': { elem_max_size = std::max( elem_max_size, sizeof(schar ) ); break; }
@@ -628,7 +638,8 @@ int icvCalcStructSize( const char* dt, int initial_size )
         case 'i': { elem_max_size = std::max( elem_max_size, sizeof(int   ) ); break; }
         case 'f': { elem_max_size = std::max( elem_max_size, sizeof(float ) ); break; }
         case 'd': { elem_max_size = std::max( elem_max_size, sizeof(double) ); break; }
-        default: break;
+        default:
+            CV_Error_(Error::StsNotImplemented, ("Unknown type identifier: '%c' in '%s'", (char)(*type), dt));
         }
     }
     size = cvAlign( size, static_cast<int>(elem_max_size) );