[MOAB-dev] r1586 - MOAB/trunk

kraftche at mcs.anl.gov kraftche at mcs.anl.gov
Tue Feb 5 12:40:27 CST 2008


Author: kraftche
Date: 2008-02-05 12:40:26 -0600 (Tue, 05 Feb 2008)
New Revision: 1586

Modified:
   MOAB/trunk/TagInfo.cpp
   MOAB/trunk/TagInfo.hpp
   MOAB/trunk/TagServer.cpp
   MOAB/trunk/TagServer.hpp
Log:
When setting variable-length tag data, check that all lengths 
are mulitples of the size of the data type.
 


Modified: MOAB/trunk/TagInfo.cpp
===================================================================
--- MOAB/trunk/TagInfo.cpp	2008-02-05 17:41:18 UTC (rev 1585)
+++ MOAB/trunk/TagInfo.cpp	2008-02-05 18:40:26 UTC (rev 1586)
@@ -92,3 +92,18 @@
   free( mMeshValue );
   mMeshValueSize = 0;
 }
+
+  
+    // Check that all lengths are valid multiples of the type size.
+    // Returns true if all lengths are valid, false othersize.
+bool TagInfo::check_valid_sizes( const int* sizes, int num_sizes ) const
+{
+  unsigned sum = 0;
+  const unsigned size = size_from_data_type( get_data_type() );
+  if (size == 1)
+    return true;
+  for (int i = 0; i < num_sizes; ++i)
+    sum |= ((unsigned)sizes[i]) % size;
+  return (sum == 0);
+}
+

Modified: MOAB/trunk/TagInfo.hpp
===================================================================
--- MOAB/trunk/TagInfo.hpp	2008-02-05 17:41:18 UTC (rev 1585)
+++ MOAB/trunk/TagInfo.hpp	2008-02-05 18:40:26 UTC (rev 1586)
@@ -76,6 +76,10 @@
   
   bool is_valid() const { return isValid; }
   void invalidate();
+  
+    // Check that all lengths are valid multiples of the type size.
+    // Returns true if all lengths are valid, false othersize.
+  bool check_valid_sizes( const int* sizes, int num_sizes ) const;
 
 private:    
 

Modified: MOAB/trunk/TagServer.cpp
===================================================================
--- MOAB/trunk/TagServer.cpp	2008-02-05 17:41:18 UTC (rev 1585)
+++ MOAB/trunk/TagServer.cpp	2008-02-05 18:40:26 UTC (rev 1586)
@@ -104,6 +104,8 @@
   }
   else if (data_size != MB_VARIABLE_LENGTH) 
     default_value_size = data_size;
+  else if (default_value_size % TagInfo::size_from_data_type(data_type))
+    return MB_INVALID_SIZE;
 
     // Check if name is already in use
     // if so, pass back the existing tag handle.
@@ -260,7 +262,7 @@
     return MB_TAG_NOT_FOUND;
   
   if (info->get_size() == MB_VARIABLE_LENGTH) {
-    if (!size)
+    if (!size || !info->check_valid_sizes( &size, 1 ))
       return MB_INVALID_SIZE;
   }
   else if (PROP_FROM_TAG_HANDLE(tag_handle) == MB_TAG_BIT)
@@ -279,12 +281,14 @@
                                  const void* data )
 {
   const MBTagId tag_id = ID_FROM_TAG_HANDLE(tag_handle);
-  switch (PROP_FROM_TAG_HANDLE(tag_handle)) {
+  const MBTagType tag_type = PROP_FROM_TAG_HANDLE(tag_handle);
+  const TagInfo* tag_info;
+  switch (tag_type) {
     case MB_TAG_DENSE:
-      if (tag_id > mTagTable[MB_TAG_DENSE].size())
+      if (!(tag_info = get_tag_info( tag_id, tag_type )))
         return MB_TAG_NOT_FOUND;
       return sequenceManager->set_tag_data( tag_id, entity_handles, num_entities, 
-                           data, mTagTable[MB_TAG_DENSE][tag_id-1].default_value() );
+                                            data, tag_info->default_value() );
   
     case MB_TAG_SPARSE:
       return mSparseData->set_data( tag_id, entity_handles, num_entities, data );
@@ -305,12 +309,14 @@
                                  const void* data )
 {
   const MBTagId tag_id = ID_FROM_TAG_HANDLE(tag_handle);
-  switch (PROP_FROM_TAG_HANDLE(tag_handle)) {
+  const MBTagType tag_type = PROP_FROM_TAG_HANDLE(tag_handle);
+  const TagInfo* tag_info;
+  switch (tag_type) {
     case MB_TAG_DENSE:
-      if (tag_id > mTagTable[MB_TAG_DENSE].size())
+      if (!(tag_info = get_tag_info( tag_id, tag_type )))
         return MB_TAG_NOT_FOUND;
       return sequenceManager->set_tag_data( tag_id, entity_handles, 
-                           data, mTagTable[MB_TAG_DENSE][tag_id-1].default_value() );
+                                            data, tag_info->default_value() );
   
     case MB_TAG_SPARSE:
       return mSparseData->set_data( tag_id, entity_handles, data );
@@ -333,12 +339,19 @@
                                  const int* lengths )
 {
   const MBTagId tag_id = ID_FROM_TAG_HANDLE(tag_handle);
-  switch (PROP_FROM_TAG_HANDLE(tag_handle)) {
+  const MBTagType tag_type = PROP_FROM_TAG_HANDLE(tag_handle);
+  const TagInfo* tag_info = get_tag_info( tag_id, tag_type );
+  if (!tag_info)
+    return MB_TAG_NOT_FOUND;
+    
+  if (tag_info->get_size() == MB_VARIABLE_LENGTH &&
+      !tag_info->check_valid_sizes( lengths, num_entities ))
+    return MB_INVALID_SIZE;
+    
+  switch (tag_type) {
     case MB_TAG_DENSE:
-      if (tag_id > mTagTable[MB_TAG_DENSE].size())
-        return MB_TAG_NOT_FOUND;
       return sequenceManager->set_tag_data( tag_id, entity_handles, num_entities, 
-                   data, lengths, mTagTable[MB_TAG_DENSE][tag_id-1].default_value() );
+                   data, lengths, tag_info->default_value() );
   
     case MB_TAG_SPARSE:
       return mSparseData->set_data( tag_id, entity_handles, num_entities, data, lengths );
@@ -360,12 +373,19 @@
                                  const int* lengths )
 {
   const MBTagId tag_id = ID_FROM_TAG_HANDLE(tag_handle);
-  switch (PROP_FROM_TAG_HANDLE(tag_handle)) {
+  const MBTagType tag_type = PROP_FROM_TAG_HANDLE(tag_handle);
+  const TagInfo* tag_info = get_tag_info( tag_id, tag_type );
+  if (!tag_info)
+    return MB_TAG_NOT_FOUND;
+    
+  if (tag_info->get_size() == MB_VARIABLE_LENGTH &&
+      !tag_info->check_valid_sizes( lengths, entity_handles.size() ))
+    return MB_INVALID_SIZE;
+    
+  switch (tag_type) {
     case MB_TAG_DENSE:
-      if (tag_id > mTagTable[MB_TAG_DENSE].size())
-        return MB_TAG_NOT_FOUND;
       return sequenceManager->set_tag_data( tag_id, entity_handles, 
-                      data, lengths, mTagTable[MB_TAG_DENSE][tag_id-1].default_value() );
+                      data, lengths, tag_info->default_value() );
   
     case MB_TAG_SPARSE:
       return mSparseData->set_data( tag_id, entity_handles, data, lengths );

Modified: MOAB/trunk/TagServer.hpp
===================================================================
--- MOAB/trunk/TagServer.hpp	2008-02-05 17:41:18 UTC (rev 1585)
+++ MOAB/trunk/TagServer.hpp	2008-02-05 18:40:26 UTC (rev 1586)
@@ -307,6 +307,8 @@
   const TagInfo* get_tag_info(const char *tag_name ) const;
   const TagInfo* get_tag_info( MBTag tag_handle ) const;
   TagInfo* get_tag_info( MBTag tag_handle );
+  const TagInfo* get_tag_info( MBTagId id, MBTagType storage ) const;
+  TagInfo* get_tag_info( MBTagId id, MBTagType storage );
   
   unsigned long get_memory_use( MBTag tag_handle ) const;
   
@@ -338,20 +340,26 @@
   return handle ? get_tag_info( handle ) : 0;
 }
 
-inline const TagInfo* TagServer::get_tag_info( MBTag tag_handle ) const
+inline const TagInfo* TagServer::get_tag_info( MBTag tag ) const
 {
-  const MBTagId id = ID_FROM_TAG_HANDLE( tag_handle );
-  const MBTagType type = PROP_FROM_TAG_HANDLE( tag_handle );
+  return get_tag_info( ID_FROM_TAG_HANDLE(tag), PROP_FROM_TAG_HANDLE(tag) );
+}
+
+inline TagInfo* TagServer::get_tag_info( MBTag tag )
+{
+  return get_tag_info( ID_FROM_TAG_HANDLE(tag), PROP_FROM_TAG_HANDLE(tag) );
+}
+
+inline const TagInfo* TagServer::get_tag_info( MBTagId id, MBTagType type ) const
+{
   if (id <= mTagTable[type].size() && mTagTable[type][id-1].is_valid())
     return &mTagTable[type][id-1];
   else
     return NULL;
 }
 
-inline TagInfo* TagServer::get_tag_info( MBTag tag_handle )
+inline TagInfo* TagServer::get_tag_info( MBTagId id, MBTagType type )
 {
-  const MBTagId id = ID_FROM_TAG_HANDLE( tag_handle );
-  const MBTagType type = PROP_FROM_TAG_HANDLE( tag_handle );
   if (id <= mTagTable[type].size() && mTagTable[type][id-1].is_valid())
     return &mTagTable[type][id-1];
   else




More information about the moab-dev mailing list