fix crash invalid input
[convexer.git] / cxr / cxr.h
index 9f11096925ab8b105784ec411a5ced306951b39b..a6643131f6f26656657e7e6441c9311c96531944 100644 (file)
--- a/cxr/cxr.h
+++ b/cxr/cxr.h
@@ -1,5 +1,5 @@
 /*
-                              CONVEXER v0.9
+                              CONVEXER v0.91
 
                A GNU/Linux-first Source1 Hammer replacement
                     built with Blender, for mapmakers
@@ -13,7 +13,8 @@
       - Compile models and model groups easily
       - It runs at an ok speed!
       - Light patch BSP files; remove unwanted realtime effects
-      - Fastest VTF compressor (thanks to Richgel999 and stb)
+      - Bestest VTF compressor (thanks to Richgel999 and stb)
+      - Pack content automatically
 
    Program structure:
 
@@ -279,7 +280,8 @@ enum cxr_soliderr
    k_soliderr_degenerate_implicit,
    k_soliderr_non_coplanar_vertices,
    k_soliderr_non_convex_poly,
-   k_soliderr_bad_result
+   k_soliderr_bad_result,
+   k_soliderr_invalid_input
 };
 
 /*
@@ -1335,10 +1337,10 @@ static void cxr_link_manifold(
 
    int init_reverse = 0;
    int unique_edge_count = 0;
-   int discard_splits = 1;
    
    /* Try remove splitting faces first */
    {
+      int split_total = 0;
       for( int j=0; j<solid->count; j++ )
       {
          cxr_polygon *poly = &mesh->polys[ solid_buffer[solid->start+j] ];
@@ -1359,19 +1361,16 @@ static void cxr_link_manifold(
          }
          
          if( interior_count < poly->loop_total-1 )
+         {
+            split_total ++;
             continue;
+         }
          
          temp_solid[ temp_solid_len ++ ] = solid_buffer[solid->start+j];
       }
 
-      if( temp_solid_len < 3 )
+      if( temp_solid_len < 3 || (split_total & 0x2) /* unkown reasons */ )
       {
-         /* Revert back to normal */
-         free( temp_solid );
-
-         temp_solid = &solid_buffer[ solid->start ];
-         temp_solid_len = solid->count;
-         discard_splits = 0;
       }
       else
       {
@@ -1382,8 +1381,7 @@ static void cxr_link_manifold(
          solid->count = temp_solid_len;
       }
 
-      if( discard_splits )
-         free( temp_solid );
+      free( temp_solid );
    }
 
    for( int j=0; j<solid->count; j++ )
@@ -2281,6 +2279,16 @@ CXR_API void cxr_free_tri_mesh( cxr_tri_mesh *mesh )
 
 CXR_API cxr_world *cxr_decompose( cxr_static_mesh *src, i32 *perrcode )
 {
+   /* Make sure data is in the mesh and isn't empty */
+   if( !src->edge_count || !src->loop_count || !src->poly_count )
+   {
+      cxr_log( "Error %d\n", k_soliderr_invalid_input );
+      if( perrcode )
+         *perrcode = k_soliderr_invalid_input;
+
+      return NULL;
+   }
+
    u32 error = 0x00;
    cxr_world *world = malloc( sizeof(*world) );