Fix possible memory error with long selections
authorTeemu Murtola <teemu.murtola@gmail.com>
Sat, 3 Dec 2016 08:50:56 +0000 (10:50 +0200)
committerMark Abraham <mark.j.abraham@gmail.com>
Thu, 8 Dec 2016 16:21:56 +0000 (17:21 +0100)
If a selection was more than 1000 characters long and there was a
whitespace exactly at the 1000 point, a buffer overflow could occur.
Replaced the buffer with std::string, simplifying the code
significantly.  Update the generated code to use a newer flex, which
also removes the need for some suppressions.

Should fix #2086.

Change-Id: I56513bcf5ee99f05ce144461740d0f868be10186

src/gromacs/CMakeLists.txt
src/gromacs/selection/scanner.cpp
src/gromacs/selection/scanner.l
src/gromacs/selection/scanner.patch
src/gromacs/selection/scanner_flex.h
src/gromacs/selection/scanner_internal.cpp
src/gromacs/selection/scanner_internal.h

index 8b7fe3fac88a1d2a4250becd1b9df4b7da64bca0..a42e624e1d6d6ac7711d909d84f0feaa7a5d0bc2 100644 (file)
@@ -170,15 +170,6 @@ check_cxx_compiler_flag(-Wno-unused-parameter HAS_NO_UNUSED_PARAMETER)
 if (HAS_NO_UNUSED_PARAMETER)
     set(_scanner_cpp_compiler_flags "${_scanner_cpp_compiler_flags} -Wno-unused-parameter")
 endif()
-check_cxx_compiler_flag(-Wno-deprecated-register HAS_NO_DEPRECATED_REGISTER)
-if (HAS_NO_DEPRECATED_REGISTER)
-    set(_scanner_cpp_compiler_flags "${_scanner_cpp_compiler_flags} -Wno-deprecated-register")
-else()
-    check_cxx_compiler_flag(-Wno-deprecated HAS_NO_DEPRECATED)
-    if (HAS_NO_DEPRECATED)
-        set(_scanner_cpp_compiler_flags "${_scanner_cpp_compiler_flags} -Wno-deprecated")
-    endif()
-endif()
 set_source_files_properties(selection/scanner.cpp PROPERTIES COMPILE_FLAGS "${_scanner_cpp_compiler_flags}")
 
 target_link_libraries(libgromacs
index 3cdfb93e73b11554cb8984336cb319257c61e377..c16a479a6793abca29797c07e961632832fd5963 100644 (file)
@@ -19,8 +19,8 @@
 
 #define FLEX_SCANNER
 #define YY_FLEX_MAJOR_VERSION 2
-#define YY_FLEX_MINOR_VERSION 5
-#define YY_FLEX_SUBMINOR_VERSION 37
+#define YY_FLEX_MINOR_VERSION 6
+#define YY_FLEX_SUBMINOR_VERSION 0
 #if YY_FLEX_SUBMINOR_VERSION > 0
 #define FLEX_BETA
 #endif
@@ -170,7 +170,15 @@ typedef void* yyscan_t;
 
 /* Size of default input buffer. */
 #ifndef YY_BUF_SIZE
+#ifdef __ia64__
+/* On IA-64, the buffer size is 16k, not 8k.
+ * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case.
+ * Ditto for the __ia64__ case accordingly.
+ */
+#define YY_BUF_SIZE 32768
+#else
 #define YY_BUF_SIZE 16384
+#endif /* __ia64__ */
 #endif
 
 /* The state buf must be large enough to hold one state per character in the main buffer.
@@ -192,6 +200,7 @@ typedef size_t yy_size_t;
 #define EOB_ACT_LAST_MATCH 2
 
     #define YY_LESS_LINENO(n)
+    #define YY_LINENO_REWIND_TO(ptr)
     
 /* Return all but the first "n" matched characters back to the input stream. */
 #define yyless(n) \
@@ -347,6 +356,9 @@ typedef int yy_state_type;
 static yy_state_type yy_get_previous_state (yyscan_t yyscanner );
 static yy_state_type yy_try_NUL_trans (yy_state_type current_state  ,yyscan_t yyscanner);
 static int yy_get_next_buffer (yyscan_t yyscanner );
+#if defined(__GNUC__) && __GNUC__ >= 3
+__attribute__((__noreturn__))
+#endif
 static void yy_fatal_error (yyconst char msg[] ,yyscan_t yyscanner );
 
 /* Done after the current pattern has been matched and before the
@@ -380,7 +392,7 @@ static yyconst flex_int16_t yy_accept[76] =
         3,    3,   17,    9,    0
     } ;
 
-static yyconst flex_int32_t yy_ec[256] =
+static yyconst YY_CHAR yy_ec[256] =
     {   0,
         1,    1,    1,    1,    1,    1,    1,    1,    2,    3,
         2,    2,    2,    1,    1,    1,    1,    1,    1,    1,
@@ -412,7 +424,7 @@ static yyconst flex_int32_t yy_ec[256] =
         1,    1,    1,    1,    1
     } ;
 
-static yyconst flex_int32_t yy_meta[33] =
+static yyconst YY_CHAR yy_meta[33] =
     {   0,
         1,    1,    2,    1,    1,    1,    1,    1,    3,    4,
         1,    1,    1,    4,    4,    1,    4,    4,    4,    4,
@@ -420,7 +432,7 @@ static yyconst flex_int32_t yy_meta[33] =
         4,    1
     } ;
 
-static yyconst flex_int16_t yy_base[80] =
+static yyconst flex_uint16_t yy_base[80] =
     {   0,
         0,    0,   93,   92,   10,   12,    0,    0,  115,  118,
        35,   37,  101,   37,    0,  106,  102,   35,  118,   98,
@@ -444,7 +456,7 @@ static yyconst flex_int16_t yy_def[80] =
        75,   75,   79,   79,    0,   75,   75,   75,   75
     } ;
 
-static yyconst flex_int16_t yy_nxt[151] =
+static yyconst flex_uint16_t yy_nxt[151] =
     {   0,
        10,   11,   12,   13,   14,   15,   16,   10,   17,   18,
        19,   20,   21,   22,   22,   23,   24,   25,   22,   22,
@@ -495,7 +507,7 @@ static yyconst flex_int16_t yy_chk[151] =
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -543,6 +555,7 @@ static yyconst flex_int16_t yy_chk[151] =
 
 #line 60 "scanner.l"
 #include "gromacs/utility/cstringutil.h"
+#include "gromacs/utility/stringutil.h"
 
 #include "parser.h"
 #include "scanner.h"
@@ -560,7 +573,7 @@ static yyconst flex_int16_t yy_chk[151] =
 
 
 
-#line 564 "scanner.cpp"
+#line 577 "scanner.cpp"
 
 #define INITIAL 0
 #define matchof 1
@@ -634,11 +647,11 @@ void _gmx_sel_yyset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
 
 FILE *_gmx_sel_yyget_in (yyscan_t yyscanner );
 
-void _gmx_sel_yyset_in  (FILE * in_str ,yyscan_t yyscanner );
+void _gmx_sel_yyset_in  (FILE * _in_str ,yyscan_t yyscanner );
 
 FILE *_gmx_sel_yyget_out (yyscan_t yyscanner );
 
-void _gmx_sel_yyset_out  (FILE * out_str ,yyscan_t yyscanner );
+void _gmx_sel_yyset_out  (FILE * _out_str ,yyscan_t yyscanner );
 
 yy_size_t _gmx_sel_yyget_leng (yyscan_t yyscanner );
 
@@ -646,11 +659,11 @@ char *_gmx_sel_yyget_text (yyscan_t yyscanner );
 
 int _gmx_sel_yyget_lineno (yyscan_t yyscanner );
 
-void _gmx_sel_yyset_lineno (int line_number ,yyscan_t yyscanner );
+void _gmx_sel_yyset_lineno (int _line_number ,yyscan_t yyscanner );
 
 int _gmx_sel_yyget_column  (yyscan_t yyscanner );
 
-void _gmx_sel_yyset_column (int column_no ,yyscan_t yyscanner );
+void _gmx_sel_yyset_column (int _column_no ,yyscan_t yyscanner );
 
 /* Macros after this point can all be overridden by user definitions in
  * section 1.
@@ -664,8 +677,12 @@ extern int _gmx_sel_yywrap (yyscan_t yyscanner );
 #endif
 #endif
 
+#ifndef YY_NO_UNPUT
+    
     static void yyunput (int c,char *buf_ptr  ,yyscan_t yyscanner);
     
+#endif
+
 #ifndef yytext_ptr
 static void yy_flex_strncpy (char *,yyconst char *,int ,yyscan_t yyscanner);
 #endif
@@ -686,7 +703,12 @@ static int input (yyscan_t yyscanner );
 
 /* Amount of stuff to slurp up with each read. */
 #ifndef YY_READ_BUF_SIZE
+#ifdef __ia64__
+/* On IA-64, the buffer size is 16k, not 8k */
+#define YY_READ_BUF_SIZE 16384
+#else
 #define YY_READ_BUF_SIZE 8192
+#endif /* __ia64__ */
 #endif
 
 /* Copy whatever the last rule matched to the standard output. */
@@ -773,7 +795,7 @@ extern int _gmx_sel_yylex (yyscan_t yyscanner);
 
 /* Code executed at the end of each rule. */
 #ifndef YY_BREAK
-#define YY_BREAK break;
+#define YY_BREAK /*LINTED*/break;
 #endif
 
 #define YY_RULE_SETUP \
@@ -783,12 +805,39 @@ extern int _gmx_sel_yylex (yyscan_t yyscanner);
  */
 YY_DECL
 {
-       register yy_state_type yy_current_state;
-       register char *yy_cp, *yy_bp;
-       register int yy_act;
+       yy_state_type yy_current_state;
+       char *yy_cp, *yy_bp;
+       int yy_act;
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
 
-#line 98 "scanner.l"
+       if ( !yyg->yy_init )
+               {
+               yyg->yy_init = 1;
+
+#ifdef YY_USER_INIT
+               YY_USER_INIT;
+#endif
+
+               if ( ! yyg->yy_start )
+                       yyg->yy_start = 1;      /* first start state */
+
+               if ( ! yyin )
+                       yyin = stdin;
+
+               if ( ! yyout )
+                       yyout = stdout;
+
+               if ( ! YY_CURRENT_BUFFER ) {
+                       _gmx_sel_yyensure_buffer_stack (yyscanner);
+                       YY_CURRENT_BUFFER_LVALUE =
+                               _gmx_sel_yy_create_buffer(yyin,YY_BUF_SIZE ,yyscanner);
+               }
+
+               _gmx_sel_yy_load_buffer_state(yyscanner );
+               }
+
+       {
+#line 99 "scanner.l"
 
 
 
@@ -822,35 +871,9 @@ YY_DECL
     }
 
 
-#line 826 "scanner.cpp"
+#line 875 "scanner.cpp"
 
-       if ( !yyg->yy_init )
-               {
-               yyg->yy_init = 1;
-
-#ifdef YY_USER_INIT
-               YY_USER_INIT;
-#endif
-
-               if ( ! yyg->yy_start )
-                       yyg->yy_start = 1;      /* first start state */
-
-               if ( ! yyin )
-                       yyin = stdin;
-
-               if ( ! yyout )
-                       yyout = stdout;
-
-               if ( ! YY_CURRENT_BUFFER ) {
-                       _gmx_sel_yyensure_buffer_stack (yyscanner);
-                       YY_CURRENT_BUFFER_LVALUE =
-                               _gmx_sel_yy_create_buffer(yyin,YY_BUF_SIZE ,yyscanner);
-               }
-
-               _gmx_sel_yy_load_buffer_state(yyscanner );
-               }
-
-       while ( 1 )             /* loops until end-of-file is reached */
+       while ( /*CONSTCOND*/1 )                /* loops until end-of-file is reached */
                {
                yy_cp = yyg->yy_c_buf_p;
 
@@ -866,7 +889,7 @@ YY_DECL
 yy_match:
                do
                        {
-                       register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)];
+                       YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ;
                        if ( yy_accept[yy_current_state] )
                                {
                                yyg->yy_last_accepting_state = yy_current_state;
@@ -903,38 +926,38 @@ do_action:        /* This label is used only to access EOF actions. */
 
 case 1:
 YY_RULE_SETUP
-#line 131 "scanner.l"
+#line 132 "scanner.l"
 break;
        YY_BREAK
 case 2:
 YY_RULE_SETUP
-#line 132 "scanner.l"
+#line 133 "scanner.l"
 { yylval->i   = strtol(yytext, NULL, 10);    ADD_TOKEN; return TOK_INT; }
        YY_BREAK
 case 3:
 YY_RULE_SETUP
-#line 133 "scanner.l"
+#line 134 "scanner.l"
 { yylval->r   = strtod(yytext, NULL);        ADD_TOKEN; return TOK_REAL; }
        YY_BREAK
 case 4:
 YY_RULE_SETUP
-#line 134 "scanner.l"
+#line 135 "scanner.l"
 { yylval->str = gmx_strndup(yytext+1, yyleng-2); ADD_TOKEN; return STR;  }
        YY_BREAK
 case 5:
 /* rule 5 can match eol */
 YY_RULE_SETUP
-#line 136 "scanner.l"
+#line 137 "scanner.l"
 { _gmx_sel_lexer_add_token(yylloc, " ", 1, state); break; }
        YY_BREAK
 case 6:
 /* rule 6 can match eol */
 YY_RULE_SETUP
-#line 137 "scanner.l"
+#line 138 "scanner.l"
 {
                     if (yytext[0] == ';' || state->statusWriter != NULL)
                     {
-                        rtrim(state->pselstr);
+                        state->pselstr = gmx::stripString(state->pselstr);
                         state->bCmdStart = true;
                         return CMD_SEP;
                     }
@@ -946,94 +969,94 @@ YY_RULE_SETUP
                 }
        YY_BREAK
 case YY_STATE_EOF(cmdstart):
-#line 151 "scanner.l"
+#line 152 "scanner.l"
 { state->bCmdStart = true; yyterminate(); }
        YY_BREAK
 case YY_STATE_EOF(INITIAL):
 case YY_STATE_EOF(matchof):
 case YY_STATE_EOF(matchbool):
-#line 152 "scanner.l"
+#line 153 "scanner.l"
 { state->bCmdStart = true; return CMD_SEP; }
        YY_BREAK
 
 case 7:
 YY_RULE_SETUP
-#line 155 "scanner.l"
+#line 156 "scanner.l"
 { ADD_TOKEN; yylval->i = 1; return TOK_INT; }
        YY_BREAK
 case 8:
 YY_RULE_SETUP
-#line 156 "scanner.l"
+#line 157 "scanner.l"
 { ADD_TOKEN; yylval->i = 0; return TOK_INT; }
        YY_BREAK
 
 case 9:
 YY_RULE_SETUP
-#line 158 "scanner.l"
+#line 159 "scanner.l"
 { ADD_TOKEN; return GROUP; }
        YY_BREAK
 case 10:
 YY_RULE_SETUP
-#line 159 "scanner.l"
+#line 160 "scanner.l"
 { ADD_TOKEN; return TO; }
        YY_BREAK
 case 11:
 YY_RULE_SETUP
-#line 160 "scanner.l"
+#line 161 "scanner.l"
 { ADD_TOKEN; BEGIN(0); return OF; }
        YY_BREAK
 case 12:
 YY_RULE_SETUP
-#line 161 "scanner.l"
+#line 162 "scanner.l"
 { ADD_TOKEN; return AND; }
        YY_BREAK
 case 13:
 YY_RULE_SETUP
-#line 162 "scanner.l"
+#line 163 "scanner.l"
 { ADD_TOKEN; return OR; }
        YY_BREAK
 case 14:
 YY_RULE_SETUP
-#line 163 "scanner.l"
+#line 164 "scanner.l"
 { ADD_TOKEN; return XOR; }
        YY_BREAK
 case 15:
 YY_RULE_SETUP
-#line 164 "scanner.l"
+#line 165 "scanner.l"
 { ADD_TOKEN; return NOT; }
        YY_BREAK
 case 16:
 YY_RULE_SETUP
-#line 165 "scanner.l"
+#line 166 "scanner.l"
 { yylval->str = gmx_strndup(yytext, yyleng); ADD_TOKEN; return CMP_OP; }
        YY_BREAK
 case 17:
 YY_RULE_SETUP
-#line 167 "scanner.l"
+#line 168 "scanner.l"
 { return _gmx_sel_lexer_process_identifier(yylval, yylloc, yytext, yyleng, state); }
        YY_BREAK
 case 18:
 /* rule 18 can match eol */
 YY_RULE_SETUP
-#line 169 "scanner.l"
+#line 170 "scanner.l"
 { _gmx_sel_lexer_add_token(yylloc, " ", 1, state); break; }
        YY_BREAK
 case 19:
 YY_RULE_SETUP
-#line 170 "scanner.l"
+#line 171 "scanner.l"
 { yylval->str = gmx_strndup(yytext, yyleng); ADD_TOKEN; return STR; }
        YY_BREAK
 case 20:
 YY_RULE_SETUP
-#line 171 "scanner.l"
+#line 172 "scanner.l"
 { ADD_TOKEN; return yytext[0]; }
        YY_BREAK
 case 21:
 YY_RULE_SETUP
-#line 172 "scanner.l"
+#line 173 "scanner.l"
 YY_FATAL_ERROR( "flex scanner jammed" );
        YY_BREAK
-#line 1037 "scanner.cpp"
+#line 1060 "scanner.cpp"
 
        case YY_END_OF_BUFFER:
                {
@@ -1163,6 +1186,7 @@ YY_FATAL_ERROR( "flex scanner jammed" );
                        "fatal flex scanner internal error--no action found" );
        } /* end of action switch */
                } /* end of scanning one token */
+       } /* end of user's declarations */
 } /* end of _gmx_sel_yylex */
 
 /* yy_get_next_buffer - try to read in a new buffer
@@ -1175,9 +1199,9 @@ YY_FATAL_ERROR( "flex scanner jammed" );
 static int yy_get_next_buffer (yyscan_t yyscanner)
 {
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
-       register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf;
-       register char *source = yyg->yytext_ptr;
-       register int number_to_move, i;
+       char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf;
+       char *source = yyg->yytext_ptr;
+       yy_size_t number_to_move, i;
        int ret_val;
 
        if ( yyg->yy_c_buf_p > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars + 1] )
@@ -1206,7 +1230,7 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
        /* Try to read more data. */
 
        /* First move last chars to start of buffer. */
-       number_to_move = (int) (yyg->yy_c_buf_p - yyg->yytext_ptr) - 1;
+       number_to_move = (yy_size_t) (yyg->yy_c_buf_p - yyg->yytext_ptr) - 1;
 
        for ( i = 0; i < number_to_move; ++i )
                *(dest++) = *(source++);
@@ -1309,15 +1333,15 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
 
     static yy_state_type yy_get_previous_state (yyscan_t yyscanner)
 {
-       register yy_state_type yy_current_state;
-       register char *yy_cp;
+       yy_state_type yy_current_state;
+       char *yy_cp;
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
 
        yy_current_state = yyg->yy_start;
 
        for ( yy_cp = yyg->yytext_ptr + YY_MORE_ADJ; yy_cp < yyg->yy_c_buf_p; ++yy_cp )
                {
-               register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1);
+               YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1);
                if ( yy_accept[yy_current_state] )
                        {
                        yyg->yy_last_accepting_state = yy_current_state;
@@ -1342,11 +1366,11 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
  */
     static yy_state_type yy_try_NUL_trans  (yy_state_type yy_current_state , yyscan_t yyscanner)
 {
-       register int yy_is_jam;
+       int yy_is_jam;
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; /* This var may be unused depending upon options. */
-       register char *yy_cp = yyg->yy_c_buf_p;
+       char *yy_cp = yyg->yy_c_buf_p;
 
-       register YY_CHAR yy_c = 1;
+       YY_CHAR yy_c = 1;
        if ( yy_accept[yy_current_state] )
                {
                yyg->yy_last_accepting_state = yy_current_state;
@@ -1365,9 +1389,11 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
        return yy_is_jam ? 0 : yy_current_state;
 }
 
-    static void yyunput (int c, register char * yy_bp , yyscan_t yyscanner)
+#ifndef YY_NO_UNPUT
+
+    static void yyunput (int c, char * yy_bp , yyscan_t yyscanner)
 {
-       register char *yy_cp;
+       char *yy_cp;
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
 
     yy_cp = yyg->yy_c_buf_p;
@@ -1378,10 +1404,10 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
        if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 )
                { /* need to shift things up to make room */
                /* +2 for EOB chars. */
-               register yy_size_t number_to_move = yyg->yy_n_chars + 2;
-               register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[
+               yy_size_t number_to_move = yyg->yy_n_chars + 2;
+               char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[
                                        YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2];
-               register char *source =
+               char *source =
                                &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move];
 
                while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
@@ -1403,6 +1429,8 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
        yyg->yy_c_buf_p = yy_cp;
 }
 
+#endif
+
 #ifndef YY_NO_INPUT
 #ifdef __cplusplus
     static int yyinput (yyscan_t yyscanner)
@@ -1556,7 +1584,7 @@ static void _gmx_sel_yy_load_buffer_state  (yyscan_t yyscanner)
        if ( ! b )
                YY_FATAL_ERROR( "out of dynamic memory in _gmx_sel_yy_create_buffer()" );
 
-       b->yy_buf_size = size;
+       b->yy_buf_size = (yy_size_t)size;
 
        /* yy_ch_buf has to be 2 characters longer than the size given because
         * we need to put in 2 end-of-buffer characters.
@@ -1717,7 +1745,7 @@ static void _gmx_sel_yyensure_buffer_stack (yyscan_t yyscanner)
                 * scanner will even need a stack. We use 2 instead of 1 to avoid an
                 * immediate realloc on the next call.
          */
-               num_to_alloc = 1;
+               num_to_alloc = 1; // After all that talk, this was set to 1 anyways...
                yyg->yy_buffer_stack = (struct yy_buffer_state**)_gmx_sel_yyalloc
                                                                (num_to_alloc * sizeof(struct yy_buffer_state*)
                                                                , yyscanner);
@@ -1734,7 +1762,7 @@ static void _gmx_sel_yyensure_buffer_stack (yyscan_t yyscanner)
        if (yyg->yy_buffer_stack_top >= (yyg->yy_buffer_stack_max) - 1){
 
                /* Increase the buffer to prepare for a possible push. */
-               int grow_size = 8 /* arbitrary grow size */;
+               yy_size_t grow_size = 8 /* arbitrary grow size */;
 
                num_to_alloc = yyg->yy_buffer_stack_max + grow_size;
                yyg->yy_buffer_stack = (struct yy_buffer_state**)_gmx_sel_yyrealloc
@@ -1842,7 +1870,9 @@ YY_BUFFER_STATE _gmx_sel_yy_scan_bytes  (yyconst char * yybytes, yy_size_t  _yyb
 
 static void yy_fatal_error (yyconst char* msg , yyscan_t yyscanner)
 {
-       (void) fprintf( stderr, "%s\n", msg );
+       struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
+       (void)yyg;
+       (void) fprintf( stderr, "%s\n", msg );
        exit( YY_EXIT_FAILURE );
 }
 
@@ -1948,10 +1978,10 @@ void _gmx_sel_yyset_extra (YY_EXTRA_TYPE  user_defined , yyscan_t yyscanner)
 }
 
 /** Set the current line number.
- * @param line_number
+ * @param _line_number line number
  * @param yyscanner The scanner object.
  */
-void _gmx_sel_yyset_lineno (int  line_number , yyscan_t yyscanner)
+void _gmx_sel_yyset_lineno (int  _line_number , yyscan_t yyscanner)
 {
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
 
@@ -1959,14 +1989,14 @@ void _gmx_sel_yyset_lineno (int  line_number , yyscan_t yyscanner)
         if (! YY_CURRENT_BUFFER )
            YY_FATAL_ERROR( "_gmx_sel_yyset_lineno called with no buffer" );
     
-    yylineno = line_number;
+    yylineno = _line_number;
 }
 
 /** Set the current column.
- * @param line_number
+ * @param _column_no column number
  * @param yyscanner The scanner object.
  */
-void _gmx_sel_yyset_column (int  column_no , yyscan_t yyscanner)
+void _gmx_sel_yyset_column (int  _column_no , yyscan_t yyscanner)
 {
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
 
@@ -1974,25 +2004,25 @@ void _gmx_sel_yyset_column (int  column_no , yyscan_t yyscanner)
         if (! YY_CURRENT_BUFFER )
            YY_FATAL_ERROR( "_gmx_sel_yyset_column called with no buffer" );
     
-    yycolumn = column_no;
+    yycolumn = _column_no;
 }
 
 /** Set the input stream. This does not discard the current
  * input buffer.
- * @param in_str A readable stream.
+ * @param _in_str A readable stream.
  * @param yyscanner The scanner object.
  * @see _gmx_sel_yy_switch_to_buffer
  */
-void _gmx_sel_yyset_in (FILE *  in_str , yyscan_t yyscanner)
+void _gmx_sel_yyset_in (FILE *  _in_str , yyscan_t yyscanner)
 {
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
-    yyin = in_str ;
+    yyin = _in_str ;
 }
 
-void _gmx_sel_yyset_out (FILE *  out_str , yyscan_t yyscanner)
+void _gmx_sel_yyset_out (FILE *  _out_str , yyscan_t yyscanner)
 {
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
-    yyout = out_str ;
+    yyout = _out_str ;
 }
 
 int _gmx_sel_yyget_debug  (yyscan_t yyscanner)
@@ -2001,10 +2031,10 @@ int _gmx_sel_yyget_debug  (yyscan_t yyscanner)
     return yy_flex_debug;
 }
 
-void _gmx_sel_yyset_debug (int  bdebug , yyscan_t yyscanner)
+void _gmx_sel_yyset_debug (int  _bdebug , yyscan_t yyscanner)
 {
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
-    yy_flex_debug = bdebug ;
+    yy_flex_debug = _bdebug ;
 }
 
 /* Accessor methods for yylval and yylloc */
@@ -2143,7 +2173,10 @@ int _gmx_sel_yylex_destroy  (yyscan_t yyscanner)
 #ifndef yytext_ptr
 static void yy_flex_strncpy (char* s1, yyconst char * s2, int n , yyscan_t yyscanner)
 {
-       register int i;
+       struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
+       (void)yyg;
+
+       int i;
        for ( i = 0; i < n; ++i )
                s1[i] = s2[i];
 }
@@ -2152,7 +2185,7 @@ static void yy_flex_strncpy (char* s1, yyconst char * s2, int n , yyscan_t yysca
 #ifdef YY_NEED_STRLEN
 static int yy_flex_strlen (yyconst char * s , yyscan_t yyscanner)
 {
-       register int n;
+       int n;
        for ( n = 0; s[n]; ++n )
                ;
 
@@ -2162,11 +2195,16 @@ static int yy_flex_strlen (yyconst char * s , yyscan_t yyscanner)
 
 void *_gmx_sel_yyalloc (yy_size_t  size , yyscan_t yyscanner)
 {
+       struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
+       (void)yyg;
        return (void *) malloc( size );
 }
 
 void *_gmx_sel_yyrealloc  (void * ptr, yy_size_t  size , yyscan_t yyscanner)
 {
+       struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
+       (void)yyg;
+
        /* The cast to (char *) in the following accommodates both
         * implementations that use char* generic pointers, and those
         * that use void* generic pointers.  It works with the latter
@@ -2179,9 +2217,11 @@ void *_gmx_sel_yyrealloc  (void * ptr, yy_size_t  size , yyscan_t yyscanner)
 
 void _gmx_sel_yyfree (void * ptr , yyscan_t yyscanner)
 {
+       struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
+       (void)yyg;
        free( (char *) ptr );   /* see _gmx_sel_yyrealloc() for (char *) cast */
 }
 
 #define YYTABLES_NAME "yytables"
 
-#line 172 "scanner.l"
+#line 173 "scanner.l"
index d2d2fd97ecbd8bc7edcfd52df34ceeaf32a89d0d..2c03a33e4278e4bbb7956e6d5524ffabd047f823 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -58,6 +58,7 @@
 }
 %{
 #include "gromacs/utility/cstringutil.h"
+#include "gromacs/utility/stringutil.h"
 
 #include "parser.h"
 #include "scanner.h"
@@ -137,7 +138,7 @@ COMMENT    (#.*)
 ";"|\n          {
                     if (yytext[0] == ';' || state->statusWriter != NULL)
                     {
-                        rtrim(state->pselstr);
+                        state->pselstr = gmx::stripString(state->pselstr);
                         state->bCmdStart = true;
                         return CMD_SEP;
                     }
index 8bf42639c23dbf615614b1657a78752b72c0111b..50b6185de3c48348fe483ba98c126674915e36e3 100644 (file)
@@ -4,17 +4,8 @@
  
  #define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol)
  
--#define _gmx_sel_yywrap(yyscanner) 1
+-#define _gmx_sel_yywrap(yyscanner) (/*CONSTCOND*/1)
 +static inline int _gmx_sel_yywrap(yyscan_t yyscanner) { return 1; }
  #define YY_SKIP_YYWRAP
  
  typedef unsigned char YY_CHAR;
-@@ -1807,7 +1807,7 @@
-       YY_BUFFER_STATE b;
-       char *buf;
-       yy_size_t n;
--      int i;
-+      yy_size_t i;
-     
-       /* Get memory for full buffer, including space for trailing EOB's. */
-       n = _yybytes_len + 2;
index abad91858e7dc5f935ff2528ff63dd32b2487038..0e23931895812e50d6d941e92514385b5f4a781b 100644 (file)
@@ -23,8 +23,8 @@
 
 #define FLEX_SCANNER
 #define YY_FLEX_MAJOR_VERSION 2
-#define YY_FLEX_MINOR_VERSION 5
-#define YY_FLEX_SUBMINOR_VERSION 37
+#define YY_FLEX_MINOR_VERSION 6
+#define YY_FLEX_SUBMINOR_VERSION 0
 #if YY_FLEX_SUBMINOR_VERSION > 0
 #define FLEX_BETA
 #endif
@@ -143,7 +143,15 @@ typedef void* yyscan_t;
 
 /* Size of default input buffer. */
 #ifndef YY_BUF_SIZE
+#ifdef __ia64__
+/* On IA-64, the buffer size is 16k, not 8k.
+ * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case.
+ * Ditto for the __ia64__ case accordingly.
+ */
+#define YY_BUF_SIZE 32768
+#else
 #define YY_BUF_SIZE 16384
+#endif /* __ia64__ */
 #endif
 
 #ifndef YY_TYPEDEF_YY_BUFFER_STATE
@@ -223,7 +231,7 @@ void *_gmx_sel_yyalloc (yy_size_t ,yyscan_t yyscanner );
 void *_gmx_sel_yyrealloc (void *,yy_size_t ,yyscan_t yyscanner );
 void _gmx_sel_yyfree (void * ,yyscan_t yyscanner );
 
-#define _gmx_sel_yywrap(yyscanner) 1
+#define _gmx_sel_yywrap(yyscanner) (/*CONSTCOND*/1)
 #define YY_SKIP_YYWRAP
 
 #define yytext_ptr yytext_r
@@ -267,11 +275,11 @@ void _gmx_sel_yyset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
 
 FILE *_gmx_sel_yyget_in (yyscan_t yyscanner );
 
-void _gmx_sel_yyset_in  (FILE * in_str ,yyscan_t yyscanner );
+void _gmx_sel_yyset_in  (FILE * _in_str ,yyscan_t yyscanner );
 
 FILE *_gmx_sel_yyget_out (yyscan_t yyscanner );
 
-void _gmx_sel_yyset_out  (FILE * out_str ,yyscan_t yyscanner );
+void _gmx_sel_yyset_out  (FILE * _out_str ,yyscan_t yyscanner );
 
 yy_size_t _gmx_sel_yyget_leng (yyscan_t yyscanner );
 
@@ -279,11 +287,11 @@ char *_gmx_sel_yyget_text (yyscan_t yyscanner );
 
 int _gmx_sel_yyget_lineno (yyscan_t yyscanner );
 
-void _gmx_sel_yyset_lineno (int line_number ,yyscan_t yyscanner );
+void _gmx_sel_yyset_lineno (int _line_number ,yyscan_t yyscanner );
 
 int _gmx_sel_yyget_column  (yyscan_t yyscanner );
 
-void _gmx_sel_yyset_column (int column_no ,yyscan_t yyscanner );
+void _gmx_sel_yyset_column (int _column_no ,yyscan_t yyscanner );
 
 /* Macros after this point can all be overridden by user definitions in
  * section 1.
@@ -311,7 +319,12 @@ static int yy_flex_strlen (yyconst char * ,yyscan_t yyscanner);
 
 /* Amount of stuff to slurp up with each read. */
 #ifndef YY_READ_BUF_SIZE
+#ifdef __ia64__
+/* On IA-64, the buffer size is 16k, not 8k */
+#define YY_READ_BUF_SIZE 16384
+#else
 #define YY_READ_BUF_SIZE 8192
+#endif /* __ia64__ */
 #endif
 
 /* Number of entries by which start-condition stack grows. */
@@ -344,8 +357,8 @@ extern int _gmx_sel_yylex (yyscan_t yyscanner);
 #undef YY_DECL
 #endif
 
-#line 172 "scanner.l"
+#line 173 "scanner.l"
 
-#line 350 "scanner_flex.h"
+#line 363 "scanner_flex.h"
 #undef _gmx_sel_yyIN_HEADER
 #endif /* _gmx_sel_yyHEADER_H */
index 79d915f58625fc4d2fdd7f891908fd06d6c0654e..da23df0453793912170321553d18f15f6a85221a 100644 (file)
 #include "selmethod.h"
 #include "symrec.h"
 
-/*! \brief
- * Step in which the allocated memory for pretty-printed input is incremented.
- */
-#define STRSTORE_ALLOCSTEP 1000
-
 /* These are defined as macros in the generated scanner_flex.h.
  * We undefine them here to have them as variable names in the subroutines.
  * There are other ways of doing this, but this is probably the easiest. */
@@ -355,12 +350,12 @@ void
 _gmx_sel_lexer_add_token(YYLTYPE *yylloc, const char *str, int len,
                          gmx_sel_lexer_t *state)
 {
-    yylloc->startIndex = yylloc->endIndex = state->pslen;
+    yylloc->startIndex = yylloc->endIndex = state->pselstr.size();
     /* Do nothing if the string is empty, or if it is a space and there is
      * no other text yet, or if there already is a space. */
     if (!str || len == 0 || strlen(str) == 0
         || (str[0] == ' ' && str[1] == 0
-            && (state->pslen == 0 || state->pselstr[state->pslen - 1] == ' ')))
+            && (state->pselstr.empty() || state->pselstr.back() == ' ')))
     {
         return;
     }
@@ -368,18 +363,9 @@ _gmx_sel_lexer_add_token(YYLTYPE *yylloc, const char *str, int len,
     {
         len = strlen(str);
     }
-    /* Allocate more memory if necessary */
-    if (state->nalloc_psel - state->pslen < len)
-    {
-        int incr = STRSTORE_ALLOCSTEP < len ? len : STRSTORE_ALLOCSTEP;
-        state->nalloc_psel += incr;
-        srenew(state->pselstr, state->nalloc_psel);
-    }
     /* Append the token to the stored string */
-    strncpy(state->pselstr + state->pslen, str, len);
-    state->pslen                += len;
-    state->pselstr[state->pslen] = 0;
-    yylloc->endIndex             = state->pslen;
+    state->pselstr.append(str, len);
+    yylloc->endIndex = state->pselstr.size();
 }
 
 void
@@ -407,10 +393,6 @@ _gmx_sel_init_lexer(yyscan_t *scannerp, struct gmx_ana_selcollection_t *sc,
 
     state->statusWriter = statusWriter;
 
-    snew(state->pselstr, STRSTORE_ALLOCSTEP);
-    state->pselstr[0]                 = 0;
-    state->pslen                      = 0;
-    state->nalloc_psel                = STRSTORE_ALLOCSTEP;
     state->currentLocation.startIndex = 0;
     state->currentLocation.endIndex   = 0;
 
@@ -435,7 +417,6 @@ _gmx_sel_free_lexer(yyscan_t scanner)
 {
     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
 
-    sfree(state->pselstr);
     sfree(state->mstack);
     if (state->bBuffer)
     {
@@ -504,7 +485,7 @@ const char *
 _gmx_sel_lexer_pselstr(yyscan_t scanner)
 {
     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
-    return state->pselstr;
+    return state->pselstr.c_str();
 }
 
 void
@@ -540,15 +521,14 @@ _gmx_sel_lexer_get_text(yyscan_t                      scanner,
     {
         return std::string();
     }
-    return std::string(&state->pselstr[startIndex], endIndex - startIndex);
+    return state->pselstr.substr(startIndex, endIndex - startIndex);
 }
 
 void
 _gmx_sel_lexer_clear_pselstr(yyscan_t scanner)
 {
     gmx_sel_lexer_t *state = _gmx_sel_yyget_extra(scanner);
-    state->pselstr[0] = 0;
-    state->pslen      = 0;
+    state->pselstr.clear();
 }
 
 void
index 70173aeb8cb8907ae50b97449f7600ca7f0a3c0f..3a182f3fd5bb06313662f69dafe3e92eb967af86 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2009,2010,2011,2012,2014,2015, by the GROMACS development team, led by
+ * Copyright (c) 2009,2010,2011,2012,2014,2015,2016, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -42,6 +42,7 @@
 #define SELECTION_SCANNER_INTERNAL_H
 
 #include <exception>
+#include <string>
 
 #include "parser.h"
 
@@ -86,11 +87,7 @@ typedef struct gmx_sel_lexer_t
     gmx::TextWriter                 *statusWriter;
 
     //! Pretty-printed version of the string parsed since last clear.
-    char                            *pselstr;
-    //! Length of the string in \a pselstr.
-    int                              pslen;
-    //! Number of bytes allocated for \a pselstr.
-    int                              nalloc_psel;
+    std::string                      pselstr;
     /*! \brief
      * Position of the result of the current Bison action.
      *