From 6f967c6ef3ea5799b59a5cea6b6f4bec10d25b04 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 21 Sep 2019 13:45:20 +0900 Subject: [PATCH] Fix errors reported by coverity scan --- src/HttpServer.cc | 97 ++++++++++++++++++-------------------- src/shrpx_config.h | 3 +- src/shrpx_connection.cc | 6 +-- src/shrpx_http2_session.cc | 1 + src/shrpx_worker.h | 3 +- 5 files changed, 53 insertions(+), 57 deletions(-) diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 6a627656..5075bc95 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -624,33 +624,30 @@ int Http2Handler::read_clear() { int rv; std::array buf; - for (;;) { - ssize_t nread; - while ((nread = read(fd_, buf.data(), buf.size())) == -1 && errno == EINTR) - ; - if (nread == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - break; - } - return -1; - } - if (nread == 0) { - return -1; + ssize_t nread; + while ((nread = read(fd_, buf.data(), buf.size())) == -1 && errno == EINTR) + ; + if (nread == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + return write_(*this); } + return -1; + } + if (nread == 0) { + return -1; + } - if (get_config()->hexdump) { - util::hexdump(stdout, buf.data(), nread); - } + if (get_config()->hexdump) { + util::hexdump(stdout, buf.data(), nread); + } - rv = nghttp2_session_mem_recv(session_, buf.data(), nread); - if (rv < 0) { - if (rv != NGHTTP2_ERR_BAD_CLIENT_MAGIC) { - std::cerr << "nghttp2_session_mem_recv() returned error: " - << nghttp2_strerror(rv) << std::endl; - } - return -1; + rv = nghttp2_session_mem_recv(session_, buf.data(), nread); + if (rv < 0) { + if (rv != NGHTTP2_ERR_BAD_CLIENT_MAGIC) { + std::cerr << "nghttp2_session_mem_recv() returned error: " + << nghttp2_strerror(rv) << std::endl; } - break; + return -1; } return write_(*this); @@ -746,40 +743,36 @@ int Http2Handler::read_tls() { ERR_clear_error(); - for (;;) { - auto rv = SSL_read(ssl_, buf.data(), buf.size()); + auto rv = SSL_read(ssl_, buf.data(), buf.size()); - if (rv <= 0) { - auto err = SSL_get_error(ssl_, rv); - switch (err) { - case SSL_ERROR_WANT_READ: - goto fin; - case SSL_ERROR_WANT_WRITE: - // renegotiation started - return -1; - default: - return -1; - } - } - - auto nread = rv; - - if (get_config()->hexdump) { - util::hexdump(stdout, buf.data(), nread); - } - - rv = nghttp2_session_mem_recv(session_, buf.data(), nread); - if (rv < 0) { - if (rv != NGHTTP2_ERR_BAD_CLIENT_MAGIC) { - std::cerr << "nghttp2_session_mem_recv() returned error: " - << nghttp2_strerror(rv) << std::endl; - } + if (rv <= 0) { + auto err = SSL_get_error(ssl_, rv); + switch (err) { + case SSL_ERROR_WANT_READ: + return write_(*this); + case SSL_ERROR_WANT_WRITE: + // renegotiation started + return -1; + default: return -1; } - break; } -fin: + auto nread = rv; + + if (get_config()->hexdump) { + util::hexdump(stdout, buf.data(), nread); + } + + rv = nghttp2_session_mem_recv(session_, buf.data(), nread); + if (rv < 0) { + if (rv != NGHTTP2_ERR_BAD_CLIENT_MAGIC) { + std::cerr << "nghttp2_session_mem_recv() returned error: " + << nghttp2_strerror(rv) << std::endl; + } + return -1; + } + return write_(*this); } diff --git a/src/shrpx_config.h b/src/shrpx_config.h index 183a8c93..ccc6b4a9 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -505,7 +505,8 @@ struct DownstreamAddrGroupConfig { DownstreamAddrGroupConfig(const StringRef &pattern) : pattern(pattern), affinity{SessionAffinity::NONE}, - redirect_if_not_tls(false) {} + redirect_if_not_tls(false), + timeout{} {} StringRef pattern; StringRef mruby_file; diff --git a/src/shrpx_connection.cc b/src/shrpx_connection.cc index d1f361d1..1937f45c 100644 --- a/src/shrpx_connection.cc +++ b/src/shrpx_connection.cc @@ -469,9 +469,9 @@ int Connection::tls_handshake() { << ERR_error_string(ERR_get_error(), nullptr); } - struct iovec iov; - auto iovcnt = tls.wbuf.riovec(&iov, 1); - auto nwrite = writev_clear(&iov, iovcnt); + struct iovec iov[1]; + auto iovcnt = tls.wbuf.riovec(iov, 1); + auto nwrite = writev_clear(iov, iovcnt); if (nwrite > 0) { tls.wbuf.drain(nwrite); } diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index ce16aee7..6043a9db 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -1097,6 +1097,7 @@ int on_response_headers(Http2Session *http2session, Downstream *downstream, auto status = resp.fs.header(http2::HD__STATUS); // libnghttp2 guarantees this exists and can be parsed + assert(status); auto status_code = http2::parse_http_status_code(status->value); resp.http_status = status_code; diff --git a/src/shrpx_worker.h b/src/shrpx_worker.h index dfa98582..53f37cb3 100644 --- a/src/shrpx_worker.h +++ b/src/shrpx_worker.h @@ -190,7 +190,8 @@ struct SharedDownstreamAddr { SharedDownstreamAddr() : balloc(1024, 1024), affinity{SessionAffinity::NONE}, - redirect_if_not_tls{false} {} + redirect_if_not_tls{false}, + timeout{} {} SharedDownstreamAddr(const SharedDownstreamAddr &) = delete; SharedDownstreamAddr(SharedDownstreamAddr &&) = delete;