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

Wictor Lund wlund at iki.fi
Wed Feb 27 07:58:42 UTC 2019


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 );



More information about the colm-users mailing list