freemyipod r588 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r587‎ | r588 | r589 >
Date:18:42, 12 February 2011
Author:theseven
Status:new
Tags:
Comment:
libemcore.py: Fix readstring: Handle null pointers and control characters gracefully
Modified paths:
  • /emcore/trunk/tools/libemcore.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/libemcore.py
@@ -281,20 +281,24 @@
282282 return addr
283283
284284 @command()
285 - def readstring(self, addr, maxlength = 256):
 285+ def readstring(self, addr, maxlength = 256, replacement = "."):
286286 """ Reads a zero terminated string from memory
287287 Reads only a maximum of 'maxlength' chars.
288288 """
 289+ if addr == 0: return "<NULL>"
289290 cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
290291 string = ""
291 - while (len(string) < maxlength or maxlength < 0):
 292+ done = False
 293+ while not done and (len(string) < maxlength or maxlength < 0):
292294 data = self._readmem(addr, min(maxlength - len(string), cin_maxsize))
293295 length = data.find(b"\0")
294296 if length >= 0:
295 - string += data[:length].decode("latin_1")
296 - break
297 - else:
298 - string += data.decode("latin_1")
 297+ data = data[:length]
 298+ done = True
 299+ for i in range(len(data)):
 300+ byte = ord(data[i : i + 1])
 301+ if byte < 0x20: string = string + replacement
 302+ else: string = string + chr(byte)
299303 addr += cin_maxsize
300304 return string
301305