[Mesa-dev] mesa: Fix substitution of large shaders

Submitted by Cody Northrop on June 5, 2014, 5:47 p.m.

Details

Message ID CAP90KKfBMJik2OPpmLar+6+6XqFYR_hHvVbv1WmZtvZzvh-kCQ@mail.gmail.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Cody Northrop June 5, 2014, 5:47 p.m.
The fixed size is insufficient for shaders I'm debugging.  Rather than just
bump it up, make it dynamic.

Thanks,

-C

Signed-off-by: Cody Northrop <cody@lunarg.com>
---
 src/mesa/main/shaderapi.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c
index 6f84acd..e63c124 100644
--- a/src/mesa/main/shaderapi.c
+++ b/src/mesa/main/shaderapi.c
@@ -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,16 @@  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);
+
+   buffer = malloc(shader_size);
+   assert(buffer);
+
+   len = fread(buffer, 1, shader_size, f);
    buffer[len] = 0;

    fclose(f);

Comments

On 06/05/2014 10:47 AM, Cody Northrop wrote:
> The fixed size is insufficient for shaders I'm debugging.  Rather than
> just bump it up, make it dynamic.
>
> Thanks,
>
> -C
>
> Signed-off-by: Cody Northrop <cody@lunarg.com <mailto:cody@lunarg.com>>
> ---
>   src/mesa/main/shaderapi.c | 14 +++++++++++---
>   1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c
> index 6f84acd..e63c124 100644
> --- a/src/mesa/main/shaderapi.c
> +++ b/src/mesa/main/shaderapi.c
> @@ -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,16 @@ 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);
> +
> +   buffer = malloc(shader_size);

Do you have to add one for the terminating zero?


> +   assert(buffer);
> +
> +   len = fread(buffer, 1, shader_size, f);
>      buffer[len] = 0;
>
>      fclose(f);
> --

I thought I was the only person who ever used this code!

Other than the one question above this looks alright.

Reviewed-by: Brian Paul <brianp@vmware.com>