diff mbox series

[v20210701,05/40] tools: handle libxl__physmap_info.name properly in convert-legacy-stream

Message ID 20210701095635.15648-6-olaf@aepfle.de (mailing list archive)
State Superseded
Headers show
Series leftover from 2020 | expand

Commit Message

Olaf Hering July 1, 2021, 9:56 a.m. UTC
The trailing member name[] in libxl__physmap_info is written as a
cstring into the stream. The current code does a sanity check if the
last byte is zero. This attempt fails with python3.4 because name[-1]
returns a type int. As a result the comparison with byte(\00) fails:

  File "/usr/lib/xen/bin/convert-legacy-stream", line 347, in read_libxl_toolstack
    raise StreamError("physmap name not NUL terminated")
StreamError: physmap name not NUL terminated

To handle both python variants the cstring is unpacked into the actual
string and the trailing nil.

Signed-off-by: Olaf Hering <olaf@aepfle.de>
---
 tools/python/scripts/convert-legacy-stream | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Andrew Cooper July 2, 2021, 3:35 p.m. UTC | #1
On 01/07/2021 10:56, Olaf Hering wrote:
> diff --git a/tools/python/scripts/convert-legacy-stream b/tools/python/scripts/convert-legacy-stream
> index 66ee3d2f5d..9003ac4f6d 100755
> --- a/tools/python/scripts/convert-legacy-stream
> +++ b/tools/python/scripts/convert-legacy-stream
> @@ -336,20 +336,21 @@ def read_libxl_toolstack(vm, data):
>          if len(data) < namelen:
>              raise StreamError("Remaining data too short for physmap name")
>  
> -        name = data[:namelen]
> +        c_string = data[:namelen]
>          data = data[namelen:]
>  
>          # Strip padding off the end of name
>          if twidth == 64:
> -            name = name[:-4]
> +            c_string = c_string[:-4]
>  
> -        if name[-1] != b'\x00':
> +        name, nil = unpack("={0}sB".format(len(c_string) - 1), c_string)

This is rather invasive.  How about simply:

diff --git a/tools/python/scripts/convert-legacy-stream
b/tools/python/scripts/convert-legacy-stream
index ca93a93848ec..d4ae94c02f21 100755
--- a/tools/python/scripts/convert-legacy-stream
+++ b/tools/python/scripts/convert-legacy-stream
@@ -342,7 +342,7 @@ def read_libxl_toolstack(vm, data):
         if twidth == 64:
             name = name[:-4]
 
-        if name[-1] != b'\x00':
+        if bytearray(name)[-1] != 0:
             raise StreamError("physmap name not NUL terminated")
 
         root = b"physmap/%x" % (phys, )

which is rather more contained, and looks to work from Py2.6 and later?

~Andrew
diff mbox series

Patch

diff --git a/tools/python/scripts/convert-legacy-stream b/tools/python/scripts/convert-legacy-stream
index 66ee3d2f5d..9003ac4f6d 100755
--- a/tools/python/scripts/convert-legacy-stream
+++ b/tools/python/scripts/convert-legacy-stream
@@ -336,20 +336,21 @@  def read_libxl_toolstack(vm, data):
         if len(data) < namelen:
             raise StreamError("Remaining data too short for physmap name")
 
-        name = data[:namelen]
+        c_string = data[:namelen]
         data = data[namelen:]
 
         # Strip padding off the end of name
         if twidth == 64:
-            name = name[:-4]
+            c_string = c_string[:-4]
 
-        if name[-1] != b'\x00':
+        name, nil = unpack("={0}sB".format(len(c_string) - 1), c_string)
+        if nil != 0:
             raise StreamError("physmap name not NUL terminated")
 
         root = b"physmap/%x" % (phys, )
         kv = [root + b"/start_addr", b"%x" % (start, ),
               root + b"/size",       b"%x" % (size, ),
-              root + b"/name",       name[:-1]]
+              root + b"/name",       name]
 
         for key, val in zip(kv[0::2], kv[1::2]):
             info("    '%s' = '%s'" % (key.decode(), val.decode()))