diff options
author | Brett Cannon <bcannon@gmail.com> | 2007-10-20 02:54:14 +0000 |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2007-10-20 02:54:14 +0000 |
commit | 4df3710a4e53ac5206c04a610dfed8be5aa9aa9d (patch) | |
tree | 39bae83285a158aed9ebdb485e8cf049095db1f5 /Parser | |
parent | 4d956fd1c85ab2517888262ed63b49026d06612c (diff) | |
download | cpython-4df3710a4e53ac5206c04a610dfed8be5aa9aa9d.tar.gz |
Plug a memory leak where a struct tok_state was not being freed.
Also tweak a comparison that was going farther than needed.
Diffstat (limited to 'Parser')
-rw-r--r-- | Parser/tokenizer.c | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 0ccd02b58d..85f7508741 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -52,6 +52,7 @@ static struct tok_state *tok_new(void); static int tok_nextc(struct tok_state *tok); static void tok_backup(struct tok_state *tok, int c); + /* Token names */ char *_PyParser_TokenNames[] = { @@ -1610,18 +1611,25 @@ PyTokenizer_RestoreEncoding(struct tok_state* tok, int len, int *offset) char * PyTokenizer_FindEncoding(FILE *fp) { 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) { rewind(fp); 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); } 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 |