[gl2ps] gl2ps bug in gl2psParseStipplePattern

Christophe Geuzaine christophe.geuzaine at case.edu
Mon Oct 30 18:01:22 CET 2006


Fletcher, Ben wrote:
> Hi,
> 
> I have encountered a problem with gl2psParseStipplePattern.
> 
> If the stipple pattern is 0xaaaa, then i will finish the for loop listed
> below being equal to 5, when its maximum value should be only 4.
> 
>   /* compute the on/off pixel sequence (since the PostScript
>      specification allows for at most 11 elements in the on/off array,
>      we limit ourselves to 5 couples of on/off states) */
>   n = 0;
>   for(i = 0; i < 5; i++){
>     while(n < 16 && !tmp[n]){ off[i]++; n++; }
>     while(n < 16 && tmp[n]){ on[i]++; n++; }
>     if(n >= 15) break;
>   }
> 
> This causes the following loop to read outside the bounds of the on[] and
> off[] arrays, and leads to *nb=12, which causes an invalid postscript output
> 
>   /* store the on/off array from right to left, starting with off
>      pixels (the longest possible array is: [on4 off4 on3 off3 on2
>      off2 on1 off1 on0 off0]) */
>   *nb = 0;
>   for(n = i; n >= 0; n--){
>     array[(*nb)++] = factor * on[n];
>     array[(*nb)++] = factor * off[n];
>   }
> 


Hi Ben - Thanks for your message: this is indeed a bug. I think the 
following routine is better: could you give it a try?


static void gl2psParseStipplePattern(GLushort pattern, GLint factor,
                                      int *nb, int array[10])
{
   int i, n;
   int on[8] = {0, 0, 0, 0, 0, 0, 0, 0};
   int off[8] = {0, 0, 0, 0, 0, 0, 0, 0};
   char tmp[16];

   /* extract the 16 bits from the OpenGL stipple pattern */
   for(n = 15; n >= 0; n--){
     tmp[n] = (char)(pattern & 0x01);
     pattern >>= 1;
   }
   /* compute the on/off pixel sequence */
   n = 0;
   for(i = 0; i < 8; i++){
     while(n < 16 && !tmp[n]){ off[i]++; n++; }
     while(n < 16 && tmp[n]){ on[i]++; n++; }
     if(n >= 15){ i++; break; }
   }

   /* store the on/off array from right to left, starting with off
      pixels. The PostScript specification allows for at most 11
      elements in the on/off array, so we limit ourselves to 5 on/off
      couples (our longest possible array is thus [on4 off4 on3 off3
      on2 off2 on1 off1 on0 off0]) */
   *nb = 0;
   for(n = i - 1; n >= 0; n--){
     array[(*nb)++] = factor * on[n];
     array[(*nb)++] = factor * off[n];
     if(*nb == 10) break;
   }
}




> 
> ben
> 
> _______________________________________________
> gl2ps mailing list
> gl2ps at geuz.org
> http://www.geuz.org/mailman/listinfo/gl2ps
> 


-- 
Christophe Geuzaine
Assistant Professor, Case Western Reserve University, Mathematics
http://www.case.edu/artsci/math/geuzaine