]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[Backport r46602 | neal.norwitz]
authorAndrew M. Kuchling <amk@amk.ca>
Fri, 6 Oct 2006 18:59:10 +0000 (18:59 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Fri, 6 Oct 2006 18:59:10 +0000 (18:59 +0000)
Patch #1357836:

Prevent an invalid memory read from test_coding in case the done flag is set.
In that case, the loop isn't entered.  I wonder if rather than setting
the done flag in the cases before the loop, if they should just exit early.

This code looks like it should be refactored.

Backport candidate (also the early break above if decoding_fgets fails)

Parser/tokenizer.c

index 07b38a99612e6ec2f1788daae4309239d83db3a0..fedab3092f33dd32d15ba242bd5dd3e71605b14c 100644 (file)
@@ -865,6 +865,11 @@ tok_nextc(register struct tok_state *tok)
                                if (decoding_fgets(tok->inp,
                                               (int)(tok->end - tok->inp),
                                               tok) == NULL) {
+                                       /* Break out early on decoding
+                                          errors, as tok->buf will be NULL
+                                        */
+                                       if (tok->decoding_erred)
+                                               return EOF;
                                        /* Last line does not end in \n,
                                           fake one */
                                        strcpy(tok->inp, "\n");
@@ -872,14 +877,16 @@ tok_nextc(register struct tok_state *tok)
                                tok->inp = strchr(tok->inp, '\0');
                                done = tok->inp[-1] == '\n';
                        }
-                       tok->cur = tok->buf + cur;
-                       /* replace "\r\n" with "\n" */
-                       /* For Mac we leave the \r, giving a syntax error */
-                       pt = tok->inp - 2;
-                       if (pt >= tok->buf && *pt == '\r') {
-                               *pt++ = '\n';
-                               *pt = '\0';
-                               tok->inp = pt;
+                       if (tok->buf != NULL) {
+                               tok->cur = tok->buf + cur;
+                               /* replace "\r\n" with "\n" */
+                               /* For Mac we leave the \r, giving a syntax error */
+                               pt = tok->inp - 2;
+                               if (pt >= tok->buf && *pt == '\r') {
+                                       *pt++ = '\n';
+                                       *pt = '\0';
+                                       tok->inp = pt;
+                               }
                        }
                }
                if (tok->done != E_OK) {