From 4fcb64fe90c008e0e29148abf1f352089d217f59 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Wed, 24 Oct 2012 10:12:32 +0100 Subject: [PATCH] Fix exception in win32 code path when CGI exec fails --- mongoose.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mongoose.c b/mongoose.c index 64ee4253..e124135f 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3198,19 +3198,22 @@ static void handle_cgi_request(struct mg_connection *conn, const char *prog) { send_http_error(conn, 500, http_500_error, "Cannot create CGI pipe: %s", strerror(ERRNO)); goto done; - } else if ((pid = spawn_process(conn, p, blk.buf, blk.vars, - fd_stdin[0], fd_stdout[1], dir)) == (pid_t) -1) { - send_http_error(conn, 500, http_500_error, - "Cannot spawn CGI process [%s]: %s", prog, strerror(ERRNO)); - goto done; } + pid = spawn_process(conn, p, blk.buf, blk.vars, fd_stdin[0], fd_stdout[1], + dir); // spawn_process() must close those! // If we don't mark them as closed, close() attempt before // return from this function throws an exception on Windows. // Windows does not like when closed descriptor is closed again. fd_stdin[0] = fd_stdout[1] = -1; + if (pid == (pid_t) -1) { + send_http_error(conn, 500, http_500_error, + "Cannot spawn CGI process [%s]: %s", prog, strerror(ERRNO)); + goto done; + } + if ((in = fdopen(fd_stdin[1], "wb")) == NULL || (out = fdopen(fd_stdout[0], "rb")) == NULL) { send_http_error(conn, 500, http_500_error,