@@ -38,7 +38,7 @@ adddfa(grammar *g, int type, char *name)
3838 Py_FatalError ("no mem to resize dfa in adddfa" );
3939 d = & g -> g_dfa [g -> g_ndfas ++ ];
4040 d -> d_type = type ;
41- d -> d_name = name ;
41+ d -> d_name = strdup ( name ) ;
4242 d -> d_nstates = 0 ;
4343 d -> d_state = NULL ;
4444 d -> d_initial = -1 ;
@@ -98,7 +98,10 @@ addlabel(labellist *ll, int type, char *str)
9898 Py_FatalError ("no mem to resize labellist in addlabel" );
9999 lb = & ll -> ll_label [ll -> ll_nlabels ++ ];
100100 lb -> lb_type = type ;
101- lb -> lb_str = str ; /* XXX strdup(str) ??? */
101+ lb -> lb_str = strdup (str );
102+ if (Py_DebugFlag )
103+ printf ("Label @ %08x, %d: %s\n" , (unsigned )ll , ll -> ll_nlabels ,
104+ PyGrammar_LabelRepr (lb ));
102105 return lb - ll -> ll_label ;
103106}
104107
@@ -152,6 +155,7 @@ translabel(grammar *g, label *lb)
152155 lb -> lb_str ,
153156 g -> g_dfa [i ].d_type );
154157 lb -> lb_type = g -> g_dfa [i ].d_type ;
158+ free (lb -> lb_str );
155159 lb -> lb_str = NULL ;
156160 return ;
157161 }
@@ -162,6 +166,7 @@ translabel(grammar *g, label *lb)
162166 printf ("Label %s is terminal %d.\n" ,
163167 lb -> lb_str , i );
164168 lb -> lb_type = i ;
169+ free (lb -> lb_str );
165170 lb -> lb_str = NULL ;
166171 return ;
167172 }
@@ -173,18 +178,29 @@ translabel(grammar *g, label *lb)
173178 if (lb -> lb_type == STRING ) {
174179 if (isalpha ((int )(lb -> lb_str [1 ])) || lb -> lb_str [1 ] == '_' ) {
175180 char * p ;
181+ char * src ;
182+ char * dest ;
183+ size_t name_len ;
176184 if (Py_DebugFlag )
177185 printf ("Label %s is a keyword\n" , lb -> lb_str );
178186 lb -> lb_type = NAME ;
179- lb -> lb_str ++ ;
180- p = strchr (lb -> lb_str , '\'' );
187+ src = lb -> lb_str + 1 ;
188+ p = strchr (src , '\'' );
181189 if (p )
182- * p = '\0' ;
190+ name_len = p - src ;
191+ else
192+ name_len = strlen (src );
193+ dest = malloc (name_len + 1 );
194+ strncpy (dest , src , name_len );
195+ dest [name_len ] = '\0' ;
196+ free (lb -> lb_str );
197+ lb -> lb_str = dest ;
183198 }
184199 else if (lb -> lb_str [2 ] == lb -> lb_str [0 ]) {
185200 int type = (int ) PyToken_OneChar (lb -> lb_str [1 ]);
186201 if (type != OP ) {
187202 lb -> lb_type = type ;
203+ free (lb -> lb_str );
188204 lb -> lb_str = NULL ;
189205 }
190206 else
@@ -196,6 +212,7 @@ translabel(grammar *g, label *lb)
196212 lb -> lb_str [2 ]);
197213 if (type != OP ) {
198214 lb -> lb_type = type ;
215+ free (lb -> lb_str );
199216 lb -> lb_str = NULL ;
200217 }
201218 else
@@ -208,6 +225,7 @@ translabel(grammar *g, label *lb)
208225 lb -> lb_str [3 ]);
209226 if (type != OP ) {
210227 lb -> lb_type = type ;
228+ free (lb -> lb_str );
211229 lb -> lb_str = NULL ;
212230 }
213231 else
0 commit comments