Changeset 252

Show
Ignore:
Timestamp:
2003-06-09 16:52:57 (6 years ago)
Author:
schwa
Message:
_MSStatParser.parse_line: Add more sanity error checking.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ftp_stat.py

    r250 r252  
    3434""" 
    3535 
    36 # $Id: ftp_stat.py,v 1.6 2003/06/09 16:04:49 schwa Exp $ 
     36# $Id: ftp_stat.py,v 1.7 2003/06/09 16:52:57 schwa Exp $ 
    3737 
    3838import stat 
     
    104104        If the line can't be parsed, raise a `ParserError`. 
    105105        """ 
    106         metadata, nlink, user, group, size, month, day, \ 
    107           year_or_time, name = line.split(None, 8) 
     106        try: 
     107            metadata, nlink, user, group, size, month, day, \ 
     108              year_or_time, name = line.split(None, 8) 
     109        except ValueError: 
     110            # "unpack list of wrong size" 
     111            raise ftp_error.ParserError("line '%s' can't be parsed" % line ) 
    108112        # st_mode 
    109113        st_mode = 0 
     114        if len(metadata) != 10: 
     115            raise ftp_error.ParserError("invalid metadata '%s'" % metadata) 
    110116        for bit in metadata[1:10]: 
    111117            bit = (bit != '-') 
     
    132138        st_atime = None 
    133139        # st_mtime 
    134         month = self._month_numbers[ month.lower() ] 
     140        try: 
     141            month = self._month_numbers[ month.lower() ] 
     142        except KeyError: 
     143            raise ftp_error.ParserError("invalid month name '%s'" % month) 
    135144        day = int(day) 
    136145        if year_or_time.find(':') == -1: 
     
    172181        raise a `ParserError`. 
    173182        """ 
    174         date, time_, dir_or_size, name = line.split(None, 3) 
     183        try: 
     184            date, time_, dir_or_size, name = line.split(None, 3) 
     185        except ValueError: 
     186            # "unpack list of wrong size" 
     187            raise ftp_error.ParserError("line '%s' can't be parsed" % line ) 
    175188        # st_mode 
    176189        st_mode = 0400   # default to read access only; 
     
    188201        # st_size 
    189202        if dir_or_size != '<DIR>': 
    190             st_size = int(dir_or_size) 
     203            try: 
     204                st_size = int(dir_or_size) 
     205            except ValueError: 
     206                raise ftp_error.ParserError("invalid size %s" % dir_or_size) 
    191207        else: 
    192208            st_size = None 
     
    194210        st_atime = None 
    195211        # st_mtime 
    196         month, day, year = map( int, date.split('-') ) 
    197         if year >= 70: 
    198             year = 1900 + year 
    199         else: 
    200             year = 2000 + year 
    201         hour, minute, am_pm = time_[0:2], time_[3:5], time_[5] 
    202         hour, minute = int(hour), int(minute) 
     212        try: 
     213            month, day, year = map( int, date.split('-') ) 
     214            if year >= 70: 
     215                year = 1900 + year 
     216            else: 
     217                year = 2000 + year 
     218            hour, minute, am_pm = time_[0:2], time_[3:5], time_[5] 
     219            hour, minute = int(hour), int(minute) 
     220        except (ValueError, IndexError): 
     221            raise ftp_error.ParserError("invalid time string '%s'" % time_) 
    203222        if am_pm == 'P': 
    204223            hour = 12 + hour