summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-02-24 22:10:08 +0000
committerNick Mathewson <nickm@torproject.org>2008-02-24 22:10:08 +0000
commite0de72dd8720b750c5a9228e6a0b1cf10b34ebec (patch)
treee003d66e64cdfa271652e1e3060a1b8c8725df3a
parentfd8a386040ac12478be508b81d955a3583e9ecc6 (diff)
downloadtor-e0de72dd8720b750c5a9228e6a0b1cf10b34ebec.tar.gz
tor-e0de72dd8720b750c5a9228e6a0b1cf10b34ebec.zip
r14410@tombo: nickm | 2008-02-23 16:51:46 -0500
Fix the last of the -Wshorten-64-to-32 warnings. svn:r13696
-rw-r--r--src/or/connection.c66
-rw-r--r--src/or/eventdns.c75
-rw-r--r--src/or/main.c2
-rw-r--r--src/or/or.h2
4 files changed, 79 insertions, 66 deletions
diff --git a/src/or/connection.c b/src/or/connection.c
index 554d9a29ab..41eee7d8b4 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -814,7 +814,7 @@ connection_create_listener(struct sockaddr *listensockaddr, int type,
}
#ifdef HAVE_SYS_UN_H
} else if (listensockaddr->sa_family == AF_UNIX) {
- int len;
+ size_t len;
start_reading = 1;
/* For now only control ports can be unix domain sockets
@@ -837,7 +837,7 @@ connection_create_listener(struct sockaddr *listensockaddr, int type,
len = strlen(((struct sockaddr_un *)listensockaddr)->sun_path) +
sizeof(((struct sockaddr_un *)listensockaddr)->sun_family);
- if (bind(s, listensockaddr, len) == -1) {
+ if (bind(s, listensockaddr, (socklen_t)len) == -1) {
log_warn(LD_NET,"Bind to %s failed: %s.", address,
tor_socket_strerror(tor_socket_errno(s)));
goto err;
@@ -925,7 +925,7 @@ connection_handle_listener_read(connection_t *conn, int new_type)
struct sockaddr_in remote;
char addrbuf[256];
/* length of the remote address. Must be whatever accept() needs. */
- socklen_t remotelen = sizeof(addrbuf);
+ socklen_t remotelen = (socklen_t)sizeof(addrbuf);
char tmpbuf[INET_NTOA_BUF_LEN];
or_options_t *options = get_options();
@@ -1129,7 +1129,8 @@ connection_connect(connection_t *conn, const char *address,
log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
options->OutboundBindAddress);
} else {
- if (bind(s, (struct sockaddr*)&ext_addr, sizeof(ext_addr)) < 0) {
+ if (bind(s, (struct sockaddr*)&ext_addr,
+ (socklen_t)sizeof(ext_addr)) < 0) {
log_warn(LD_NET,"Error binding network socket: %s",
tor_socket_strerror(tor_socket_errno(s)));
tor_close_socket(s);
@@ -1150,7 +1151,8 @@ connection_connect(connection_t *conn, const char *address,
log_debug(LD_NET,"Connecting to %s:%u.",escaped_safe_str(address),port);
- if (connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
+ if (connect(s,(struct sockaddr *)&dest_addr,
+ (socklen_t)sizeof(dest_addr)) < 0) {
int e = tor_socket_errno(s);
if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
/* yuck. kill it. */
@@ -1437,13 +1439,13 @@ connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
* of a cell on the network; <b>priority</b> says whether we should
* write many of them or just a few; and <b>conn_bucket</b> (if
* non-negative) provides an upper limit for our answer. */
-static int
+static ssize_t
connection_bucket_round_robin(int base, int priority,
- int global_bucket, int conn_bucket)
+ ssize_t global_bucket, ssize_t conn_bucket)
{
- int at_most;
- int num_bytes_high = (priority ? 32 : 16) * base;
- int num_bytes_low = (priority ? 4 : 2) * base;
+ ssize_t at_most;
+ ssize_t num_bytes_high = (priority ? 32 : 16) * base;
+ ssize_t num_bytes_low = (priority ? 4 : 2) * base;
/* Do a rudimentary round-robin so one circuit can't hog a connection.
* Pick at most 32 cells, at least 4 cells if possible, and if we're in
@@ -1467,7 +1469,7 @@ connection_bucket_round_robin(int base, int priority,
}
/** How many bytes at most can we read onto this connection? */
-static int
+static ssize_t
connection_bucket_read_limit(connection_t *conn, time_t now)
{
int base = connection_speaks_cells(conn) ?
@@ -1496,7 +1498,7 @@ connection_bucket_read_limit(connection_t *conn, time_t now)
}
/** How many bytes at most can we write onto this connection? */
-int
+ssize_t
connection_bucket_write_limit(connection_t *conn, time_t now)
{
int base = connection_speaks_cells(conn) ?
@@ -1576,6 +1578,8 @@ connection_buckets_decrement(connection_t *conn, time_t now,
{
if (!connection_is_rate_limited(conn))
return; /* local IPs are free */
+ tor_assert(num_read < INT_MAX);
+ tor_assert(num_written < INT_MAX);
if (num_read > 0)
rep_hist_note_bytes_read(num_read, now);
@@ -1583,13 +1587,13 @@ connection_buckets_decrement(connection_t *conn, time_t now,
rep_hist_note_bytes_written(num_written, now);
if (connection_counts_as_relayed_traffic(conn, now)) {
- global_relayed_read_bucket -= num_read;
- global_relayed_write_bucket -= num_written;
+ global_relayed_read_bucket -= (int)num_read;
+ global_relayed_write_bucket -= (int)num_written;
}
- global_read_bucket -= num_read;
- global_write_bucket -= num_written;
+ global_read_bucket -= (int)num_read;
+ global_write_bucket -= (int)num_written;
if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
- TO_OR_CONN(conn)->read_bucket -= num_read;
+ TO_OR_CONN(conn)->read_bucket -= (int)num_read;
}
/** If we have exhausted our global buckets, or the buckets for conn,
@@ -1907,7 +1911,8 @@ loop_again:
static int
connection_read_to_buf(connection_t *conn, int *max_to_read)
{
- int result, at_most = *max_to_read;
+ int result;
+ ssize_t at_most = *max_to_read;
size_t slack_in_buf, more_to_read;
size_t n_read = 0, n_written = 0;
@@ -1938,10 +1943,10 @@ connection_read_to_buf(connection_t *conn, int *max_to_read)
}
log_debug(LD_NET,
- "%d: starting, inbuf_datalen %d (%d pending in tls object)."
- " at_most %d.",
- conn->s,(int)buf_datalen(conn->inbuf),
- tor_tls_get_pending_bytes(or_conn->tls), at_most);
+ "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
+ " at_most %ld.",
+ conn->s,(long)buf_datalen(conn->inbuf),
+ tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
initial_size = buf_datalen(conn->inbuf);
/* else open, or closing */
@@ -2023,12 +2028,14 @@ connection_read_to_buf(connection_t *conn, int *max_to_read)
}
if (n_read > 0) { /* change *max_to_read */
- *max_to_read = at_most - n_read;
+ /*XXXX021 check for overflow*/
+ *max_to_read = (int)(at_most - n_read);
}
if (conn->type == CONN_TYPE_AP) {
edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
- edge_conn->n_read += n_read;
+ /*XXXX021 check for overflow*/
+ edge_conn->n_read += (int)n_read;
}
connection_buckets_decrement(conn, time(NULL), n_read, n_written);
@@ -2096,9 +2103,9 @@ int
connection_handle_write(connection_t *conn, int force)
{
int e;
- socklen_t len=sizeof(e);
+ socklen_t len=(socklen_t)sizeof(e);
int result;
- int max_to_write;
+ ssize_t max_to_write;
time_t now = time(NULL);
size_t n_read = 0, n_written = 0;
@@ -2148,7 +2155,7 @@ connection_handle_write(connection_t *conn, int force)
return -1;
}
- max_to_write = force ? (int)conn->outbuf_flushlen
+ max_to_write = force ? (ssize_t)conn->outbuf_flushlen
: connection_bucket_write_limit(conn, now);
if (connection_speaks_cells(conn) &&
@@ -2222,7 +2229,8 @@ connection_handle_write(connection_t *conn, int force)
if (conn->type == CONN_TYPE_AP) {
edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
- edge_conn->n_written += n_written;
+ /*XXXX021 check for overflow.*/
+ edge_conn->n_written += (int)n_written;
}
connection_buckets_decrement(conn, time(NULL), n_read, n_written);
@@ -2315,7 +2323,7 @@ _connection_write_to_buf_impl(const char *string, size_t len,
if (zlib) {
conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
} else {
- int extra = 0;
+ ssize_t extra = 0;
conn->outbuf_flushlen += len;
/* Should we try flushing the outbuf now? */
diff --git a/src/or/eventdns.c b/src/or/eventdns.c
index 75d1467d47..34e622b6e6 100644
--- a/src/or/eventdns.c
+++ b/src/or/eventdns.c
@@ -310,7 +310,7 @@ static int global_max_nameserver_timeout = 3;
/* These are the timeout values for nameservers. If we find a nameserver is down */
/* we try to probe it at intervals as given below. Values are in seconds. */
static const struct timeval global_nameserver_timeouts[] = {{10, 0}, {60, 0}, {300, 0}, {900, 0}, {3600, 0}};
-static const int global_nameserver_timeouts_length = sizeof(global_nameserver_timeouts)/sizeof(struct timeval);
+static const int global_nameserver_timeouts_length = (int)(sizeof(global_nameserver_timeouts)/sizeof(struct timeval));
static struct nameserver *nameserver_pick(void);
static void evdns_request_insert(struct request *req, struct request **head);
@@ -754,7 +754,7 @@ reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply)
}
static INLINE int
-name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
+name_parse(u8 *packet, int length, int *idx, char *name_out, size_t name_out_len) {
int name_end = -1;
int j = *idx;
int ptr_count = 0;
@@ -947,7 +947,7 @@ reply_parse(u8 *packet, int length) {
/* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
/* callback. */
static int
-request_parse(u8 *packet, int length, struct evdns_server_port *port, struct sockaddr *addr, socklen_t addrlen)
+request_parse(u8 *packet, ssize_t length, struct evdns_server_port *port, struct sockaddr *addr, socklen_t addrlen)
{
int j = 0; /* index into packet */
u16 _t; /* used by the macros */
@@ -968,6 +968,9 @@ request_parse(u8 *packet, int length, struct evdns_server_port *port, struct soc
if (flags & 0x8000) return -1; /* Must not be an answer. */
flags &= 0x0110; /* Only RD and CD get preserved. */
+ if (length > INT_MAX)
+ return -1;
+
server_req = malloc(sizeof(struct server_request));
if (server_req == NULL) return -1;
memset(server_req, 0, sizeof(struct server_request));
@@ -985,8 +988,8 @@ request_parse(u8 *packet, int length, struct evdns_server_port *port, struct soc
for (i = 0; i < questions; ++i) {
u16 type, class;
struct evdns_server_question *q;
- int namelen;
- if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0)
+ size_t namelen;
+ if (name_parse(packet, (int)length, &j, tmp_name, sizeof(tmp_name))<0)
goto err;
GET16(type);
GET16(class);
@@ -1143,7 +1146,8 @@ nameserver_read(struct nameserver *ns) {
u8 packet[1500];
for (;;) {
- const int r = recv(ns->socket, packet, sizeof(packet), 0);
+ const int r =
+ (int)recv(ns->socket, packet,(socklen_t)sizeof(packet), 0);
if (r < 0) {
int err = last_error(ns->socket);
if (error_is_eagain(err)) return;
@@ -1162,10 +1166,10 @@ server_port_read(struct evdns_server_port *s) {
u8 packet[1500];
struct sockaddr_storage addr;
socklen_t addrlen;
- int r;
+ ssize_t r;
for (;;) {
- addrlen = sizeof(struct sockaddr_storage);
+ addrlen = (socklen_t)sizeof(struct sockaddr_storage);
r = recvfrom(s->socket, packet, sizeof(packet), 0,
(struct sockaddr*) &addr, &addrlen);
if (r < 0) {
@@ -1185,8 +1189,8 @@ server_port_flush(struct evdns_server_port *port)
{
while (port->pending_replies) {
struct server_request *req = port->pending_replies;
- int r = sendto(port->socket, req->response, req->response_len, 0,
- (struct sockaddr*) &req->addr, req->addrlen);
+ ssize_t r = sendto(port->socket, req->response, req->response_len, 0,
+ (struct sockaddr*) &req->addr, (socklen_t)req->addrlen);
if (r < 0) {
int err = last_error(port->socket);
if (error_is_eagain(err))
@@ -1339,7 +1343,7 @@ dnslabel_table_add(struct dnslabel_table *table, const char *label, off_t pos)
/* */
static off_t
dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
- const char *name, const int name_len,
+ const char *name, const size_t name_len,
struct dnslabel_table *table) {
const char *end = name + name_len;
int ref = 0;
@@ -1370,22 +1374,22 @@ dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
}
name = strchr(name, '.');
if (!name) {
- const unsigned int label_len = end - start;
+ const size_t label_len = end - start;
if (label_len > 63) return -1;
if ((size_t)(j+label_len+1) > buf_len) return -2;
if (table) dnslabel_table_add(table, start, j);
- buf[j++] = label_len;
+ buf[j++] = (uint8_t)label_len;
memcpy(buf + j, start, label_len);
j += end - start;
break;
} else {
/* append length of the label. */
- const unsigned int label_len = name - start;
+ const size_t label_len = name - start;
if (label_len > 63) return -1;
if ((size_t)(j+label_len+1) > buf_len) return -2;
if (table) dnslabel_table_add(table, start, j);
- buf[j++] = label_len;
+ buf[j++] = (uint8_t)label_len;
memcpy(buf + j, start, name - start);
j += name - start;
@@ -1406,8 +1410,8 @@ dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
/* Finds the length of a dns request for a DNS name of the given */
/* length. The actual request may be smaller than the value returned */
/* here */
-static int
-evdns_request_len(const int name_len) {
+static size_t
+evdns_request_len(const size_t name_len) {
return 96 + /* length of the DNS standard header */
name_len + 2 +
4; /* space for the resource type */
@@ -1418,7 +1422,7 @@ evdns_request_len(const int name_len) {
/* */
/* Returns the amount of space used. Negative on error. */
static int
-evdns_request_data_build(const char *const name, const int name_len,
+evdns_request_data_build(const char *const name, const size_t name_len,
const u16 trans_id, const u16 type, const u16 class,
u8 *const buf, size_t buf_len) {
off_t j = 0; /* current offset into buf */
@@ -1705,10 +1709,10 @@ evdns_server_request_respond(struct evdns_server_request *_req, int err)
{
struct server_request *req = TO_SERVER_REQUEST(_req);
struct evdns_server_port *port = req->port;
- int r;
+ ssize_t r;
if (!req->response) {
if ((r = evdns_server_request_format_response(req, err))<0)
- return r;
+ return (int)r;
}
r = sendto(port->socket, req->response, req->response_len, 0,
@@ -1896,13 +1900,13 @@ evdns_request_timeout_callback(int fd, short events, void *arg) {
/* 2 other failure */
static int
evdns_request_transmit_to(struct request *req, struct nameserver *server) {
- const int r = send(server->socket, req->request, req->request_len, 0);
+ const ssize_t r = send(server->socket, req->request, req->request_len, 0);
if (r < 0) {
int err = last_error(server->socket);
if (error_is_eagain(err)) return 1;
nameserver_failed(req->ns, strerror(err));
return 2;
- } else if (r != (int)req->request_len) {
+ } else if (r != (ssize_t)req->request_len) {
return 1; /* short write */
} else {
return 0;
@@ -2091,7 +2095,7 @@ evdns_resume(void)
}
static int
-_evdns_nameserver_add_impl(unsigned long int address, int port) {
+_evdns_nameserver_add_impl(u32 address, int port) {
/* first check to see if we already have this nameserver */
const struct nameserver *server = server_head, *const started_at = server_head;
@@ -2124,7 +2128,8 @@ _evdns_nameserver_add_impl(unsigned long int address, int port) {
sin.sin_addr.s_addr = address;
sin.sin_port = htons(port);
sin.sin_family = AF_INET;
- if (connect(ns->socket, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
+ if (connect(ns->socket, (struct sockaddr *) &sin,
+ (socklen_t)sizeof(sin)) != 0) {
err = 2;
goto out2;
}
@@ -2168,7 +2173,7 @@ out1:
/* exported function */
int
evdns_nameserver_add(unsigned long int address) {
- return _evdns_nameserver_add_impl(address, 53);
+ return _evdns_nameserver_add_impl((u32)address, 53);
}
/* exported function */
@@ -2232,8 +2237,8 @@ request_new(int type, const char *name, int flags,
const char issuing_now =
(global_requests_inflight < global_max_requests_inflight) ? 1 : 0;
- const int name_len = strlen(name);
- const int request_max_len = evdns_request_len(name_len);
+ const size_t name_len = strlen(name);
+ const size_t request_max_len = evdns_request_len(name_len);
const u16 trans_id = issuing_now ? transaction_id_pick() : 0xffff;
/* the request data is alloced in a single block with the header */
struct request *const req =
@@ -2370,7 +2375,7 @@ int evdns_resolve_reverse_ipv6(struct in6_addr *in, int flags, evdns_callback_ty
/* to decide that a name is non-local and so try a raw lookup first. */
struct search_domain {
- int len;
+ size_t len;
struct search_domain *next;
/* the text string is appended to this structure */
};
@@ -2426,7 +2431,7 @@ evdns_search_clear(void) {
static void
search_postfix_add(const char *domain) {
- int domain_len;
+ size_t domain_len;
struct search_domain *sdomain;
while (domain[0] == '.') domain++;
domain_len = strlen(domain);
@@ -2488,7 +2493,7 @@ search_set_from_hostname(void) {
/* warning: returns malloced string */
static char *
search_make_new(const struct search_state *const state, int n, const char *const base_name) {
- const int base_len = strlen(base_name);
+ const size_t base_len = strlen(base_name);
const char need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
struct search_domain *dom;
@@ -2497,7 +2502,7 @@ search_make_new(const struct search_state *const state, int n, const char *const
/* this is the postfix we want */
/* the actual postfix string is kept at the end of the structure */
const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
- const int postfix_len = dom->len;
+ const size_t postfix_len = dom->len;
char *const newname = (char *) malloc(base_len + need_to_append_dot + postfix_len + 1);
if (!newname) return NULL;
memcpy(newname, base_name, base_len);
@@ -2626,9 +2631,9 @@ strtok_r(char *s, const char *delim, char **state) {
static int
strtoint(const char *const str) {
char *endptr;
- const int r = strtol(str, &endptr, 10);
- if (*endptr) return -1;
- return r;
+ const long r = strtol(str, &endptr, 10);
+ if (*endptr || r > INT_MAX) return -1;
+ return (int)r;
}
/* helper version of atoi that returns -1 on error and clips to bounds. */
@@ -2766,7 +2771,7 @@ evdns_resolv_conf_parse(int flags, const char *const filename) {
if (!resolv) { err = 4; goto out1; }
n = 0;
- while ((r = read(fd, resolv+n, (size_t)st.st_size-n)) > 0) {
+ while ((r = (int)read(fd, resolv+n, (size_t)st.st_size-n)) > 0) {
n += r;
if (n == st.st_size)
break;
diff --git a/src/or/main.c b/src/or/main.c
index faeca20f0a..396b6f3b91 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -536,7 +536,7 @@ conn_close_if_marked(int i)
if ((conn->s >= 0 || conn->linked_conn) && connection_wants_to_flush(conn)) {
/* s == -1 means it's an incomplete edge connection, or that the socket
* has already been closed as unflushable. */
- int sz = connection_bucket_write_limit(conn, now);
+ ssize_t sz = connection_bucket_write_limit(conn, now);
if (!conn->hold_open_until_flushed)
log_info(LD_NET,
"Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
diff --git a/src/or/or.h b/src/or/or.h
index b0ec610737..f7af3e7c91 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -2707,7 +2707,7 @@ int connection_connect(connection_t *conn, const char *address, uint32_t addr,
int retry_all_listeners(smartlist_t *replaced_conns,
smartlist_t *new_conns);
-int connection_bucket_write_limit(connection_t *conn, time_t now);
+ssize_t connection_bucket_write_limit(connection_t *conn, time_t now);
int global_write_bucket_low(connection_t *conn, size_t attempt, int priority);
void connection_bucket_init(void);
void connection_bucket_refill(int seconds_elapsed, time_t now);