Skip to content

Commit 4e72660

Browse files
Fix _build_date() crash on non-standard locale date strings (#2513)
Some Android devices return locale-dependent strings in ro.build.date (e.g. '2017年 11月 14日 星期二 09:55:07 CST' for Chinese locale) that dateutil.parser.parse() cannot handle, causing wait_for_device() to crash with a ParserError. Fix: prefer ro.build.date.utc (integer epoch timestamp) which is locale-independent and available on all Android devices. Fall back to ro.build.date with error handling if the UTC property is missing.
1 parent 07f3caa commit 4e72660

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

pwnlib/adb/adb.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,8 +1275,20 @@ def __hash__(self, other):
12751275

12761276
def _build_date():
12771277
"""Returns the build date in the form YYYY-MM-DD as a string"""
1278+
# Prefer ro.build.date.utc (integer epoch) as ro.build.date is
1279+
# locale-dependent and may contain non-ASCII characters that
1280+
# dateutil cannot parse (e.g. Chinese locale dates). See #2513.
1281+
utc = getprop('ro.build.date.utc')
1282+
if utc and utc.strip().isdigit():
1283+
import datetime
1284+
as_datetime = datetime.datetime.fromtimestamp(int(utc.strip()), tz=datetime.timezone.utc)
1285+
return as_datetime.strftime('%Y-%b-%d')
1286+
12781287
as_string = getprop('ro.build.date')
1279-
as_datetime = dateutil.parser.parse(as_string)
1288+
try:
1289+
as_datetime = dateutil.parser.parse(as_string)
1290+
except (ValueError, OverflowError):
1291+
return as_string
12801292
return as_datetime.strftime('%Y-%b-%d')
12811293

12821294
def find_ndk_project_root(source):

0 commit comments

Comments
 (0)