Merge remote-tracking branch 'origin/topic/jsiwek/gh-167'

* origin/topic/jsiwek/gh-167:
  GH-167: improve error message for unclosed function at EOF
This commit is contained in:
Johanna Amann 2019-01-24 15:22:31 -08:00
commit 7465bceb7e
7 changed files with 57 additions and 12 deletions

View file

@ -1,4 +1,8 @@
2.6-105 | 2019-01-24 15:22:31 -0800
* GH-167: improve error message for unclosed function at EOF (Jon Siwek, Corelight)
2.6-103 | 2019-01-24 17:09:05 -0600 2.6-103 | 2019-01-24 17:09:05 -0600
* Change digest.h functions to use EVP_MD_CTX interface (Johanna Amann) * Change digest.h functions to use EVP_MD_CTX interface (Johanna Amann)

View file

@ -1 +1 @@
2.6-103 2.6-105

View file

@ -94,6 +94,9 @@
#include <string> #include <string>
extern const char* filename; // Absolute path of file currently being parsed. extern const char* filename; // Absolute path of file currently being parsed.
extern const char* last_filename; // Absolute path of last file parsed.
extern const char* last_tok_filename;
extern const char* last_last_tok_filename;
YYLTYPE GetCurrentLocation(); YYLTYPE GetCurrentLocation();
extern int yyerror(const char[]); extern int yyerror(const char[]);
@ -1723,19 +1726,27 @@ opt_deprecated:
int yyerror(const char msg[]) int yyerror(const char msg[])
{ {
char* msgbuf = new char[strlen(msg) + strlen(last_tok) + 128];
if ( last_tok[0] == '\n' )
sprintf(msgbuf, "%s, on previous line", msg);
else if ( last_tok[0] == '\0' )
sprintf(msgbuf, "%s, at end of file", msg);
else
sprintf(msgbuf, "%s, at or near \"%s\"", msg, last_tok);
if ( in_debug ) if ( in_debug )
g_curr_debug_error = copy_string(msg); g_curr_debug_error = copy_string(msg);
reporter->Error("%s", msgbuf); if ( last_tok[0] == '\n' )
reporter->Error("%s, on previous line", msg);
else if ( last_tok[0] == '\0' )
{
if ( last_filename )
reporter->Error("%s, at end of file %s", msg, last_filename);
else
reporter->Error("%s, at end of file", msg);
}
else
{
if ( last_last_tok_filename && last_tok_filename &&
! streq(last_last_tok_filename, last_tok_filename) )
reporter->Error("%s, at or near \"%s\" or end of file %s",
msg, last_tok, last_last_tok_filename);
else
reporter->Error("%s, at or near \"%s\"", msg, last_tok);
}
return 0; return 0;
} }

View file

@ -43,11 +43,16 @@ int_list if_stack;
int line_number = 1; int line_number = 1;
const char* filename = 0; // Absolute path of file currently being parsed. const char* filename = 0; // Absolute path of file currently being parsed.
const char* last_filename = 0; // Absolute path of last file parsed.
static const char* last_id_tok = 0; static const char* last_id_tok = 0;
const char* last_tok_filename = 0;
const char* last_last_tok_filename = 0;
char last_tok[128]; char last_tok[128];
#define YY_USER_ACTION strncpy(last_tok, yytext, sizeof(last_tok) - 1); #define YY_USER_ACTION strncpy(last_tok, yytext, sizeof(last_tok) - 1); \
last_last_tok_filename = last_tok_filename; \
last_tok_filename = ::filename;
#define YY_USER_INIT last_tok[0] = '\0'; #define YY_USER_INIT last_tok[0] = '\0';
// We define our own YY_INPUT because we want to trap the case where // We define our own YY_INPUT because we want to trap the case where
@ -899,6 +904,8 @@ void add_to_name_list(char* s, char delim, name_list& nl)
int yywrap() int yywrap()
{ {
last_filename = ::filename;
if ( reporter->Errors() > 0 ) if ( reporter->Errors() > 0 )
return 1; return 1;

View file

@ -0,0 +1 @@
error: syntax error, at end of file ./a.bro

View file

@ -0,0 +1 @@
error in ./b.bro, line 1: syntax error, at or near "module" or end of file ./a.bro

View file

@ -0,0 +1,21 @@
# @TEST-EXEC-FAIL: bro -b a.bro >output1 2>&1
# @TEST-EXEC-FAIL: bro -b a.bro b.bro >output2 2>&1
# @TEST-EXEC: btest-diff output1
# @TEST-EXEC: btest-diff output2
@TEST-START-FILE a.bro
module A;
event bro_init()
{
print "a";
@TEST-END-FILE
@TEST-START-FILE b.bro
module B;
event bro_init()
{
print "b";
}
@TEST-END-FILE