Several preshader fixes and static analysis tweaks.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 25 Oct 2011 12:01:34 -0400
changeset 1063 b10281d6eb45
parent 1062 ea5c89942bd3
child 1064 d3f0f4cf84f2
Several preshader fixes and static analysis tweaks.
mojoshader_effects.c
mojoshader_opengl.c
--- a/mojoshader_effects.c	Tue Oct 25 02:24:45 2011 -0400
+++ b/mojoshader_effects.c	Tue Oct 25 12:01:34 2011 -0400
@@ -27,11 +27,11 @@
         memset(temps, '\0', sizeof (double) * preshader->temp_count);
     } // if
 
-    double dst[4];
-    double src[3][4];
-    const double *src0 = src[0];
-    const double *src1 = src[1];
-    const double *src2 = src[2];
+    double dst[4] = { 0, 0, 0, 0 };
+    double src[3][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
+    const double *src0 = &src[0][0];
+    const double *src1 = &src[1][0];
+    const double *src2 = &src[2][0];
 
     MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
     int instit;
@@ -43,6 +43,9 @@
         const int elemsbytes = sizeof (double) * elems;
         const int isscalarop = (inst->opcode >= scalarstart);
 
+        assert(elems >= 0);
+        assert(elems <= 4);
+
         // load up our operands...
         int opiter, elemiter;
         for (opiter = 0; opiter < inst->operand_count-1; opiter++, operand++)
@@ -54,8 +57,9 @@
                 case MOJOSHADER_PRESHADEROPERAND_LITERAL:
                 {
                     const double *lit = &preshader->literals[index];
+                    assert((index + elems) <= preshader->literal_count);
                     if (!isscalar)
-                        memcpy(src[opiter], lit, elemsbytes);
+                        memcpy(&src[opiter][0], lit, elemsbytes);
                     else
                     {
                         const double val = *lit;
@@ -88,15 +92,18 @@
                     break;
 
                 case MOJOSHADER_PRESHADEROPERAND_TEMP:
-                    if (isscalar)
-                        src[opiter][0] = temps[index];
-                    else
-                        memcpy(src[opiter], temps + index, elemsbytes);
+                    if (temps != NULL)
+                    {
+                        if (isscalar)
+                            src[opiter][0] = temps[index];
+                        else
+                            memcpy(src[opiter], temps + index, elemsbytes);
+                    } // if
                     break;
 
                 default:
                     assert(0 && "unexpected preshader operand type.");
-                    break;
+                    return;
             } // switch
         } // for
 
@@ -170,7 +177,11 @@
 
         // Figure out where dst wants to be stored.
         if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
+        {
+            assert(preshader->temp_count >=
+                    operand->index + (elemsbytes / sizeof (double)));
             memcpy(temps + operand->index, dst, elemsbytes);
+        } // if
         else
         {
             assert(operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT);
--- a/mojoshader_opengl.c	Tue Oct 25 02:24:45 2011 -0400
+++ b/mojoshader_opengl.c	Tue Oct 25 12:01:34 2011 -0400
@@ -1429,7 +1429,7 @@
 
         if (largest > 0)
         {
-            const size_t len = largest * sizeof (GLfloat);
+            const size_t len = largest * sizeof (GLfloat) * 4;
             GLfloat *buf = (GLfloat *) Malloc(len);
             if (buf == NULL)
                 return 0;