google / adb-sync

Apache License 2.0
1.08k stars 169 forks source link

Problems with encoding: UnicodeDecodeError: 'utf-8' codec can't decode byte: invalid start byte #41

Open krantic opened 4 years ago

krantic commented 4 years ago

Hello, when starting adb-sync I always get the following error message. I'm using Windows 10 1909, Python 3.8.1 and the latest version of the script.

python adb-sync --reverse /sdcard/test/ "C:\Backup\2019-12-29\sdcard"
INFO:root:Sync: local b'C:\\Backup\\2019-12-29\\sdcard', remote b'/sdcard/test/'
Traceback (most recent call last):
  File "adb-sync", line 883, in <module>
    main()
  File "adb-sync", line 870, in main
    if not syncer.IsWorking():
  File "adb-sync", line 507, in IsWorking
    return self.adb.IsWorking()
  File "adb-sync", line 219, in IsWorking
    with Stdout(self.adb +
  File "adb-sync", line 76, in __init__
    self.popen = subprocess.Popen(args, stdout=subprocess.PIPE)
  File "C:\Python38\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Python38\lib\subprocess.py", line 1247, in _execute_child
    args = list2cmdline(args)
  File "C:\Python38\lib\subprocess.py", line 549, in list2cmdline
    for arg in map(os.fsdecode, seq):
  File "C:\Python38\lib\os.py", line 818, in fsdecode
    return filename.decode(encoding, errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 41: invalid start byte

As a workarround I tried different encodings for the script but without success:

  1. UTF-8/UTF-8 with BOM/Windows 1252/ISO 8859-1: Same error message as above.
  2. UTF-16 LE/BE:
    SyntaxError: Non-UTF-8 code starting with '\xff' in file C:\Users\Kristian\adb-sync\adb-sync on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Are there any other solutions ? Thanks in advance!

sonicer105 commented 4 years ago

I also have this issue


D:\Sources\adb-sync>C:\Users\redacted\AppData\Local\Programs\Python\Python38\python.exe adb-sync -R /storage/emulated/0/Music C:\Users\redacted\Desktop\Backup\Music
INFO:root:Sync: local b'C:\\Users\\redacted\\Desktop\\Backup\\Music/Music', remote b'/storage/emulated/0/Music'
Traceback (most recent call last):
  File "adb-sync", line 883, in <module>
    main()
  File "adb-sync", line 870, in main
    if not syncer.IsWorking():
  File "adb-sync", line 507, in IsWorking
    return self.adb.IsWorking()
  File "adb-sync", line 219, in IsWorking
    with Stdout(self.adb +
  File "adb-sync", line 76, in __init__
    self.popen = subprocess.Popen(args, stdout=subprocess.PIPE)
  File "C:\Users\redacted\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\redacted\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1247, in _execute_child
    args = list2cmdline(args)
  File "C:\Users\redacted\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 549, in list2cmdline
    for arg in map(os.fsdecode, seq):
  File "C:\Users\redacted\AppData\Local\Programs\Python\Python38\lib\os.py", line 818, in fsdecode
    return filename.decode(encoding, errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 41: invalid start byte```
PiN73 commented 4 years ago

Same problem no Windows

C:\Users\Pavel\Phone>python adb-sync\adb-sync --reverse /sdcard/DCIM .
INFO:root:Sync: local b'./DCIM', remote b'/sdcard/DCIM'
Traceback (most recent call last):
  File "adb-sync\adb-sync", line 883, in <module>
    main()
  File "adb-sync\adb-sync", line 870, in main
    if not syncer.IsWorking():
  File "adb-sync\adb-sync", line 507, in IsWorking
    return self.adb.IsWorking()
  File "adb-sync\adb-sync", line 219, in IsWorking
    with Stdout(self.adb +
  File "adb-sync\adb-sync", line 76, in __init__
    self.popen = subprocess.Popen(args, stdout=subprocess.PIPE)
  File "C:\Users\Pavel\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Pavel\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1247, in _execute_child
    args = list2cmdline(args)
  File "C:\Users\Pavel\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 549, in list2cmdline
    for arg in map(os.fsdecode, seq):
  File "C:\Users\Pavel\AppData\Local\Programs\Python\Python38\lib\os.py", line 818, in fsdecode
    return filename.decode(encoding, errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 41: invalid start byte

Sync seems working after I disable checks in IsWorking function e.g. made it just return True, but I think it should be a better way to fix it

ipcjs commented 4 years ago

modify

        b'(', b'(;  #`ls`$PATH\'"(\\\\\\\\){};!\xc0\xaf\xff\xc2\xbf'

to

        b'(', '(;  #`ls`$PATH\'"(\\\\\\\\){};!\xc0\xaf\xff\xc2\xbf'

can fix the problem : )

drwatson221b commented 3 years ago

modify

        b'(', b'(;  #`ls`$PATH\'"(\\\\\\\\){};!\xc0\xaf\xff\xc2\xbf'

to

        b'(', '(;  #`ls`$PATH\'"(\\\\\\\\){};!\xc0\xaf\xff\xc2\xbf'

can fix the problem : )

Now returns with error:

C:\Users\sriva\Downloads\platform-tools_r31.0.0-windows\platform-tools>python adb-sync -R /sdcard/Movies/ "C:\Users\sriva\Desktop\" INFO:root:Sync: local b'C:\Users\sriva\Desktop"', remote b'/sdcard/Movies/' Traceback (most recent call last): File "C:\Users\sriva\Downloads\platform-tools_r31.0.0-windows\platform-tools\adb-sync", line 883, in main() File "C:\Users\sriva\Downloads\platform-tools_r31.0.0-windows\platform-tools\adb-sync", line 870, in main if not syncer.IsWorking(): File "C:\Users\sriva\Downloads\platform-tools_r31.0.0-windows\platform-tools\adb-sync", line 507, in IsWorking return self.adb.IsWorking() File "C:\Users\sriva\Downloads\platform-tools_r31.0.0-windows\platform-tools\adb-sync", line 221, in IsWorking b'date +%s' % (self.QuoteArgument(test_string),)]) as stdout: File "C:\Users\sriva\Downloads\platform-tools_r31.0.0-windows\platform-tools\adb-sync", line 201, in QuoteArgument arg = arg.replace(b'\', b'\\') TypeError: replace() argument 1 must be str, not bytes

Khaja786431 commented 12 months ago

Can anyone help to resolve the issue. Traceback (most recent call last): File "C:\Baymax_v0.3\install.py", line 112, in if res.decode().count("Android Debug Bridge version") > 0: ^^^^^^^^^^^^ UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 92: invalid start byte

freemedom commented 6 months ago
    test_strings = [
        b'(', b'(;  #`ls`$PATH\'"(\\\\\\\\){};!'
    ]

Remove \xc0\xaf\xff\xc2\xbf and it will work. I actually don't think the IsWorking function here is very necessary. This paragraph is presumably to test whether some special characters that may be present in the file name can interact properly.

I guess this is probably due to the different decode mechanism of python3 and python2.

Reference: https://stackoverflow.com/questions/23772144/python-unicodedecodeerror-utf8-codec-cant-decode-byte-0xc0-in-position-0-i https://security.stackexchange.com/questions/48879/why-does-directory-traversal-attack-c0af-work

image