hackers-painters / katana-parser

A CSS parsing library in pure C99
http://hackers-painters.github.io/katana-parser
MIT License
180 stars 40 forks source link

Crash when parsing bootstrap #17

Open msclecram opened 6 years ago

msclecram commented 6 years ago

Hi,

I'm trying to get some style properties of Gumbo nodes. I load the css included in the html file, however when I try to load Bootstrap the program crashes inside the katana parse function. It crashes inside the katanaerror funcion when it tries to print an error with text "syntax error at ".

This is the css I'm trying to load: https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css

Stack:

image

Thanks!

jonathanmcdougall commented 6 years ago

I just ran into this myself, katana crashes on any parse error. Introduced in this commit, this:

snprintf(e->message, KATANA_ERROR_MESSAGE_SIZE, "%s at %s", error,
         katanaget_text(parser->scanner));

should be

snprintf(e->message, KATANA_ERROR_MESSAGE_SIZE, "%s at %s", error,
         katanaget_text(*parser->scanner));

because parser->scanner is a yyscan_t*, and katanaget_text() expects a yyscan_t. Since all that stuff is actually a bunch of void*, everything compiles. Adding the dereference fixes the crash.

Also, first_line and last_line in the KatanaError are broken because they're not initialized in the buffer state. Adding the following to katana_scan_buffer() (katana.lex.c:3309), just before the call to katana_switch_to_buffer(), fixes it:

b->yy_buffer_status = YY_BUFFER_NEW;

// add:
b->yy_bs_lineno = 0;
b->yy_bs_column = 0;

katana_switch_to_buffer(b ,yyscanner );