Skip to content

Commit 727f716

Browse files
authored
gh-160601: Handle surrogate characters in sampling profiler binary output (#160615)
1 parent dc8ec9f commit 727f716

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎Lib/test/test_profiling/test_sampling_profiler/test_binary_format.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,34 @@ def test_unicode_funcnames(self):
829829
self.assertEqual(count, 1)
830830
self.assert_samples_equal(samples, collector)
831831

832+
def test_surrogates_in_frame_names(self):
833+
cases = [
834+
("\udce9", r"\udce9"), # A surrogate-escaped filename byte.
835+
("\uddd0", r"\uddd0"), # The surrogate from gh-160601.
836+
("\ud800", r"\ud800"),
837+
("\udfff", r"\udfff"),
838+
("\ud800\udfff", r"\ud800\udfff"),
839+
("caf\xe9\udce9\U0001f40d", "caf\xe9\\udce9\U0001f40d"),
840+
]
841+
for name, escaped_name in cases:
842+
with self.subTest(name=name):
843+
frames = [
844+
make_frame(f"/path/{name}.py", 42, f"func_{name}"),
845+
make_frame("test.py", 1, "main"),
846+
]
847+
samples = [[make_interpreter(0, [make_thread(1, frames)])]] * 3
848+
collector, count = self.roundtrip(samples)
849+
self.assertEqual(count, 3)
850+
851+
expected_frames = [
852+
make_frame(f"/path/{escaped_name}.py", 42,
853+
f"func_{escaped_name}"),
854+
frames[1],
855+
]
856+
expected = [[make_interpreter(
857+
0, [make_thread(1, expected_frames)])]] * 3
858+
self.assert_samples_equal(expected, collector)
859+
832860
def test_special_char_filenames(self):
833861
"""Filenames with special characters roundtrip exactly."""
834862
filenames = [
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a :exc:`UnicodeEncodeError` in :mod:`profiling.sampling` binary mode
2+
when a sampled filename or function name contains surrogate characters.
3+
These characters are now written as backslash escapes in the binary file.

‎Modules/_remote_debugging/binary_io_writer.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,15 @@ writer_intern_string(BinaryWriter *writer, PyObject *string, uint32_t *index)
351351
}
352352
}
353353

354-
Py_ssize_t str_len;
355-
const char *str_data = PyUnicode_AsUTF8AndSize(string, &str_len);
356-
if (!str_data) {
354+
/* Frame names can contain surrogates (e.g. from undecodable filenames).
355+
* Escape them to keep the string table valid UTF-8. */
356+
PyObject *encoded = PyUnicode_AsEncodedString(string, "utf-8", "backslashreplace");
357+
if (encoded == NULL) {
357358
return -1;
358359
}
360+
Py_ssize_t str_len = PyBytes_GET_SIZE(encoded);
359361
if ((uintmax_t)str_len > UINT32_MAX) {
362+
Py_DECREF(encoded);
360363
writer->state = BINARY_WRITER_LIMIT_REACHED;
361364
PyErr_Format(PyExc_OverflowError,
362365
"string length %zd exceeds binary format maximum %u",
@@ -366,10 +369,12 @@ writer_intern_string(BinaryWriter *writer, PyObject *string, uint32_t *index)
366369

367370
char *str_copy = PyMem_Malloc(str_len + 1);
368371
if (!str_copy) {
372+
Py_DECREF(encoded);
369373
PyErr_NoMemory();
370374
return -1;
371375
}
372-
memcpy(str_copy, str_data, str_len + 1);
376+
memcpy(str_copy, PyBytes_AS_STRING(encoded), str_len + 1);
377+
Py_DECREF(encoded);
373378

374379
*index = (uint32_t)writer->string_count;
375380

0 commit comments

Comments
 (0)