@@ -182,8 +182,10 @@ expand_encodebuffer(MultibyteEncodeBuffer *buf, Py_ssize_t esize)
182182 orgsize = PyBytes_GET_SIZE (buf -> outobj );
183183 incsize = (esize < (orgsize >> 1 ) ? (orgsize >> 1 ) | 1 : esize );
184184
185- if (orgsize > PY_SSIZE_T_MAX - incsize )
185+ if (orgsize > PY_SSIZE_T_MAX - incsize ) {
186+ PyErr_NoMemory ();
186187 return -1 ;
188+ }
187189
188190 if (_PyBytes_Resize (& buf -> outobj , orgsize + incsize ) == -1 )
189191 return -1 ;
@@ -194,11 +196,11 @@ expand_encodebuffer(MultibyteEncodeBuffer *buf, Py_ssize_t esize)
194196
195197 return 0 ;
196198}
197- #define REQUIRE_ENCODEBUFFER (buf , s ) { \
198- if ((s) < 1 || (buf)->outbuf + (s) > (buf)->outbuf_end ) \
199+ #define REQUIRE_ENCODEBUFFER (buf , s ) do { \
200+ if ((s) < 0 || (s) > (buf)->outbuf_end - (buf)->outbuf ) \
199201 if (expand_encodebuffer(buf, s) == -1) \
200202 goto errorexit; \
201- }
203+ } while(0)
202204
203205
204206/**
@@ -332,10 +334,11 @@ multibytecodec_encerror(MultibyteCodec *codec,
332334
333335 assert (PyBytes_Check (retstr ));
334336 retstrsize = PyBytes_GET_SIZE (retstr );
335- REQUIRE_ENCODEBUFFER (buf , retstrsize );
336-
337- memcpy (buf -> outbuf , PyBytes_AS_STRING (retstr ), retstrsize );
338- buf -> outbuf += retstrsize ;
337+ if (retstrsize > 0 ) {
338+ REQUIRE_ENCODEBUFFER (buf , retstrsize );
339+ memcpy (buf -> outbuf , PyBytes_AS_STRING (retstr ), retstrsize );
340+ buf -> outbuf += retstrsize ;
341+ }
339342
340343 newpos = PyLong_AsSsize_t (PyTuple_GET_ITEM (retobj , 1 ));
341344 if (newpos < 0 && !PyErr_Occurred ())
0 commit comments