diff options
author | Craig Tiller <ctiller@google.com> | 2015-06-23 09:21:11 -0700 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2015-06-23 09:21:11 -0700 |
commit | f8fedc43d76f171b8f87bb65dc5d253e67db996f (patch) | |
tree | ba0c863805571971021101f6d6d440603a191b66 /src/core/support | |
parent | 60401f13b7113f11b456d8d0c01fa13c3d7a4632 (diff) | |
parent | 84976a70ce07f13c959e8630b5c10d4fbbace4d8 (diff) |
Merge pull request #2141 from murgatroid99/core_support_conversion_warnings
Fix integer conversion warnings in src/core/support
Diffstat (limited to 'src/core/support')
-rw-r--r-- | src/core/support/cmdline.c | 6 | ||||
-rw-r--r-- | src/core/support/cpu_linux.c | 8 | ||||
-rw-r--r-- | src/core/support/file.c | 3 | ||||
-rw-r--r-- | src/core/support/histogram.c | 6 | ||||
-rw-r--r-- | src/core/support/host_port.c | 4 | ||||
-rw-r--r-- | src/core/support/murmur_hash.c | 14 | ||||
-rw-r--r-- | src/core/support/slice.c | 28 | ||||
-rw-r--r-- | src/core/support/slice_buffer.c | 8 | ||||
-rw-r--r-- | src/core/support/string.c | 6 | ||||
-rw-r--r-- | src/core/support/subprocess_posix.c | 4 | ||||
-rw-r--r-- | src/core/support/time.c | 16 | ||||
-rw-r--r-- | src/core/support/time_posix.c | 2 |
12 files changed, 55 insertions, 50 deletions
diff --git a/src/core/support/cmdline.c b/src/core/support/cmdline.c index 4baad85040..45a3182f73 100644 --- a/src/core/support/cmdline.c +++ b/src/core/support/cmdline.c @@ -228,7 +228,7 @@ static void value_state(gpr_cmdline *cl, char *arg) { cl->cur_arg->name); print_usage_and_die(cl); } - *(int *)cl->cur_arg->value = intval; + *(int *)cl->cur_arg->value = (int)intval; break; case ARGTYPE_BOOL: if (0 == strcmp(arg, "1") || 0 == strcmp(arg, "true")) { @@ -287,8 +287,8 @@ static void normal_state(gpr_cmdline *cl, char *arg) { eq = strchr(arg, '='); if (eq != NULL) { /* copy the string into a temp buffer and extract the name */ - tmp = arg_name = gpr_malloc(eq - arg + 1); - memcpy(arg_name, arg, eq - arg); + tmp = arg_name = gpr_malloc((size_t)(eq - arg + 1)); + memcpy(arg_name, arg, (size_t)(eq - arg)); arg_name[eq - arg] = 0; } else { arg_name = arg; diff --git a/src/core/support/cpu_linux.c b/src/core/support/cpu_linux.c index 37e840d4cf..282d4daab1 100644 --- a/src/core/support/cpu_linux.c +++ b/src/core/support/cpu_linux.c @@ -51,7 +51,9 @@ static int ncpus = 0; static void init_num_cpus() { - ncpus = sysconf(_SC_NPROCESSORS_ONLN); + /* This must be signed. sysconf returns -1 when the number cannot be + determined */ + ncpus = (int)sysconf(_SC_NPROCESSORS_ONLN); if (ncpus < 1) { gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1"); ncpus = 1; @@ -61,7 +63,7 @@ static void init_num_cpus() { unsigned gpr_cpu_num_cores(void) { static gpr_once once = GPR_ONCE_INIT; gpr_once_init(&once, init_num_cpus); - return ncpus; + return (unsigned)ncpus; } unsigned gpr_cpu_current_cpu(void) { @@ -70,7 +72,7 @@ unsigned gpr_cpu_current_cpu(void) { gpr_log(GPR_ERROR, "Error determining current CPU: %s\n", strerror(errno)); return 0; } - return cpu; + return (unsigned)cpu; } #endif /* GPR_CPU_LINUX */ diff --git a/src/core/support/file.c b/src/core/support/file.c index 8ce7a67fb1..c1361d8a9e 100644 --- a/src/core/support/file.c +++ b/src/core/support/file.c @@ -58,7 +58,8 @@ gpr_slice gpr_load_file(const char *filename, int add_null_terminator, goto end; } fseek(file, 0, SEEK_END); - contents_size = ftell(file); + /* Converting to size_t on the assumption that it will not fail */ + contents_size = (size_t)ftell(file); fseek(file, 0, SEEK_SET); contents = gpr_malloc(contents_size + (add_null_terminator ? 1 : 0)); bytes_read = fread(contents, 1, contents_size, file); diff --git a/src/core/support/histogram.c b/src/core/support/histogram.c index 673affde71..2f1adcf511 100644 --- a/src/core/support/histogram.c +++ b/src/core/support/histogram.c @@ -189,12 +189,12 @@ static double threshold_for_count_below(gpr_histogram *h, double count_below) { break; } } - return (bucket_start(h, lower_idx) + bucket_start(h, upper_idx)) / 2.0; + return (bucket_start(h, (double)lower_idx) + bucket_start(h, (double)upper_idx)) / 2.0; } else { /* treat values as uniform throughout the bucket, and find where this value should lie */ - lower_bound = bucket_start(h, lower_idx); - upper_bound = bucket_start(h, lower_idx + 1); + lower_bound = bucket_start(h, (double)lower_idx); + upper_bound = bucket_start(h, (double)(lower_idx + 1)); return GPR_CLAMP(upper_bound - (upper_bound - lower_bound) * (count_so_far - count_below) / h->buckets[lower_idx], diff --git a/src/core/support/host_port.c b/src/core/support/host_port.c index 53669f063b..0906ebc2a3 100644 --- a/src/core/support/host_port.c +++ b/src/core/support/host_port.c @@ -76,7 +76,7 @@ void gpr_split_host_port(const char *name, char **host, char **port) { return; } host_start = name + 1; - host_len = rbracket - host_start; + host_len = (size_t)(rbracket - host_start); if (memchr(host_start, ':', host_len) == NULL) { /* Require all bracketed hosts to contain a colon, because a hostname or IPv4 address should never use brackets. */ @@ -87,7 +87,7 @@ void gpr_split_host_port(const char *name, char **host, char **port) { if (colon != NULL && strchr(colon + 1, ':') == NULL) { /* Exactly 1 colon. Split into host:port. */ host_start = name; - host_len = colon - name; + host_len = (size_t)(colon - name); port_start = colon + 1; } else { /* 0 or 2+ colons. Bare hostname or IPv6 litearal. */ diff --git a/src/core/support/murmur_hash.c b/src/core/support/murmur_hash.c index cc84691508..37fdca82ba 100644 --- a/src/core/support/murmur_hash.c +++ b/src/core/support/murmur_hash.c @@ -48,7 +48,7 @@ gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed) { const gpr_uint8 *data = (const gpr_uint8 *)key; - const int nblocks = len / 4; + const size_t nblocks = len / 4; int i; gpr_uint32 h1 = seed; @@ -57,11 +57,11 @@ gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed) { const gpr_uint32 c1 = 0xcc9e2d51; const gpr_uint32 c2 = 0x1b873593; - const gpr_uint32 *blocks = (const uint32_t *)(data + nblocks * 4); - const uint8_t *tail = (const uint8_t *)(data + nblocks * 4); + const gpr_uint32 *blocks = ((const gpr_uint32 *)key) + nblocks; + const gpr_uint8 *tail = (const gpr_uint8 *)(data + nblocks * 4); /* body */ - for (i = -nblocks; i; i++) { + for (i = -(int)nblocks; i; i++) { k1 = GETBLOCK32(blocks, i); k1 *= c1; @@ -78,9 +78,9 @@ gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed) { /* tail */ switch (len & 3) { case 3: - k1 ^= tail[2] << 16; + k1 ^= ((gpr_uint32)tail[2]) << 16; case 2: - k1 ^= tail[1] << 8; + k1 ^= ((gpr_uint32)tail[1]) << 8; case 1: k1 ^= tail[0]; k1 *= c1; @@ -90,7 +90,7 @@ gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed) { }; /* finalization */ - h1 ^= len; + h1 ^= (gpr_uint32)len; FMIX32(h1); return h1; } diff --git a/src/core/support/slice.c b/src/core/support/slice.c index 4cff029286..a2d62fc1e5 100644 --- a/src/core/support/slice.c +++ b/src/core/support/slice.c @@ -194,7 +194,7 @@ gpr_slice gpr_slice_malloc(size_t length) { } else { /* small slice: just inline the data */ slice.refcount = NULL; - slice.data.inlined.length = length; + slice.data.inlined.length = (gpr_uint8)length; } return slice; } @@ -202,11 +202,11 @@ gpr_slice gpr_slice_malloc(size_t length) { gpr_slice gpr_slice_sub_no_ref(gpr_slice source, size_t begin, size_t end) { gpr_slice subset; + GPR_ASSERT(end >= begin); + if (source.refcount) { /* Enforce preconditions */ - GPR_ASSERT(source.data.refcounted.length >= begin); GPR_ASSERT(source.data.refcounted.length >= end); - GPR_ASSERT(end >= begin); /* Build the result */ subset.refcount = source.refcount; @@ -214,8 +214,10 @@ gpr_slice gpr_slice_sub_no_ref(gpr_slice source, size_t begin, size_t end) { subset.data.refcounted.bytes = source.data.refcounted.bytes + begin; subset.data.refcounted.length = end - begin; } else { + /* Enforce preconditions */ + GPR_ASSERT(source.data.inlined.length >= end); subset.refcount = NULL; - subset.data.inlined.length = end - begin; + subset.data.inlined.length = (gpr_uint8)(end - begin); memcpy(subset.data.inlined.bytes, source.data.inlined.bytes + begin, end - begin); } @@ -227,7 +229,7 @@ gpr_slice gpr_slice_sub(gpr_slice source, size_t begin, size_t end) { if (end - begin <= sizeof(subset.data.inlined.bytes)) { subset.refcount = NULL; - subset.data.inlined.length = end - begin; + subset.data.inlined.length = (gpr_uint8)(end - begin); memcpy(subset.data.inlined.bytes, GPR_SLICE_START_PTR(source) + begin, end - begin); } else { @@ -245,17 +247,17 @@ gpr_slice gpr_slice_split_tail(gpr_slice *source, size_t split) { /* inlined data, copy it out */ GPR_ASSERT(source->data.inlined.length >= split); tail.refcount = NULL; - tail.data.inlined.length = source->data.inlined.length - split; + tail.data.inlined.length = (gpr_uint8)(source->data.inlined.length - split); memcpy(tail.data.inlined.bytes, source->data.inlined.bytes + split, tail.data.inlined.length); - source->data.inlined.length = split; + source->data.inlined.length = (gpr_uint8)split; } else { size_t tail_length = source->data.refcounted.length - split; GPR_ASSERT(source->data.refcounted.length >= split); if (tail_length < sizeof(tail.data.inlined.bytes)) { /* Copy out the bytes - it'll be cheaper than refcounting */ tail.refcount = NULL; - tail.data.inlined.length = tail_length; + tail.data.inlined.length = (gpr_uint8)tail_length; memcpy(tail.data.inlined.bytes, source->data.refcounted.bytes + split, tail_length); } else { @@ -280,16 +282,16 @@ gpr_slice gpr_slice_split_head(gpr_slice *source, size_t split) { GPR_ASSERT(source->data.inlined.length >= split); head.refcount = NULL; - head.data.inlined.length = split; + head.data.inlined.length = (gpr_uint8)split; memcpy(head.data.inlined.bytes, source->data.inlined.bytes, split); - source->data.inlined.length -= split; + source->data.inlined.length = (gpr_uint8)(source->data.inlined.length - split); memmove(source->data.inlined.bytes, source->data.inlined.bytes + split, source->data.inlined.length); } else if (split < sizeof(head.data.inlined.bytes)) { GPR_ASSERT(source->data.refcounted.length >= split); head.refcount = NULL; - head.data.inlined.length = split; + head.data.inlined.length = (gpr_uint8)split; memcpy(head.data.inlined.bytes, source->data.refcounted.bytes, split); source->data.refcounted.bytes += split; source->data.refcounted.length -= split; @@ -311,7 +313,7 @@ gpr_slice gpr_slice_split_head(gpr_slice *source, size_t split) { } int gpr_slice_cmp(gpr_slice a, gpr_slice b) { - int d = GPR_SLICE_LENGTH(a) - GPR_SLICE_LENGTH(b); + int d = (int)(GPR_SLICE_LENGTH(a) - GPR_SLICE_LENGTH(b)); if (d != 0) return d; return memcmp(GPR_SLICE_START_PTR(a), GPR_SLICE_START_PTR(b), GPR_SLICE_LENGTH(a)); @@ -319,7 +321,7 @@ int gpr_slice_cmp(gpr_slice a, gpr_slice b) { int gpr_slice_str_cmp(gpr_slice a, const char *b) { size_t b_length = strlen(b); - int d = GPR_SLICE_LENGTH(a) - b_length; + int d = (int)(GPR_SLICE_LENGTH(a) - b_length); if (d != 0) return d; return memcmp(GPR_SLICE_START_PTR(a), b, b_length); } diff --git a/src/core/support/slice_buffer.c b/src/core/support/slice_buffer.c index 91b5d8c98b..7e25d99774 100644 --- a/src/core/support/slice_buffer.c +++ b/src/core/support/slice_buffer.c @@ -81,7 +81,7 @@ gpr_uint8 *gpr_slice_buffer_tiny_add(gpr_slice_buffer *sb, unsigned n) { if ((back->data.inlined.length + n) > sizeof(back->data.inlined.bytes)) goto add_new; out = back->data.inlined.bytes + back->data.inlined.length; - back->data.inlined.length += n; + back->data.inlined.length = (gpr_uint8)(back->data.inlined.length + n); return out; add_new: @@ -89,7 +89,7 @@ add_new: back = &sb->slices[sb->count]; sb->count++; back->refcount = NULL; - back->data.inlined.length = n; + back->data.inlined.length = (gpr_uint8)n; return back->data.inlined.bytes; } @@ -116,7 +116,7 @@ void gpr_slice_buffer_add(gpr_slice_buffer *sb, gpr_slice s) { GPR_SLICE_INLINED_SIZE) { memcpy(back->data.inlined.bytes + back->data.inlined.length, s.data.inlined.bytes, s.data.inlined.length); - back->data.inlined.length += s.data.inlined.length; + back->data.inlined.length = (gpr_uint8)(back->data.inlined.length + s.data.inlined.length); } else { size_t cp1 = GPR_SLICE_INLINED_SIZE - back->data.inlined.length; memcpy(back->data.inlined.bytes + back->data.inlined.length, @@ -126,7 +126,7 @@ void gpr_slice_buffer_add(gpr_slice_buffer *sb, gpr_slice s) { back = &sb->slices[n]; sb->count = n + 1; back->refcount = NULL; - back->data.inlined.length = s.data.inlined.length - cp1; + back->data.inlined.length = (gpr_uint8)(s.data.inlined.length - cp1); memcpy(back->data.inlined.bytes, s.data.inlined.bytes + cp1, s.data.inlined.length - cp1); } diff --git a/src/core/support/string.c b/src/core/support/string.c index bfd7ce1590..6a80ccc841 100644 --- a/src/core/support/string.c +++ b/src/core/support/string.c @@ -94,7 +94,7 @@ char *gpr_hexdump(const char *buf, size_t len, gpr_uint32 flags) { if (len) hexout_append(&out, ' '); hexout_append(&out, '\''); for (cur = beg; cur != end; ++cur) { - hexout_append(&out, isprint(*cur) ? *cur : '.'); + hexout_append(&out, isprint(*cur) ? *(char*)cur : '.'); } hexout_append(&out, '\''); } @@ -113,7 +113,7 @@ int gpr_parse_bytes_to_uint32(const char *buf, size_t len, gpr_uint32 *result) { for (i = 0; i < len; i++) { if (buf[i] < '0' || buf[i] > '9') return 0; /* bad char */ - new = 10 * out + (buf[i] - '0'); + new = 10 * out + (gpr_uint32)(buf[i] - '0'); if (new < out) return 0; /* overflow */ out = new; } @@ -143,7 +143,7 @@ int gpr_ltoa(long value, char *string) { if (neg) value = -value; while (value) { - string[i++] = '0' + value % 10; + string[i++] = (char)('0' + value % 10); value /= 10; } if (neg) string[i++] = '-'; diff --git a/src/core/support/subprocess_posix.c b/src/core/support/subprocess_posix.c index b4631fa0ed..171054e4da 100644 --- a/src/core/support/subprocess_posix.c +++ b/src/core/support/subprocess_posix.c @@ -66,8 +66,8 @@ gpr_subprocess *gpr_subprocess_create(int argc, const char **argv) { if (pid == -1) { return NULL; } else if (pid == 0) { - exec_args = gpr_malloc((argc + 1) * sizeof(char *)); - memcpy(exec_args, argv, argc * sizeof(char *)); + exec_args = gpr_malloc(((size_t)argc + 1) * sizeof(char *)); + memcpy(exec_args, argv, (size_t)argc * sizeof(char *)); exec_args[argc] = NULL; execv(exec_args[0], exec_args); /* if we reach here, an error has occurred */ diff --git a/src/core/support/time.c b/src/core/support/time.c index 7dbf95059f..d47b08b266 100644 --- a/src/core/support/time.c +++ b/src/core/support/time.c @@ -86,11 +86,11 @@ gpr_timespec gpr_time_from_nanos(long ns) { result = gpr_inf_past; } else if (ns >= 0) { result.tv_sec = ns / GPR_NS_PER_SEC; - result.tv_nsec = ns - result.tv_sec * GPR_NS_PER_SEC; + result.tv_nsec = (int)(ns - result.tv_sec * GPR_NS_PER_SEC); } else { /* Calculation carefully formulated to avoid any possible under/overflow. */ result.tv_sec = (-(999999999 - (ns + GPR_NS_PER_SEC)) / GPR_NS_PER_SEC) - 1; - result.tv_nsec = ns - result.tv_sec * GPR_NS_PER_SEC; + result.tv_nsec = (int)(ns - result.tv_sec * GPR_NS_PER_SEC); } return result; } @@ -103,11 +103,11 @@ gpr_timespec gpr_time_from_micros(long us) { result = gpr_inf_past; } else if (us >= 0) { result.tv_sec = us / 1000000; - result.tv_nsec = (us - result.tv_sec * 1000000) * 1000; + result.tv_nsec = (int)((us - result.tv_sec * 1000000) * 1000); } else { /* Calculation carefully formulated to avoid any possible under/overflow. */ result.tv_sec = (-(999999 - (us + 1000000)) / 1000000) - 1; - result.tv_nsec = (us - result.tv_sec * 1000000) * 1000; + result.tv_nsec = (int)((us - result.tv_sec * 1000000) * 1000); } return result; } @@ -120,11 +120,11 @@ gpr_timespec gpr_time_from_millis(long ms) { result = gpr_inf_past; } else if (ms >= 0) { result.tv_sec = ms / 1000; - result.tv_nsec = (ms - result.tv_sec * 1000) * 1000000; + result.tv_nsec = (int)((ms - result.tv_sec * 1000) * 1000000); } else { /* Calculation carefully formulated to avoid any possible under/overflow. */ result.tv_sec = (-(999 - (ms + 1000)) / 1000) - 1; - result.tv_nsec = (ms - result.tv_sec * 1000) * 1000000; + result.tv_nsec = (int)((ms - result.tv_sec * 1000) * 1000000); } return result; } @@ -245,10 +245,10 @@ gpr_int32 gpr_time_to_millis(gpr_timespec t) { care?) */ return -2147483647; } else { - return t.tv_sec * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS; + return (gpr_int32)(t.tv_sec * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS); } } double gpr_timespec_to_micros(gpr_timespec t) { - return t.tv_sec * GPR_US_PER_SEC + t.tv_nsec * 1e-3; + return (double)t.tv_sec * GPR_US_PER_SEC + t.tv_nsec * 1e-3; } diff --git a/src/core/support/time_posix.c b/src/core/support/time_posix.c index 3675f1eb22..afb58ef231 100644 --- a/src/core/support/time_posix.c +++ b/src/core/support/time_posix.c @@ -51,7 +51,7 @@ static struct timespec timespec_from_gpr(gpr_timespec gts) { static gpr_timespec gpr_from_timespec(struct timespec ts) { gpr_timespec rv; rv.tv_sec = ts.tv_sec; - rv.tv_nsec = ts.tv_nsec; + rv.tv_nsec = (int)ts.tv_nsec; return rv; } |