]> arthur.barton.de Git - bup.git/commit
Prevent Python 3 from interfering with argv bytes
authorRob Browning <rlb@defaultvalue.org>
Tue, 10 Sep 2019 06:56:02 +0000 (01:56 -0500)
committerRob Browning <rlb@defaultvalue.org>
Sun, 15 Dec 2019 20:57:33 +0000 (14:57 -0600)
commitff935e1abef2ebe89a809c100edc7931523f3349
tree8f13c68e928b7975eefb1c882767474e9d29d557
parent3c64b5788c3e6b10528a81285b71da2edbed8950
Prevent Python 3 from interfering with argv bytes

Python 3 insists on treating all arguments as Unicode, and and if the
bytes don't fit, it shoehorns them in anyway[1].  We need the raw,
original bytes in many cases (paths being the obvious example), and
Python claims they can be extracted via fsdecode.

But experimentation with 3.7 has demonstrated that while this is
necessary, it is not sufficient to handle all possible binary
arguments in at least a UTF-8 locale.  The interpreter may crash at
startup with some (randomly generated) argument values:

  Fatal Python error: _PyMainInterpreterConfig_Read: memory allocation failed
  ValueError: character U+134bd2 is not in range [U+0000; U+10ffff]

  Current thread 0x00007f2f0e1d8740 (most recent call first):
  Traceback (most recent call last):
    File "t/test-argv", line 28, in <module>
      out = check_output(cmd)
    File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
      **kwargs).stdout
    File "/usr/lib/python3.7/subprocess.py", line 487, in run
      output=stdout, stderr=stderr)

To fix that, always set the encoding to ISO-8859-1 before launching
Python, which should hopefully (given that ISO-8859-1 is a single-byte
"pass through" encoding), prevent Python from interfering with the
arguments.

Add t/test-argv to perform randomized testing for clean argv
pass-through.  At the moment, with Python 3.7.3, if I disable the code
before the python exec in cmd/bup-python, this test eventually
provokes the crash above (though not on every run).

[1] https://www.python.org/dev/peps/pep-0383/

Thanks to Aaron M. Ucko for pointing out LC_ALL had been overlooked in
an earlier version of this patch, and would have undone the
adjustments.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
Makefile
cmd/python-cmd.sh
lib/bup/compat.py
t/echo-argv-bytes [new file with mode: 0755]
t/test-argv [new file with mode: 0755]