@@ -174,8 +174,10 @@ expand_encodebuffer(MultibyteEncodeBuffer *buf, Py_ssize_t esize)
174174 orgsize = PyBytes_GET_SIZE (buf -> outobj );
175175 incsize = (esize < (orgsize >> 1 ) ? (orgsize >> 1 ) | 1 : esize );
176176
177- if (orgsize > PY_SSIZE_T_MAX - incsize )
177+ if (orgsize > PY_SSIZE_T_MAX - incsize ) {
178+ PyErr_NoMemory ();
178179 return -1 ;
180+ }
179181
180182 if (_PyBytes_Resize (& buf -> outobj , orgsize + incsize ) == -1 )
181183 return -1 ;
@@ -186,11 +188,11 @@ expand_encodebuffer(MultibyteEncodeBuffer *buf, Py_ssize_t esize)
186188
187189 return 0 ;
188190}
189- #define REQUIRE_ENCODEBUFFER (buf , s ) { \
190- if ((s) < 1 || (buf)->outbuf + (s) > (buf)->outbuf_end ) \
191+ #define REQUIRE_ENCODEBUFFER (buf , s ) do { \
192+ if ((s) < 0 || (s) > (buf)->outbuf_end - (buf)->outbuf ) \
191193 if (expand_encodebuffer(buf, s) == -1) \
192194 goto errorexit; \
193- }
195+ } while(0)
194196
195197
196198/**
@@ -324,10 +326,11 @@ multibytecodec_encerror(MultibyteCodec *codec,
324326
325327 assert (PyBytes_Check (retstr ));
326328 retstrsize = PyBytes_GET_SIZE (retstr );
327- REQUIRE_ENCODEBUFFER (buf , retstrsize );
328-
329- memcpy (buf -> outbuf , PyBytes_AS_STRING (retstr ), retstrsize );
330- buf -> outbuf += retstrsize ;
329+ if (retstrsize > 0 ) {
330+ REQUIRE_ENCODEBUFFER (buf , retstrsize );
331+ memcpy (buf -> outbuf , PyBytes_AS_STRING (retstr ), retstrsize );
332+ buf -> outbuf += retstrsize ;
333+ }
331334
332335 newpos = PyLong_AsSsize_t (PyTuple_GET_ITEM (retobj , 1 ));
333336 if (newpos < 0 && !PyErr_Occurred ())
0 commit comments