Skip to content

Commit 6e0152b

Browse files
committed
Audit of caml_write_fd: update channel state
Similar to the previous commit
1 parent 8971cec commit 6e0152b

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

runtime/io.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,36 @@ CAMLexport int caml_write_fd(int fd, int flags, void * buf, int n)
195195
return retcode;
196196
}
197197

198+
void write_channel_to_fd(struct channel * channel, int towrite)
199+
{
200+
int written;
201+
value exn = Val_unit;
202+
written = caml_write_fd_exn(channel->fd, channel->flags,
203+
channel->buff, towrite, &exn);
204+
if (written < 0) {
205+
caml_raise_if_exception(exn);
206+
CAMLassert(0);
207+
}
208+
if (written < towrite)
209+
memmove(channel->buff, channel->buff + written, towrite - written);
210+
channel->offset += written;
211+
channel->curr = channel->buff + towrite - written;
212+
caml_raise_if_exception(exn);
213+
}
214+
198215
/* Attempt to flush the buffer. This will make room in the buffer for
199216
at least one character. Returns true if the buffer is empty at the
200217
end of the flush, or false if some data remains in the buffer.
201218
*/
202219

203220
CAMLexport int caml_flush_partial(struct channel *channel)
204221
{
205-
int towrite, written;
222+
int towrite;
206223

207224
towrite = channel->curr - channel->buff;
208225
CAMLassert (towrite >= 0);
209226
if (towrite > 0) {
210-
written = caml_write_fd(channel->fd, channel->flags,
211-
channel->buff, towrite);
212-
channel->offset += written;
213-
if (written < towrite)
214-
memmove(channel->buff, channel->buff + written, towrite - written);
215-
channel->curr -= written;
227+
write_channel_to_fd(channel, towrite);
216228
}
217229
return (channel->curr == channel->buff);
218230
}
@@ -238,7 +250,7 @@ CAMLexport void caml_putword(struct channel *channel, uint32_t w)
238250

239251
CAMLexport int caml_putblock(struct channel *channel, char *p, intnat len)
240252
{
241-
int n, free, towrite, written;
253+
int n, free;
242254

243255
n = len >= INT_MAX ? INT_MAX : (int) len;
244256
free = channel->end - channel->curr;
@@ -251,13 +263,7 @@ CAMLexport int caml_putblock(struct channel *channel, char *p, intnat len)
251263
/* Write request overflows buffer (or just fills it up): transfer whatever
252264
fits to buffer and write the buffer */
253265
memmove(channel->curr, p, free);
254-
towrite = channel->end - channel->buff;
255-
written = caml_write_fd(channel->fd, channel->flags,
256-
channel->buff, towrite);
257-
if (written < towrite)
258-
memmove(channel->buff, channel->buff + written, towrite - written);
259-
channel->offset += written;
260-
channel->curr = channel->end - written;
266+
write_channel_to_fd(channel, channel->end - channel->buff);
261267
return free;
262268
}
263269
}

0 commit comments

Comments
 (0)