2424#define GUARDSZ 8
2525// NUL followed by random bytes.
2626static const char guard [GUARDSZ ] _Py_NONSTRING = "\x00\xfa\x69\xc4\x67\xa3\x6c\x58" ;
27- const char CANARY_BYTE = 0xdd ;
2827
2928/*[clinic input]
3029module fcntl
@@ -122,14 +121,14 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
122121 return PyBytes_FromStringAndSize (buf , len );
123122 }
124123 else {
125- PyBytesWriter * writer = PyBytesWriter_Create (len + 1 );
124+ PyBytesWriter * writer = PyBytesWriter_Create (len + GUARDSZ );
126125 if (writer == NULL ) {
127126 PyBuffer_Release (& view );
128127 return NULL ;
129128 }
130129 char * ptr = PyBytesWriter_GetData (writer );
131130 memcpy (ptr , view .buf , len );
132- ptr [ len ] = CANARY_BYTE ;
131+ memcpy ( ptr + len , guard , GUARDSZ ) ;
133132 PyBuffer_Release (& view );
134133
135134 do {
@@ -144,7 +143,7 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
144143 PyBytesWriter_Discard (writer );
145144 return NULL ;
146145 }
147- if (ptr [ len ] != CANARY_BYTE ) {
146+ if (memcmp ( ptr + len , guard , GUARDSZ ) != 0 ) {
148147 PyErr_SetString (PyExc_SystemError ,
149148 "Memory corruption in fcntl() due to "
150149 "buffer overflow. "
@@ -153,7 +152,7 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
153152 PyBytesWriter_Discard (writer );
154153 return NULL ;
155154 }
156- // Truncate the last byte (canary byte )
155+ // Truncate the last bytes (guard )
157156 return PyBytesWriter_FinishWithSize (writer , len );
158157 }
159158#undef FCNTL_BUFSZ
@@ -319,14 +318,14 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
319318 return PyBytes_FromStringAndSize (buf , len );
320319 }
321320 else {
322- PyBytesWriter * writer = PyBytesWriter_Create (len + 1 );
321+ PyBytesWriter * writer = PyBytesWriter_Create (len + GUARDSZ );
323322 if (writer == NULL ) {
324323 PyBuffer_Release (& view );
325324 return NULL ;
326325 }
327326 char * ptr = PyBytesWriter_GetData (writer );
328327 memcpy (ptr , view .buf , len );
329- ptr [ len ] = CANARY_BYTE ;
328+ memcpy ( buf + len , guard , GUARDSZ ) ;
330329 PyBuffer_Release (& view );
331330
332331 do {
@@ -341,7 +340,7 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
341340 PyBytesWriter_Discard (writer );
342341 return NULL ;
343342 }
344- if (ptr [ len ] != CANARY_BYTE ) {
343+ if (memcmp ( ptr + len , guard , GUARDSZ ) != 0 ) {
345344 PyErr_SetString (PyExc_SystemError ,
346345 "Memory corruption in ioctl() due to "
347346 "buffer overflow. "
@@ -350,7 +349,7 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
350349 PyBytesWriter_Discard (writer );
351350 return NULL ;
352351 }
353- // Truncate the last byte (canary byte )
352+ // Truncate the last bytes (guard )
354353 return PyBytesWriter_FinishWithSize (writer , len );
355354 }
356355#undef IOCTL_BUFSZ
0 commit comments