Skip to content

Commit

Permalink
python: Use bytes instead of str for event callback buffer
Browse files Browse the repository at this point in the history
The event callback gets a buffer parameter which is usually something
like a log message.  However as it comes from C it is not necessarily
well-formed (eg) UTF-8 but could contain any old sequence of bytes.

In the test case provided by the reporter, we failed to encode the
buffer as 'str' with this error:

  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 137: unexpected end of data

Use 'bytes' instead.  Strictly speaking this changes the type
signature of the callbacks, but our existing Python tests which just
print the buffer using '%s' don't fail and in any case we don't
guarantee the stability of non-C APIs.

Reported-by: Yonatan Shtarkman
See: https://listman.redhat.com/archives/libguestfs/2023-February/030653.html
  • Loading branch information
rwmjones committed Feb 14, 2023
1 parent 6ef5837 commit bbf396f
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion python/handle.c
Expand Up @@ -131,7 +131,7 @@ guestfs_int_py_event_callback_wrapper (guestfs_h *g,
}

/* XXX As with Perl we don't pass the guestfs_h handle here. */
args = Py_BuildValue ("(Kis#O)",
args = Py_BuildValue ("(Kiy#O)",
(unsigned PY_LONG_LONG) event, event_handle,
buf, buf_len, py_array);
if (args == NULL) {
Expand Down

0 comments on commit bbf396f

Please sign in to comment.