libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit b8e13a57a0035f1f416d593d64115bd4417c2028
parent 8f444c321f95f3d4c20363f397c4bf3a91b09a1d
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Wed, 27 Apr 2022 13:53:05 +0300

Fixed compiler warnings of implicit casting, which could change the value

Diffstat:
Msrc/microhttpd/connection.c | 48+++++++++++++++++++++++++++---------------------
Msrc/microhttpd/daemon.c | 13+++++++------
Msrc/microhttpd/digestauth.c | 8++++----
Msrc/microhttpd/internal.c | 3++-
Msrc/microhttpd/memorypool.c | 2+-
Msrc/microhttpd/mhd_mono_clock.c | 8++++----
Msrc/microhttpd/mhd_send.c | 61++++++++++++++++++++++++++++++++-----------------------------
Msrc/microhttpd/mhd_str.c | 26+++++++++++++-------------
Msrc/microhttpd/postprocessor.c | 27++++++++++++++++-----------
Msrc/microhttpd/response.c | 52+++++++++++++++++++++++++++-------------------------
10 files changed, 133 insertions(+), 115 deletions(-)

diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -1091,7 +1091,7 @@ try_ready_normal_body (struct MHD_Connection *connection) return MHD_NO; } response->data_start = connection->response_write_position; - response->data_size = ret; + response->data_size = (size_t) ret; if (0 == ret) { connection->state = MHD_CONNECTION_NORMAL_BODY_UNREADY; @@ -1190,12 +1190,14 @@ try_ready_chunked_body (struct MHD_Connection *connection, const size_t data_write_offset = (size_t) (connection->response_write_position - response->data_start); /* buffer already ready, use what is there for the chunk */ - ret = response->data_size - data_write_offset; + mhd_assert (SSIZE_MAX < (response->data_size - data_write_offset)); + mhd_assert (response->data_size >= data_write_offset); + ret = (ssize_t) (response->data_size - data_write_offset); if ( ((size_t) ret) > size_to_fill) ret = (ssize_t) size_to_fill; memcpy (&connection->write_buffer[max_chunk_hdr_len], &response->data[data_write_offset], - ret); + (size_t) ret); } else { @@ -1263,10 +1265,10 @@ try_ready_chunked_body (struct MHD_Connection *connection, chunk_hdr_len); connection->write_buffer[max_chunk_hdr_len - 2] = '\r'; connection->write_buffer[max_chunk_hdr_len - 1] = '\n'; - connection->write_buffer[max_chunk_hdr_len + ret] = '\r'; - connection->write_buffer[max_chunk_hdr_len + ret + 1] = '\n'; - connection->response_write_position += ret; - connection->write_buffer_append_offset = max_chunk_hdr_len + ret + 2; + connection->write_buffer[max_chunk_hdr_len + (size_t) ret] = '\r'; + connection->write_buffer[max_chunk_hdr_len + (size_t) ret + 1] = '\n'; + connection->response_write_position += (size_t) ret; + connection->write_buffer_append_offset = max_chunk_hdr_len + (size_t) ret + 2; return MHD_YES; } @@ -2138,7 +2140,7 @@ build_header_response (struct MHD_Connection *connection) if (buf_size < pos + 5) /* space + code + space */ return MHD_NO; buf[pos++] = ' '; - pos += MHD_uint16_to_str (rcode, buf + pos, + pos += MHD_uint16_to_str ((uint16_t) rcode, buf + pos, buf_size - pos); buf[pos++] = ' '; @@ -2819,10 +2821,11 @@ parse_cookie_header (struct MHD_Connection *connection) if (old != '=') { /* value part omitted, use empty string... */ + mhd_assert (ekill >= pos); if (MHD_NO == connection_add_header (connection, pos, - ekill - pos + 1, + (size_t) (ekill - pos + 1), "", 0, MHD_COOKIE_KIND)) @@ -2860,12 +2863,14 @@ parse_cookie_header (struct MHD_Connection *connection) end--; *end = '\0'; } + mhd_assert (ekill >= pos); + mhd_assert (end >= equals); if (MHD_NO == connection_add_header (connection, pos, - ekill - pos + 1, + (size_t) (ekill - pos + 1), equals, - end - equals, + (size_t) (end - equals), MHD_COOKIE_KIND)) return MHD_NO; pos = semicolon; @@ -3057,16 +3062,17 @@ parse_initial_message_line (struct MHD_Connection *connection, connection->version = http_version + 1; if (MHD_NO == parse_http_version (connection, connection->version, line_len - - (connection->version - line))) + - (size_t) + (connection->version - line))) return MHD_NO; - uri_len = http_version - uri; + uri_len = (size_t) (http_version - uri); } else { connection->version = ""; if (MHD_NO == parse_http_version (connection, connection->version, 0)) return MHD_NO; - uri_len = line_len - (uri - line); + uri_len = line_len - (size_t) (uri - line); } /* check for spaces in URI if we are "strict" */ if ( (1 <= daemon->strict_for_client) && @@ -3845,7 +3851,7 @@ MHD_connection_handle_read (struct MHD_Connection *connection, MHD_REQUEST_TERMINATED_WITH_ERROR); return; } - connection->read_buffer_offset += bytes_read; + connection->read_buffer_offset += (size_t) bytes_read; MHD_update_last_activity_ (connection); #if DEBUG_STATES MHD_DLOG (connection->daemon, @@ -3969,7 +3975,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection) (int) ret, &HTTP_100_CONTINUE[connection->continue_message_write_offset]); #endif - connection->continue_message_write_offset += ret; + connection->continue_message_write_offset += (size_t) ret; MHD_update_last_activity_ (connection); return; case MHD_CONNECTION_CONTINUE_SENT: @@ -4058,10 +4064,10 @@ MHD_connection_handle_write (struct MHD_Connection *connection) mhd_assert (! connection->rp_props.chunked); mhd_assert (connection->rp_props.send_reply_body); connection->write_buffer_send_offset += wb_ready; - connection->response_write_position = ret - wb_ready; + connection->response_write_position = ((size_t) ret) - wb_ready; } else - connection->write_buffer_send_offset += ret; + connection->write_buffer_send_offset += (size_t) ret; MHD_update_last_activity_ (connection); if (MHD_CONNECTION_HEADERS_SENDING != connection->state) return; @@ -4142,7 +4148,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection) NULL); return; } - connection->response_write_position += ret; + connection->response_write_position += (size_t) ret; MHD_update_last_activity_ (connection); } if (connection->response_write_position == @@ -4174,7 +4180,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection) NULL); return; } - connection->write_buffer_send_offset += ret; + connection->write_buffer_send_offset += (size_t) ret; MHD_update_last_activity_ (connection); if (MHD_CONNECTION_CHUNKED_BODY_READY != connection->state) return; @@ -4210,7 +4216,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection) NULL); return; } - connection->write_buffer_send_offset += ret; + connection->write_buffer_send_offset += (size_t) ret; MHD_update_last_activity_ (connection); if (MHD_CONNECTION_FOOTERS_SENDING != connection->state) return; diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -1429,7 +1429,7 @@ process_urh (struct MHD_UpgradeResponseHandle *urh) } else /* 0 < res */ { - urh->in_buffer_used += res; + urh->in_buffer_used += (size_t) res; if (0 < gnutls_record_check_pending (connection->tls_session)) { connection->tls_read_ready = true; @@ -1484,7 +1484,7 @@ process_urh (struct MHD_UpgradeResponseHandle *urh) } else /* 0 < res */ { - urh->out_buffer_used += res; + urh->out_buffer_used += (size_t) res; if (buf_size > (size_t) res) urh->mhd.celi &= ~((enum MHD_EpollState) MHD_EPOLL_STATE_READ_READY); } @@ -1531,7 +1531,7 @@ process_urh (struct MHD_UpgradeResponseHandle *urh) "%" PRIu64 \ " bytes of data received from application: %s\n"), (uint64_t) urh->out_buffer_used, - gnutls_strerror (res)); + gnutls_strerror ((int) res)); #endif /* Discard any data unsent to remote. */ urh->out_buffer_used = 0; @@ -1543,7 +1543,7 @@ process_urh (struct MHD_UpgradeResponseHandle *urh) } else /* 0 < res */ { - const size_t next_out_buffer_used = urh->out_buffer_used - res; + const size_t next_out_buffer_used = urh->out_buffer_used - (size_t) res; if (0 != next_out_buffer_used) { memmove (urh->out_buffer, @@ -1614,7 +1614,7 @@ process_urh (struct MHD_UpgradeResponseHandle *urh) } else /* 0 < res */ { - const size_t next_in_buffer_used = urh->in_buffer_used - res; + const size_t next_in_buffer_used = urh->in_buffer_used - (size_t) res; if (0 != next_in_buffer_used) { memmove (urh->in_buffer, @@ -3535,7 +3535,8 @@ MHD_add_connection (struct MHD_Daemon *daemon, for (i = 0; i < daemon->worker_pool_size; ++i) { struct MHD_Daemon *const worker = - &daemon->worker_pool[(i + client_socket) % daemon->worker_pool_size]; + &daemon->worker_pool[(i + (unsigned int) client_socket) + % daemon->worker_pool_size]; if (worker->connections < worker->connection_limit) return internal_add_connection (worker, client_socket, diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c @@ -480,7 +480,7 @@ lookup_sub_value (char *dest, else { if (size > (size_t) ((q2 - q1) + 1)) - size = (q2 - q1) + 1; + size = (size_t) (q2 - q1) + 1; size--; memcpy (dest, q1, @@ -541,7 +541,7 @@ check_nonce_nc (struct MHD_Connection *connection, np = nonce; while ('\0' != *np) { - off = (off << 8) | (*np ^ (off >> 24)); + off = (off << 8) | (((uint8_t) *np) ^ (off >> 24)); np++; } off = off % mod; @@ -1399,7 +1399,7 @@ MHD_queue_auth_fail_response2 (struct MHD_Connection *connection, char *header; header = MHD_calloc_ (1, - hlen + 1); + (size_t) hlen + 1); if (NULL == header) { #ifdef HAVE_MESSAGES @@ -1410,7 +1410,7 @@ MHD_queue_auth_fail_response2 (struct MHD_Connection *connection, } if (MHD_snprintf_ (header, - hlen + 1, + (size_t) hlen + 1, "Digest realm=\"%s\",qop=\"auth\",nonce=\"%s\",opaque=\"%s\",algorithm=%s%s", realm, nonce, diff --git a/src/microhttpd/internal.c b/src/microhttpd/internal.c @@ -172,7 +172,8 @@ MHD_http_unescape (char *val) } } *wpos = '\0'; /* add 0-terminator */ - return wpos - val; + mhd_assert (wpos >= val); + return (size_t) (wpos - val); } diff --git a/src/microhttpd/memorypool.c b/src/microhttpd/memorypool.c @@ -447,7 +447,7 @@ MHD_pool_reallocate (struct MemoryPool *pool, if (NULL != old) { /* Have previously allocated data */ - const size_t old_offset = (uint8_t *) old - pool->memory; + const size_t old_offset = (size_t) (((uint8_t *) old) - pool->memory); const bool shrinking = (old_size > new_size); /* Try resizing in-place */ if (shrinking) diff --git a/src/microhttpd/mhd_mono_clock.c b/src/microhttpd/mhd_mono_clock.c @@ -448,7 +448,7 @@ MHD_monotonic_msec_counter (void) (0 == clock_gettime (mono_clock_id, &ts)) ) return (uint64_t) (((uint64_t) (ts.tv_sec - mono_clock_start)) * 1000 - + (ts.tv_nsec / 1000000)); + + (uint64_t) (ts.tv_nsec / 1000000)); #endif /* HAVE_CLOCK_GETTIME */ #ifdef HAVE_CLOCK_GET_TIME if (_MHD_INVALID_CLOCK_SERV != mono_clock_service) @@ -458,7 +458,7 @@ MHD_monotonic_msec_counter (void) if (KERN_SUCCESS == clock_get_time (mono_clock_service, &cur_time)) return (uint64_t) (((uint64_t) (cur_time.tv_sec - mono_clock_start)) - * 1000 + (cur_time.tv_nsec / 1000000)); + * 1000 + (uint64_t) (cur_time.tv_nsec / 1000000)); } #endif /* HAVE_CLOCK_GET_TIME */ #if defined(_WIN32) @@ -487,14 +487,14 @@ MHD_monotonic_msec_counter (void) #ifdef HAVE_TIMESPEC_GET if (TIME_UTC == timespec_get (&ts, TIME_UTC)) return (uint64_t) (((uint64_t) (ts.tv_sec - gettime_start)) * 1000 - + (ts.tv_nsec / 1000000)); + + (uint64_t) (ts.tv_nsec / 1000000)); #elif defined(HAVE_GETTIMEOFDAY) if (1) { struct timeval tv; if (0 == gettimeofday (&tv, NULL)) return (uint64_t) (((uint64_t) (tv.tv_sec - gettime_start)) * 1000 - + (tv.tv_usec / 1000)); + + (uint64_t) (tv.tv_usec / 1000)); } #endif /* HAVE_GETTIMEOFDAY */ diff --git a/src/microhttpd/mhd_send.c b/src/microhttpd/mhd_send.c @@ -125,7 +125,7 @@ iov_max_init_ (void) { long res = sysconf (_SC_IOV_MAX); if (res >= 0) - mhd_iov_max_ = res; + mhd_iov_max_ = (unsigned long) res; #if defined(IOV_MAX) else mhd_iov_max_ = IOV_MAX; @@ -1399,7 +1399,6 @@ send_iov_nontls (struct MHD_Connection *connection, bool push_data) { ssize_t res; - ssize_t total_sent; size_t items_to_send; #ifdef HAVE_SENDMSG struct msghdr msg; @@ -1501,35 +1500,38 @@ send_iov_nontls (struct MHD_Connection *connection, } /* Some data has been sent */ - total_sent = res; - /* Adjust the internal tracking information for the iovec to - * take this last send into account. */ - while ((0 != res) && (r_iov->iov[r_iov->sent].iov_len <= (size_t) res)) + if (1) { - res -= r_iov->iov[r_iov->sent].iov_len; - r_iov->sent++; /* The iov element has been completely sent */ - mhd_assert ((r_iov->cnt > r_iov->sent) || (0 == res)); - } + size_t track_sent = (size_t) res; + /* Adjust the internal tracking information for the iovec to + * take this last send into account. */ + while ((0 != track_sent) && (r_iov->iov[r_iov->sent].iov_len <= track_sent)) + { + track_sent -= r_iov->iov[r_iov->sent].iov_len; + r_iov->sent++; /* The iov element has been completely sent */ + mhd_assert ((r_iov->cnt > r_iov->sent) || (0 == track_sent)); + } - if (r_iov->cnt == r_iov->sent) - post_send_setopt (connection, true, push_data); - else - { + if (r_iov->cnt == r_iov->sent) + post_send_setopt (connection, true, push_data); + else + { #ifdef EPOLL_SUPPORT - connection->epoll_state &= - ~((enum MHD_EpollState) MHD_EPOLL_STATE_WRITE_READY); + connection->epoll_state &= + ~((enum MHD_EpollState) MHD_EPOLL_STATE_WRITE_READY); #endif /* EPOLL_SUPPORT */ - if (0 != res) - { - mhd_assert (r_iov->cnt > r_iov->sent); - /* The last iov element has been partially sent */ - r_iov->iov[r_iov->sent].iov_base = - (void *) ((uint8_t *) r_iov->iov[r_iov->sent].iov_base + (size_t) res); - r_iov->iov[r_iov->sent].iov_len -= (MHD_iov_size_) res; + if (0 != track_sent) + { + mhd_assert (r_iov->cnt > r_iov->sent); + /* The last iov element has been partially sent */ + r_iov->iov[r_iov->sent].iov_base = + (void *) ((uint8_t *) r_iov->iov[r_iov->sent].iov_base + track_sent); + r_iov->iov[r_iov->sent].iov_len -= (MHD_iov_size_) track_sent; + } } } - return total_sent; + return res; } @@ -1567,7 +1569,7 @@ send_iov_emu (struct MHD_Connection *connection, do { if ((size_t) SSIZE_MAX - total_sent < r_iov->iov[r_iov->sent].iov_len) - return total_sent; /* return value would overflow */ + return (ssize_t) total_sent; /* return value would overflow */ res = MHD_send_data_ (connection, r_iov->iov[r_iov->sent].iov_base, @@ -1580,7 +1582,7 @@ send_iov_emu (struct MHD_Connection *connection, return res; /* Nothing was sent, return result as is */ if (MHD_ERR_AGAIN_ == res) - return total_sent; /* Some data has been sent, return the amount */ + return (ssize_t) total_sent; /* Return the amount of the sent data */ return res; /* Any kind of a hard error */ } @@ -1589,13 +1591,14 @@ send_iov_emu (struct MHD_Connection *connection, if (r_iov->iov[r_iov->sent].iov_len != (size_t) res) { + const size_t sent = (size_t) res; /* Incomplete buffer has been sent. * Adjust buffer of the last element. */ r_iov->iov[r_iov->sent].iov_base = - (void *) ((uint8_t *) r_iov->iov[r_iov->sent].iov_base + (size_t) res); - r_iov->iov[r_iov->sent].iov_len -= res; + (void *) ((uint8_t *) r_iov->iov[r_iov->sent].iov_base + sent); + r_iov->iov[r_iov->sent].iov_len -= sent; - return total_sent; + return (ssize_t) total_sent; } /* The iov element has been completely sent */ r_iov->sent++; diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c @@ -899,12 +899,12 @@ MHD_str_to_uint64_ (const char *str, return 0; res *= 10; - res += digit; + res += (unsigned int) digit; str++; } while (isasciidigit (*str)); *out_val = res; - return str - start; + return (size_t) (str - start); } @@ -944,7 +944,7 @@ MHD_str_to_uint64_n_ (const char *str, return 0; res *= 10; - res += digit; + res += (unsigned int) digit; i++; } while ( (i < maxlen) && isasciidigit (str[i]) ); @@ -984,7 +984,7 @@ MHD_strx_to_uint32_ (const char *str, % 16)) ) ) { res *= 16; - res += digit; + res += (unsigned int) digit; } else return 0; @@ -994,7 +994,7 @@ MHD_strx_to_uint32_ (const char *str, if (str - start > 0) *out_val = res; - return str - start; + return (size_t) (str - start); } @@ -1032,7 +1032,7 @@ MHD_strx_to_uint32_n_ (const char *str, return 0; res *= 16; - res += digit; + res += (unsigned int) digit; i++; } @@ -1071,7 +1071,7 @@ MHD_strx_to_uint64_ (const char *str, % 16)) ) ) { res *= 16; - res += digit; + res += (unsigned int) digit; } else return 0; @@ -1081,7 +1081,7 @@ MHD_strx_to_uint64_ (const char *str, if (str - start > 0) *out_val = res; - return str - start; + return (size_t) (str - start); } @@ -1119,7 +1119,7 @@ MHD_strx_to_uint64_n_ (const char *str, return 0; res *= 16; - res += digit; + res += (unsigned int) digit; i++; } @@ -1178,7 +1178,7 @@ MHD_str_to_uvalue_n_ (const char *str, return 0; res *= base; - res += digit; + res += (unsigned int) digit; i++; } @@ -1330,7 +1330,7 @@ MHD_uint8_to_str_pad (uint8_t val, } else { - buf[pos++] = '0' + digit; + buf[pos++] = '0' + (char) digit; val %= 100; min_digits = 2; } @@ -1345,13 +1345,13 @@ MHD_uint8_to_str_pad (uint8_t val, } else { - buf[pos++] = '0' + digit; + buf[pos++] = '0' + (char) digit; val %= 10; } if (buf_size <= pos) return 0; - buf[pos++] = '0' + val; + buf[pos++] = '0' + (char) val; return pos; } diff --git a/src/microhttpd/postprocessor.c b/src/microhttpd/postprocessor.c @@ -367,20 +367,23 @@ process_value (struct MHD_PostProcessor *pp, if ( (NULL != last_escape) && (((size_t) (value_end - last_escape)) < sizeof (pp->xbuf)) ) { - pp->xbuf_pos = value_end - last_escape; + mhd_assert (value_end >= last_escape); + pp->xbuf_pos = (size_t) (value_end - last_escape); memcpy (pp->xbuf, last_escape, - value_end - last_escape); + (size_t) (value_end - last_escape)); value_end = last_escape; } while ( (value_start != value_end) || (pp->must_ikvi) || (xoff > 0) ) { - size_t delta = value_end - value_start; + size_t delta = (size_t) (value_end - value_start); bool cut = false; size_t clen = 0; + mhd_assert (value_end >= value_start); + if (delta > XBUF_SIZE - xoff) delta = XBUF_SIZE - xoff; /* move (additional) input into processing buffer */ @@ -660,7 +663,8 @@ post_process_urlencoded (struct MHD_PostProcessor *pp, mhd_assert ((NULL != end_key) || (NULL == start_key)); if (1) { - const size_t key_len = end_key - start_key; + const size_t key_len = (size_t) (end_key - start_key); + mhd_assert (end_key >= start_key); if (0 != key_len) { if ( (pp->buffer_pos + key_len >= pp->buffer_size) || @@ -725,7 +729,8 @@ post_process_urlencoded (struct MHD_PostProcessor *pp, mhd_assert ((PP_ProcessKey == pp->state) || (NULL != end_key)); if (NULL == end_key) end_key = &post_data[poff]; - key_len = end_key - start_key; + mhd_assert (end_key >= start_key); + key_len = (size_t) (end_key - start_key); mhd_assert (0 != key_len); /* it must be always non-zero here */ if (pp->buffer_pos + key_len >= pp->buffer_size) { @@ -854,13 +859,13 @@ find_boundary (struct MHD_PostProcessor *pp, '-', pp->buffer_pos); if (NULL == dash) - (*ioffptr) += pp->buffer_pos; /* skip entire buffer */ + (*ioffptr) += pp->buffer_pos; /* skip entire buffer */ else if (dash == buf) - (*ioffptr)++; /* at least skip one byte */ + (*ioffptr)++; /* at least skip one byte */ else - (*ioffptr) += dash - buf; /* skip to first possible boundary */ + (*ioffptr) += (size_t) (dash - buf); /* skip to first possible boundary */ } - return MHD_NO; /* expected boundary */ + return MHD_NO; /* expected boundary */ } /* remove boundary from buffer */ (*ioffptr) += 2 + blen; @@ -908,7 +913,7 @@ try_get_value (const char *buf, if (NULL == (endv = strchr (&spos[klen + 2], '\"'))) return; /* no end-quote */ - vlen = endv - spos - klen - 1; + vlen = (size_t) (endv - spos) - klen - 1; *destination = malloc (vlen); if (NULL == *destination) return; /* out of memory */ @@ -1037,7 +1042,7 @@ process_value_to_boundary (struct MHD_PostProcessor *pp, newline = pp->buffer_pos - 4; break; } - newline = r - buf; + newline = (size_t) (r - buf); if (0 == memcmp ("\r\n--", &buf[newline], 4)) diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c @@ -213,11 +213,11 @@ add_response_header_connection (struct MHD_Response *response, /** the length of the "Connection" key */ static const size_t key_len = MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_CONNECTION); - size_t value_len; /**< the length of the @a value */ + size_t value_len; /**< the length of the @a value */ size_t old_value_len; /**< the length of the existing "Connection" value */ - size_t buf_size; /**< the size of the buffer */ - ssize_t norm_len; /**< the length of the normalised value */ - char *buf; /**< the temporal buffer */ + size_t buf_size; /**< the size of the buffer */ + size_t norm_len; /**< the length of the normalised value */ + char *buf; /**< the temporal buffer */ struct MHD_HTTP_Res_Header *hdr; /**< existing "Connection" header */ bool value_has_close; /**< the @a value has "close" token */ bool already_has_close; /**< existing "Connection" header has "close" token */ @@ -253,19 +253,27 @@ add_response_header_connection (struct MHD_Response *response, value_len = strlen (value); if (value_len >= SSIZE_MAX) return MHD_NO; - /* Additional space for normalisation and zero-termination*/ - norm_len = (ssize_t) (value_len + value_len / 2 + 1); + /* Additional space for normalisation and zero-termination */ + norm_len = value_len + value_len / 2 + 1; buf_size = old_value_len + (size_t) norm_len; buf = malloc (buf_size); if (NULL == buf) return MHD_NO; - /* Remove "close" token (if any), it will be moved to the front */ - value_has_close = MHD_str_remove_token_caseless_ (value, value_len, "close", - MHD_STATICSTR_LEN_ ( \ - "close"), - buf + old_value_len, - &norm_len); + if (1) + { /* local scope */ + ssize_t norm_len_s = (ssize_t) norm_len; + /* Remove "close" token (if any), it will be moved to the front */ + value_has_close = MHD_str_remove_token_caseless_ (value, value_len, "close", + MHD_STATICSTR_LEN_ ( \ + "close"), + buf + old_value_len, + &norm_len_s); + mhd_assert (0 <= norm_len_s); + if (0 > norm_len_s) + norm_len = 0; /* Must never happen */ + norm_len = (size_t) norm_len; + } #ifdef UPGRADE_SUPPORT if ( (NULL != response->upgrade_handler) && value_has_close) { /* The "close" token cannot be used with connection "upgrade" */ @@ -273,17 +281,10 @@ add_response_header_connection (struct MHD_Response *response, return MHD_NO; } #endif /* UPGRADE_SUPPORT */ - mhd_assert (0 <= norm_len); - if (0 > norm_len) - norm_len = 0; /* Must never happen */ if (0 != norm_len) - { - size_t len = norm_len; - MHD_str_remove_tokens_caseless_ (buf + old_value_len, &len, + MHD_str_remove_tokens_caseless_ (buf + old_value_len, &norm_len, "keep-alive", MHD_STATICSTR_LEN_ ("keep-alive")); - norm_len = (ssize_t) len; - } if (0 == norm_len) { /* New value is empty after normalisation */ if (! value_has_close) @@ -301,7 +302,7 @@ add_response_header_connection (struct MHD_Response *response, if (value_has_close && ! already_has_close) { /* Need to insert "close" token at the first position */ - mhd_assert (buf_size >= old_value_len + (size_t) norm_len \ + mhd_assert (buf_size >= old_value_len + norm_len \ + MHD_STATICSTR_LEN_ ("close, ") + 1); if (0 != norm_len) memmove (buf + MHD_STATICSTR_LEN_ ("close, ") + old_value_len, @@ -333,7 +334,7 @@ add_response_header_connection (struct MHD_Response *response, mhd_assert ((value_has_close && ! already_has_close) ? \ (MHD_STATICSTR_LEN_ ("close, ") + old_value_len == pos) : \ (old_value_len == pos)); - pos += (size_t) norm_len; + pos += norm_len; } mhd_assert (buf_size > pos); buf[pos] = 0; /* Null terminate the result */ @@ -1118,7 +1119,7 @@ MHD_create_response_from_fd_at_offset (size_t size, { return MHD_create_response_from_fd_at_offset64 (size, fd, - offset); + (uint64_t) offset); } @@ -1631,7 +1632,7 @@ MHD_create_response_from_iovec (const struct MHD_IoVec *iov, MHD_iovec_ *iov_copy; int num_copy_elements = i_cp; - iov_copy = MHD_calloc_ (num_copy_elements, + iov_copy = MHD_calloc_ ((size_t) num_copy_elements, \ sizeof(MHD_iovec_)); if (NULL == iov_copy) { @@ -1662,8 +1663,9 @@ MHD_create_response_from_iovec (const struct MHD_IoVec *iov, i_cp++; } mhd_assert (num_copy_elements == i_cp); + mhd_assert (0 <= i_cp); response->data_iov = iov_copy; - response->data_iovcnt = i_cp; + response->data_iovcnt = (unsigned int) i_cp; } return response; }