@@ -118,8 +118,16 @@ namespace kanzi {
118118
119119 // If the remaining chunk is large, write directly to FD to avoid double copy
120120 if (remaining >= streamsize (_buffer.size ())) {
121- if (WRITE (_fd, src, remaining) != remaining) {
122- return n - remaining; // Error
121+ streamsize toWrite = remaining;
122+
123+ while (toWrite > 0 ) {
124+ const ptrdiff_t written = ptrdiff_t (WRITE (_fd, src, toWrite));
125+
126+ if (written <= 0 )
127+ return n - remaining; // Error
128+
129+ src += written;
130+ toWrite -= streamsize (written);
123131 }
124132
125133 remaining = 0 ;
@@ -136,7 +144,19 @@ namespace kanzi {
136144 int flush () {
137145 ptrdiff_t n = pptr () - pbase ();
138146 if (n > 0 ) {
139- if (WRITE (_fd, pbase (), n) != n) return EOF ;
147+ char * dst = pbase ();
148+ ptrdiff_t remaining = n;
149+
150+ while (remaining > 0 ) {
151+ const ptrdiff_t written = ptrdiff_t (WRITE (_fd, dst, remaining));
152+
153+ if (written <= 0 )
154+ return EOF ;
155+
156+ dst += written;
157+ remaining -= written;
158+ }
159+
140160 pbump (-int (n)); // Reset pbump by subtracting the amount written
141161 }
142162
@@ -280,6 +300,8 @@ KANZI_API int CDECL disposeCompressor(struct cContext** ppCtx, size_t* outSize)
280300 *outSize = int (pCos->getWritten () - w);
281301
282302 delete pCos;
303+ pCos = nullptr ;
304+ pCtx->pCos = nullptr ;
283305 }
284306
285307 if (pCtx->fos != nullptr )
@@ -290,6 +312,12 @@ KANZI_API int CDECL disposeCompressor(struct cContext** ppCtx, size_t* outSize)
290312 *ppCtx = nullptr ;
291313 }
292314 catch (const exception&) {
315+ if (pCos != nullptr ) {
316+ delete pCos;
317+ pCos = nullptr ;
318+ pCtx->pCos = nullptr ;
319+ }
320+
293321 if (pCtx->fos != nullptr )
294322 delete static_cast <FileOutputStream*>(pCtx->fos );
295323
0 commit comments