[colm-users] Off by one error in pdarun.c

Adrian Thurston thurston at colm.net
Thu Feb 28 11:28:54 UTC 2019


Hi Wictor,

Strings in colm are not normally null terminated. Probably the error 
you're seeing originates from when the string is printed. Can you share 
some code that exhibits the problem?

On 2019-02-27 14:58, Wictor Lund wrote:
> Hi colm users!
> 
> I started getting some garbage after the parse error message so I
> investigated and found that the error string is cut off just before the 
> '\0'
> character.
> 
> I made a patch that addresses this and  uses snprintf instead of 
> sprintf.
> 
> --
> Wictor Lund
> 
> diff --git a/src/pdarun.c b/src/pdarun.c
> index ab0de7eb..3914bc65 100644
> --- a/src/pdarun.c
> +++ b/src/pdarun.c
> @@ -436,9 +436,15 @@ static void report_parse_error( program_t *prg,
> tree_t **sp, struct pda_run *pda
> 
> 
>                 if ( name == 0 )
>                         name = "<input>";
> -               char *formatted = malloc( strlen( name ) + 128 );
> -               sprintf( formatted, "%s:%ld:%ld: parse error", name,
> line, column );
> -               error_head = string_alloc_full( prg, formatted,
> strlen(formatted) );
> +               size_t formatted_s = strlen( name ) + 128;
> +               char *formatted = malloc( formatted_s );
> +               size_t fmt_s = snprintf( formatted, formatted_s,
> +                                        "%s:%ld:%ld: parse error",
> +                                        name, line, column );
> +               if ( fmt_s >= formatted_s ) {
> +                       fmt_s = formatted_s - 1;
> +               }
> +               error_head = string_alloc_full( prg, formatted, fmt_s + 
> 1 );
>                 free( formatted );
> 
>                 error_head->location = location_allocate( prg );
> 
> _______________________________________________
> colm-users mailing list
> colm-users at colm.net
> http://www.colm.net/cgi-bin/mailman/listinfo/colm-users



More information about the colm-users mailing list