From 6077322ef8c26df0ac1f0ead00940d92e5200691 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Sat, 13 Jun 2015 23:29:00 +1000 Subject: [PATCH] Check for socket errors when connection is terminated by server This means that when a connection fails or is closed due to a socket error, the error is properly bubbled back up to the user. --- source/newio.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/source/newio.c b/source/newio.c index 4ef2896..a880c82 100644 --- a/source/newio.c +++ b/source/newio.c @@ -214,11 +214,21 @@ int BX_dgets (char *str, int des, int buffer, int buffersize, void *ssl_fd) * use that as a cheap way to check for #1. If #1 is false, * then #2 must have been true, and if nbytes is 0, then * that indicates an EOF condition. + * + * The "EOF" condition might actually be a socket error, so check + * for that and return it to the caller. */ else if (!nbytes && ioe->write_pos == 0) { + int so_error; + socklen_t len = sizeof so_error; + *str = 0; - dgets_errno = -1; + if (getsockopt(des, SOL_SOCKET, SO_ERROR, &so_error, &len) == 0 && + so_error != 0) + dgets_errno = so_error; + else + dgets_errno = -1; return -1; }