Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f123f84

Browse files
committed
Patch by Per Cederqvist, who writes:
""" - It needlessly used the makefile() method for each response that is read from the SMTP server. - If the remote SMTP server closes the connection unexpectedly the code raised an IndexError. It now raises an SMTPServerDisconnected exception instead. - The code now checks that all lines in a multiline response actually contains an error code. """ The Dragon approves.
1 parent 9065ea3 commit f123f84

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

Lib/smtplib.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,30 @@ def getreply(self):
187187
188188
- server response string corresponding to response code (multiline
189189
responses are converted to a single, multiline string).
190+
191+
Raises SMTPServerDisconnected if end-of-file is reached.
190192
"""
191193
resp=[]
192-
self.file = self.sock.makefile('rb')
194+
if self.file is None:
195+
self.file = self.sock.makefile('rb')
193196
while 1:
194197
line = self.file.readline()
198+
if line == '':
199+
self.close()
200+
raise SMTPServerDisconnected("Connection unexpectedly closed")
195201
if self.debuglevel > 0: print 'reply:', `line`
196202
resp.append(string.strip(line[4:]))
197203
code=line[:3]
198-
#check if multiline resp
204+
# Check that the error code is syntactically correct.
205+
# Don't attempt to read a continuation line if it is broken.
206+
try:
207+
errcode = string.atoi(code)
208+
except ValueError:
209+
errcode = -1
210+
break
211+
# Check if multiline response.
199212
if line[3:4]!="-":
200213
break
201-
try:
202-
errcode = string.atoi(code)
203-
except(ValueError):
204-
errcode = -1
205214

206215
errmsg = string.join(resp,"\n")
207216
if self.debuglevel > 0:

0 commit comments

Comments
 (0)