On Thursday 18 September 2008 08:42:46 [EMAIL PROTECTED] wrote:

> Author: julianalbo
> Date: Thu Sep 18 08:42:43 2008
> New Revision: 31230
>
> Modified:
>    trunk/compilers/imcc/pcc.c
>
> Log:
> miscellaneous fixes
>
> Modified: trunk/compilers/imcc/pcc.c
> ===========================================================================
>=== --- trunk/compilers/imcc/pcc.c     (original)
> +++ trunk/compilers/imcc/pcc.c        Thu Sep 18 08:42:43 2008
> @@ -211,7 +211,16 @@
>      int i, flags;
>      char s[16];
>      SymReg ** const regs  = mem_allocate_n_zeroed_typed(n + 1, SymReg *);
> -    char    * buf         = mem_allocate_n_typed(5*n+1, char *);
> +    /* Assumptions:
> +     * Flags has no more than 3 hex digits
> +     * Plus 0x and , gives 6 char for arg
> +     * 4 more for: "( , )", and
> +     * 1 more for C string 0 delimiter
> +     * Last item has no , but we can forget that to avoid
> +     * to have to check for 0 args.
> +     */
> +    unsigned int bufsize = 6 * n + 5;
> +    char * buf = mem_allocate_n_typed(bufsize, char);
>
>      strcpy(buf, "\"(");
>      for (i = 0; i < n; i++) {

The comment helps, but defining these magic numbers as magic constants might 
be even clearer (but please keep the comment).

> @@ -252,7 +261,8 @@
>
>          if (i < n - 1)
>              strcat(s, ",");
> -        strcat(buf, s);         /* XXX check avail len */
> +        PARROT_ASSERT(strlen(buf) + strlen(s) < bufsize);
> +        strcat(buf, s);
>      }
>
>      strcat(buf, ")\"");

That only helps with debug builds; if there are possible inputs we won't 
discover in our testing, we might as well make this an unconditional test and 
throw an exception if something goes wrong here.  IMCC is user-facing, so I 
want to be paranoid.

-- c

Reply via email to