]> granicus.if.org Git - python/commitdiff
Plug a memory leak where a struct tok_state was not being freed.
authorBrett Cannon <bcannon@gmail.com>
Sat, 20 Oct 2007 02:54:14 +0000 (02:54 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sat, 20 Oct 2007 02:54:14 +0000 (02:54 +0000)
Also tweak a comparison that was going farther than needed.

Parser/tokenizer.c

index 0ccd02b58d12c4ac637e5eecfdb0864ddda8816e..85f750874132f657360330102883175b5074ae1a 100644 (file)
@@ -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