From f60fe670adbd4e2b4c8534ea4fd4c619ed8352d0 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Wed, 6 Feb 2019 20:36:11 +0000 Subject: [PATCH] handle_{got,sent}data: separate length and error params. Now we pass an error code in a separate dedicated parameter, instead of overloading the length parameter so that a negative value means an error code. This enables length to become unsigned without causing trouble. --- windows/winhandl.c | 8 ++++---- windows/winhsock.c | 17 +++++++---------- windows/winplink.c | 19 +++++++------------ windows/winser.c | 13 +++++++------ windows/winstuff.h | 6 ++++-- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/windows/winhandl.c b/windows/winhandl.c index c2cc6a82..0e2a66ea 100644 --- a/windows/winhandl.c +++ b/windows/winhandl.c @@ -664,9 +664,9 @@ void handle_got_event(HANDLE event) * EOF, or (nearly equivalently) read error. */ h->u.i.defunct = true; - h->u.i.gotdata(h, NULL, -h->u.i.readerr); + h->u.i.gotdata(h, NULL, 0, h->u.i.readerr); } else { - backlog = h->u.i.gotdata(h, h->u.i.buffer, h->u.i.len); + backlog = h->u.i.gotdata(h, h->u.i.buffer, h->u.i.len, 0); handle_throttle(&h->u.i, backlog); } break; @@ -686,11 +686,11 @@ void handle_got_event(HANDLE event) * thread is terminating by now). */ h->u.o.defunct = true; - h->u.o.sentdata(h, -h->u.o.writeerr); + h->u.o.sentdata(h, 0, h->u.o.writeerr); } else { bufchain_consume(&h->u.o.queued_data, h->u.o.lenwritten); noise_ultralight(NOISE_SOURCE_IOLEN, h->u.o.lenwritten); - h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data)); + h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data), 0); handle_try_output(&h->u.o); } break; diff --git a/windows/winhsock.c b/windows/winhsock.c index a50989a8..0f5586f5 100644 --- a/windows/winhsock.c +++ b/windows/winhsock.c @@ -45,11 +45,11 @@ typedef struct HandleSocket { Socket sock; } HandleSocket; -static int handle_gotdata(struct handle *h, const void *data, int len) +static int handle_gotdata(struct handle *h, const void *data, int len, int err) { HandleSocket *hs = (HandleSocket *)handle_get_privdata(h); - if (len < 0) { + if (err) { plug_closing(hs->plug, "Read error from handle", 0, 0); return 0; } else if (len == 0) { @@ -79,25 +79,22 @@ static int handle_gotdata(struct handle *h, const void *data, int len) } } -static int handle_stderr(struct handle *h, const void *data, int len) +static int handle_stderr(struct handle *h, const void *data, int len, int err) { HandleSocket *hs = (HandleSocket *)handle_get_privdata(h); - if (len > 0) + if (!err && len > 0) log_proxy_stderr(hs->plug, &hs->stderrdata, data, len); return 0; } -static void handle_sentdata(struct handle *h, int new_backlog) +static void handle_sentdata(struct handle *h, int new_backlog, int err) { HandleSocket *hs = (HandleSocket *)handle_get_privdata(h); - if (new_backlog < 0) { - /* Special case: this is actually reporting an error writing - * to the underlying handle, and our input value is the error - * code itself, negated. */ - plug_closing(hs->plug, win_strerror(-new_backlog), -new_backlog, 0); + if (err) { + plug_closing(hs->plug, win_strerror(err), err, 0); return; } diff --git a/windows/winplink.c b/windows/winplink.c index 46e2afa3..3543808f 100644 --- a/windows/winplink.c +++ b/windows/winplink.c @@ -205,14 +205,11 @@ char *do_select(SOCKET skt, bool startup) return NULL; } -int stdin_gotdata(struct handle *h, const void *data, int len) +int stdin_gotdata(struct handle *h, const void *data, int len, int err) { - if (len < 0) { - /* - * Special case: report read error. - */ + if (err) { char buf[4096]; - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -len, 0, + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, lenof(buf), NULL); buf[lenof(buf)-1] = '\0'; if (buf[strlen(buf)-1] == '\n') @@ -232,14 +229,11 @@ int stdin_gotdata(struct handle *h, const void *data, int len) return 0; } -void stdouterr_sent(struct handle *h, int new_backlog) +void stdouterr_sent(struct handle *h, int new_backlog, int err) { - if (new_backlog < 0) { - /* - * Special case: report write error. - */ + if (err) { char buf[4096]; - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -new_backlog, 0, + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, lenof(buf), NULL); buf[lenof(buf)-1] = '\0'; if (buf[strlen(buf)-1] == '\n') @@ -248,6 +242,7 @@ void stdouterr_sent(struct handle *h, int new_backlog) (h == stdout_handle ? "output" : "error"), buf); cleanup_exit(0); } + if (backend_connected(backend)) { backend_unthrottle(backend, (handle_backlog(stdout_handle) + handle_backlog(stderr_handle))); diff --git a/windows/winser.c b/windows/winser.c index 1d86006a..4afd150b 100644 --- a/windows/winser.c +++ b/windows/winser.c @@ -40,10 +40,11 @@ static void serial_terminate(Serial *serial) } } -static int serial_gotdata(struct handle *h, const void *data, int len) +static int serial_gotdata( + struct handle *h, const void *data, int len, int err) { Serial *serial = (Serial *)handle_get_privdata(h); - if (len <= 0) { + if (err || len == 0) { const char *error_msg; /* @@ -53,7 +54,7 @@ static int serial_gotdata(struct handle *h, const void *data, int len) * pipes or some other non-serial device, in which case EOF * may become meaningful here. */ - if (len == 0) + if (!err) error_msg = "End of file reading from serial device"; else error_msg = "Error reading from serial device"; @@ -66,16 +67,16 @@ static int serial_gotdata(struct handle *h, const void *data, int len) seat_connection_fatal(serial->seat, "%s", error_msg); - return 0; /* placate optimiser */ + return 0; } else { return seat_stdout(serial->seat, data, len); } } -static void serial_sentdata(struct handle *h, int new_backlog) +static void serial_sentdata(struct handle *h, int new_backlog, int err) { Serial *serial = (Serial *)handle_get_privdata(h); - if (new_backlog < 0) { + if (err) { const char *error_msg = "Error writing to serial device"; serial_terminate(serial); diff --git a/windows/winstuff.h b/windows/winstuff.h index 617aa969..5a1f13bf 100644 --- a/windows/winstuff.h +++ b/windows/winstuff.h @@ -603,8 +603,10 @@ void init_ucs(Conf *, struct unicode_data *); #define HANDLE_FLAG_IGNOREEOF 2 #define HANDLE_FLAG_UNITBUFFER 4 struct handle; -typedef int (*handle_inputfn_t)(struct handle *h, const void *data, int len); -typedef void (*handle_outputfn_t)(struct handle *h, int new_backlog); +typedef int (*handle_inputfn_t)( + struct handle *h, const void *data, int len, int err); +typedef void (*handle_outputfn_t)( + struct handle *h, int new_backlog, int err); struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata, void *privdata, int flags); struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata,