<div dir="ltr"><div>Whoops, yes, you're right.  ftell() of SEEK_END and strlen() are returning the same value, which does not include the terminating zero.  Updated patch below.<br><br></div><div>We use this quite a bit, much faster than editing large apitrace files<br>
<br></div><div>Thanks,<br><br></div><div>-C<br><br><br><span style="font-family:courier new,monospace">Signed-off-by: Cody Northrop <<a href="mailto:cody@lunarg.com">cody@lunarg.com</a>><br>---<br> src/mesa/main/shaderapi.c | 17 ++++++++++++++---<br>
 1 file changed, 14 insertions(+), 3 deletions(-)<br><br>diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c<br>index 6f84acd..4048ddf 100644<br>--- a/src/mesa/main/shaderapi.c<br>+++ b/src/mesa/main/shaderapi.c<br>
@@ -1392,7 +1392,7 @@ _mesa_LinkProgram(GLhandleARB programObj)<br> static GLcharARB *<br> read_shader(const char *fname)<br> {<br>-   const int max = 50*1000;<br>+   int shader_size = 0;<br>    FILE *f = fopen(fname, "r");<br>
    GLcharARB *buffer, *shader;<br>    int len;<br>@@ -1401,8 +1401,19 @@ read_shader(const char *fname)<br>       return NULL;<br>    }<br> <br>-   buffer = malloc(max);<br>-   len = fread(buffer, 1, max, f);<br>+   /* allocate enough room for the entire shader */<br>
+   fseek(f, 0, SEEK_END);<br>+   shader_size = ftell(f);<br>+   rewind(f);<br>+   assert(shader_size);<br>+<br>+   /* add one for terminating zero */<br>+   shader_size++;<br>+<br>+   buffer = malloc(shader_size);<br>+   assert(buffer);<br>
+<br>+   len = fread(buffer, 1, shader_size, f);<br>    buffer[len] = 0;<br> <br>    fclose(f);<br>-- <br>1.8.3.2</span><br><br><br></div><div><br></div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Thu, Jun 5, 2014 at 7:13 PM, Brian Paul <span dir="ltr"><<a href="mailto:brianp@vmware.com" target="_blank">brianp@vmware.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="">On 06/05/2014 10:47 AM, Cody Northrop wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">
The fixed size is insufficient for shaders I'm debugging.  Rather than<br>
just bump it up, make it dynamic.<br>
<br>
Thanks,<br>
<br>
-C<br>
<br></div>
Signed-off-by: Cody Northrop <<a href="mailto:cody@lunarg.com" target="_blank">cody@lunarg.com</a> <mailto:<a href="mailto:cody@lunarg.com" target="_blank">cody@lunarg.com</a>>><div><div class="h5"><br>
---<br>
  src/mesa/main/shaderapi.c | 14 +++++++++++---<br>
  1 file changed, 11 insertions(+), 3 deletions(-)<br>
<br>
diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c<br>
index 6f84acd..e63c124 100644<br>
--- a/src/mesa/main/shaderapi.c<br>
+++ b/src/mesa/main/shaderapi.c<br>
@@ -1392,7 +1392,7 @@ _mesa_LinkProgram(GLhandleARB programObj)<br>
  static GLcharARB *<br>
  read_shader(const char *fname)<br>
  {<br>
-   const int max = 50*1000;<br>
+   int shader_size = 0;<br>
     FILE *f = fopen(fname, "r");<br>
     GLcharARB *buffer, *shader;<br>
     int len;<br>
@@ -1401,8 +1401,16 @@ read_shader(const char *fname)<br>
        return NULL;<br>
     }<br>
<br>
-   buffer = malloc(max);<br>
-   len = fread(buffer, 1, max, f);<br>
+   /* allocate enough room for the entire shader */<br>
+   fseek(f, 0, SEEK_END);<br>
+   shader_size = ftell(f);<br>
+   rewind(f);<br>
+   assert(shader_size);<br>
+<br>
+   buffer = malloc(shader_size);<br>
</div></div></blockquote>
<br>
Do you have to add one for the terminating zero?<div class=""><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+   assert(buffer);<br>
+<br>
+   len = fread(buffer, 1, shader_size, f);<br>
     buffer[len] = 0;<br>
<br>
     fclose(f);<br>
--<br>
</blockquote>
<br></div>
I thought I was the only person who ever used this code!<br>
<br>
Other than the one question above this looks alright.<br>
<br>
Reviewed-by: Brian Paul <<a href="mailto:brianp@vmware.com" target="_blank">brianp@vmware.com</a>><br>
<br>
______________________________<u></u>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/<u></u>mailman/listinfo/mesa-dev</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr"><div><font color="#999999"><font face="trebuchet ms, sans-serif"> Cody Northrop<br> Graphics Software Engineer<br> LunarG, Inc.- 3D Driver Innovations</font><font face="trebuchet ms, sans-serif" style="font-size:small"><br>
 Email: <a href="mailto:cody@lunarg.com" target="_blank">cody@lunarg.com</a><br> Website: <a href="http://www.lunarg.com/" target="_blank">http://www.lunarg.com</a></font></font></div></div>
</div>