Skip to content

Commit 522ef49

Browse files
committed
Fix
1 parent 9adcf8e commit 522ef49

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

Doc/library/mmap.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
333333
Annotate the memory mapping with the given *name* for easier identification
334334
in ``/proc/<pid>/maps`` if the kernel supports the feature and :option:`-X dev <-X>` is passed
335335
to Python or if Python is built in :ref:`debug mode <debug-build>`
336-
The length of *name* must not exceed 67 bytes.
336+
The length of *name* must not exceed 67 bytes including '\0'
337337

338338
.. availability:: Linux >= 5.17 (kernel built with ``CONFIG_ANON_VMA_NAME`` option)
339339

Include/internal/pycore_mmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ _PyAnnotateMemoryMap(void *addr, size_t size, const char *name)
2525
return;
2626
}
2727
#endif
28+
// The name length cannot exceed 80 (including the '\0').
2829
assert(strlen(name) < 80);
2930
int old_errno = errno;
3031
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)addr, size, name);

Lib/test/test_mmap.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,14 +1174,14 @@ def test_set_name(self):
11741174
result = m.set_name('test_mapping')
11751175
self.assertIsNone(result)
11761176

1177-
# Test name length limit (80 chars including prefix "cpython:mmap:")
1178-
# Prefix is 13 chars, so max name is 67 chars
1179-
long_name = 'x' * 67
1177+
# Test name length limit (80 chars including prefix "cpython:mmap:" and '\0')
1178+
# Prefix is 13 chars, so max name is 66 chars
1179+
long_name = 'x' * 66
11801180
result = m.set_name(long_name)
11811181
self.assertIsNone(result)
11821182

11831183
# Test name too long
1184-
too_long_name = 'x' * 68
1184+
too_long_name = 'x' * 67
11851185
with self.assertRaises(ValueError):
11861186
m.set_name(too_long_name)
11871187

Modules/mmapmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,12 +1133,12 @@ mmap_mmap_set_name_impl(mmap_object *self, const char *name)
11331133
{
11341134
#if defined(MAP_ANONYMOUS) && defined(__linux__)
11351135
const char *prefix = "cpython:mmap:";
1136-
if (strlen(name) + strlen(prefix) > 80) {
1136+
if (strlen(name) + strlen(prefix) > 79) {
11371137
PyErr_SetString(PyExc_ValueError, "name is too long");
11381138
return NULL;
11391139
}
11401140
if (self->flags & MAP_ANONYMOUS) {
1141-
char buf[81];
1141+
char buf[80];
11421142
sprintf(buf, "%s%s", prefix, name);
11431143
_PyAnnotateMemoryMap(self->data, self->size, buf);
11441144
Py_RETURN_NONE;

0 commit comments

Comments
 (0)