Plug a memory leak where a struct tok_state was not being freed.

Also tweak a comparison that was going farther than needed.
This commit is contained in:
Brett Cannon 2007-10-20 02:54:14 +00:00
parent 6643cb2a7a
commit d5ec98c7fb

View file

@ -52,6 +52,7 @@ static struct tok_state *tok_new(void);
static int tok_nextc(struct tok_state *tok); static int tok_nextc(struct tok_state *tok);
static void tok_backup(struct tok_state *tok, int c); static void tok_backup(struct tok_state *tok, int c);
/* Token names */ /* Token names */
char *_PyParser_TokenNames[] = { char *_PyParser_TokenNames[] = {
@ -1610,18 +1611,25 @@ PyTokenizer_RestoreEncoding(struct tok_state* tok, int len, int *offset)
char * char *
PyTokenizer_FindEncoding(FILE *fp) { PyTokenizer_FindEncoding(FILE *fp) {
struct tok_state *tok; struct tok_state *tok;
char *p_start=NULL, *p_end=NULL; char *p_start=NULL, *p_end=NULL, *encoding=NULL;
if ((tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL)) == NULL) { if ((tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL)) == NULL) {
rewind(fp); rewind(fp);
return NULL; return NULL;
} }
while(((tok->lineno <= 2) && (tok->done == E_OK))) { while(((tok->lineno < 2) && (tok->done == E_OK))) {
PyTokenizer_Get(tok, &p_start, &p_end); PyTokenizer_Get(tok, &p_start, &p_end);
} }
rewind(fp); rewind(fp);
return tok->encoding;
if (tok->encoding) {
encoding = (char *)PyMem_MALLOC(strlen(tok->encoding));
strcpy(encoding, tok->encoding);
}
PyTokenizer_Free(tok);
return encoding;
} }
#ifdef Py_DEBUG #ifdef Py_DEBUG