mesa: Fix substitution of large shaders
authorCody Northrop <cody@lunarg.com>
Thu, 5 Jun 2014 17:27:51 +0000 (11:27 -0600)
committerCourtney Goeltzenleuchter <courtney@LunarG.com>
Tue, 10 Jun 2014 16:45:31 +0000 (10:45 -0600)
Signed-off-by: Cody Northrop <cody@lunarg.com>
Reviewed-by: Brian Paul <brianp@vmware.com>
src/mesa/main/shaderapi.c

index 28739daeb97ffaf7d0d1f941894ead44a364841d..2ec2444daa26096d973f512288379b68ac758c5a 100644 (file)
@@ -1392,7 +1392,7 @@ _mesa_LinkProgram(GLhandleARB programObj)
 static GLcharARB *
 read_shader(const char *fname)
 {
-   const int max = 50*1000;
+   int shader_size = 0;
    FILE *f = fopen(fname, "r");
    GLcharARB *buffer, *shader;
    int len;
@@ -1401,8 +1401,19 @@ read_shader(const char *fname)
       return NULL;
    }
 
-   buffer = malloc(max);
-   len = fread(buffer, 1, max, f);
+   /* allocate enough room for the entire shader */
+   fseek(f, 0, SEEK_END);
+   shader_size = ftell(f);
+   rewind(f);
+   assert(shader_size);
+
+   /* add one for terminating zero */
+   shader_size++;
+
+   buffer = malloc(shader_size);
+   assert(buffer);
+
+   len = fread(buffer, 1, shader_size, f);
    buffer[len] = 0;
 
    fclose(f);