diff options
author | David Robillard <d@drobilla.net> | 2019-10-27 17:26:43 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-10-27 22:40:28 +0100 |
commit | f0c5278f57b1ba231568f8b8fd6ff355b9622a09 (patch) | |
tree | 33cf03f882ed76172144051586f42ff757f02c4a | |
parent | bbe5313c31386366175222cde3055b63848ebb4c (diff) | |
download | serd-f0c5278f57b1ba231568f8b8fd6ff355b9622a09.tar.gz serd-f0c5278f57b1ba231568f8b8fd6ff355b9622a09.tar.bz2 serd-f0c5278f57b1ba231568f8b8fd6ff355b9622a09.zip |
Fix integer conversion warnings
-rw-r--r-- | src/env.c | 4 | ||||
-rw-r--r-- | src/n3.c | 17 | ||||
-rw-r--r-- | src/node.c | 16 | ||||
-rw-r--r-- | src/reader.c | 6 | ||||
-rw-r--r-- | src/serd_internal.h | 12 | ||||
-rw-r--r-- | src/string.c | 8 | ||||
-rw-r--r-- | src/uri.c | 10 |
7 files changed, 40 insertions, 33 deletions
@@ -188,13 +188,13 @@ serd_env_expand(const SerdEnv* env, return SERD_ERR_BAD_ARG; } - const size_t name_len = colon - curie->buf; + const size_t name_len = (size_t)(colon - curie->buf); const SerdPrefix* const prefix = serd_env_find(env, curie->buf, name_len); if (prefix) { uri_prefix->buf = prefix->uri.buf; uri_prefix->len = prefix->uri.n_bytes; uri_suffix->buf = colon + 1; - uri_suffix->len = curie->n_bytes - (colon - curie->buf) - 1; + uri_suffix->len = curie->n_bytes - name_len - 1; return SERD_SUCCESS; } return SERD_ERR_BAD_CURIE; @@ -48,8 +48,9 @@ read_HEX(SerdReader* reader) if (is_xdigit(c)) { return eat_byte_safe(reader, c); } - return r_err(reader, SERD_ERR_BAD_SYNTAX, - "invalid hexadecimal digit `%c'\n", c); + + return (uint8_t)r_err(reader, SERD_ERR_BAD_SYNTAX, + "invalid hexadecimal digit `%c'\n", c); } // Read UCHAR escape, initial \ is already eaten by caller @@ -78,7 +79,7 @@ read_UCHAR(SerdReader* reader, Ref dest, uint32_t* char_code) } char* endptr = NULL; - const uint32_t code = strtoul((const char*)buf, &endptr, 16); + const uint32_t code = (uint32_t)strtoul((const char*)buf, &endptr, 16); assert(endptr == (char*)buf + length); unsigned size = 0; @@ -103,17 +104,17 @@ read_UCHAR(SerdReader* reader, Ref dest, uint32_t* char_code) uint32_t c = code; switch (size) { case 4: - buf[3] = 0x80 | (uint8_t)(c & 0x3F); + buf[3] = (uint8_t)(0x80u | (c & 0x3Fu)); c >>= 6; c |= (16 << 12); // set bit 4 // fallthru case 3: - buf[2] = 0x80 | (uint8_t)(c & 0x3F); + buf[2] = (uint8_t)(0x80u | (c & 0x3Fu)); c >>= 6; c |= (32 << 6); // set bit 5 // fallthru case 2: - buf[1] = 0x80 | (uint8_t)(c & 0x3F); + buf[1] = (uint8_t)(0x80u | (c & 0x3Fu)); c >>= 6; c |= 0xC0; // set bits 6 and 7 // fallthru @@ -926,7 +927,7 @@ read_anon(SerdReader* reader, ReadContext ctx, bool subject, Ref* dest) ctx.subject = *dest; if (!empty) { - *ctx.flags &= ~(SERD_LIST_CONT); + *ctx.flags &= ~(unsigned)SERD_LIST_CONT; if (!subject) { *ctx.flags |= SERD_ANON_CONT; } @@ -1097,7 +1098,7 @@ end_collection(SerdReader* reader, ReadContext ctx, Ref n1, Ref n2, bool ret) { pop_node(reader, n2); pop_node(reader, n1); - *ctx.flags &= ~SERD_LIST_CONT; + *ctx.flags &= ~(unsigned)SERD_LIST_CONT; return ret && (eat_byte_safe(reader, ')') == ')'); } @@ -279,7 +279,7 @@ serd_node_new_decimal(double d, unsigned frac_digits) char* t = s - 1; uint64_t dec = (uint64_t)int_part; do { - *t-- = '0' + (dec % 10); + *t-- = (char)('0' + dec % 10); } while ((dec /= 10) > 0); *s++ = '.'; @@ -288,20 +288,20 @@ serd_node_new_decimal(double d, unsigned frac_digits) double frac_part = fabs(d - int_part); if (frac_part < DBL_EPSILON) { *s++ = '0'; - node.n_bytes = node.n_chars = (s - buf); + node.n_bytes = node.n_chars = (size_t)(s - buf); } else { - uint64_t frac = llround(frac_part * pow(10.0, (int)frac_digits)); + uint64_t frac = (uint64_t)llround(frac_part * pow(10.0, (int)frac_digits)); s += frac_digits - 1; unsigned i = 0; // Skip trailing zeros for (; i < frac_digits - 1 && !(frac % 10); ++i, --s, frac /= 10) {} - node.n_bytes = node.n_chars = (s - buf) + 1; + node.n_bytes = node.n_chars = (size_t)(s - buf) + 1u; // Write digits from last trailing zero to decimal point for (; i < frac_digits; ++i) { - *s-- = '0' + (frac % 10); + *s-- = (char)('0' + (frac % 10)); frac /= 10; } } @@ -313,7 +313,7 @@ SerdNode serd_node_new_integer(int64_t i) { int64_t abs_i = (i < 0) ? -i : i; - const unsigned digits = serd_digits(abs_i); + const unsigned digits = serd_digits((double)abs_i); char* buf = (char*)calloc(digits + 2, 1); SerdNode node = { (const uint8_t*)buf, 0, 0, 0, SERD_LITERAL }; @@ -324,11 +324,11 @@ serd_node_new_integer(int64_t i) ++s; } - node.n_bytes = node.n_chars = (s - buf) + 1; + node.n_bytes = node.n_chars = (size_t)(s - buf) + 1u; // Write integer part (right to left) do { - *s-- = '0' + (abs_i % 10); + *s-- = (char)('0' + (abs_i % 10)); } while ((abs_i /= 10) > 0); return node; diff --git a/src/reader.c b/src/reader.c index 1a7f58d4..d63c2734 100644 --- a/src/reader.c +++ b/src/reader.c @@ -41,7 +41,7 @@ set_blank_id(SerdReader* reader, Ref ref, size_t buf_size) { SerdNode* node = deref(reader, ref); const char* prefix = reader->bprefix ? (const char*)reader->bprefix : ""; - node->n_bytes = node->n_chars = snprintf( + node->n_bytes = node->n_chars = (size_t)snprintf( (char*)node->buf, buf_size, "%sb%u", prefix, reader->next_id++); } @@ -96,7 +96,7 @@ push_node_padded(SerdReader* reader, size_t maxlen, reader->allocs, sizeof(reader->allocs) * (++reader->n_allocs)); reader->allocs[reader->n_allocs - 1] = ((uint8_t*)mem - reader->stack.buf); #endif - return (uint8_t*)node - reader->stack.buf; + return (Ref)((uint8_t*)node - reader->stack.buf); } Ref @@ -127,7 +127,7 @@ pop_node(SerdReader* reader, Ref ref) #endif SerdNode* const node = deref(reader, ref); uint8_t* const top = reader->stack.buf + reader->stack.size; - serd_stack_pop_aligned(&reader->stack, top - (uint8_t*)node); + serd_stack_pop_aligned(&reader->stack, (size_t)(top - (uint8_t*)node)); } return 0; } diff --git a/src/serd_internal.h b/src/serd_internal.h index 73c8113b..d1c0a3a5 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -228,13 +228,14 @@ serd_stack_push_aligned(SerdStack* stack, size_t n_bytes, size_t align) serd_stack_push(stack, 1); // Push padding if necessary - const uint8_t pad = align - stack->size % align; + const size_t pad = align - stack->size % align; if (pad > 0) { serd_stack_push(stack, pad); } // Set top of stack to pad count so we can properly pop later - stack->buf[stack->size - 1] = pad; + assert(pad < UINT8_MAX); + stack->buf[stack->size - 1] = (uint8_t)pad; // Push requested space at aligned location return serd_stack_push(stack, n_bytes); @@ -250,7 +251,7 @@ serd_stack_pop_aligned(SerdStack* stack, size_t n_bytes) const uint8_t pad = stack->buf[stack->size - 1]; // Pop padding and pad count - serd_stack_pop(stack, pad + 1); + serd_stack_pop(stack, pad + 1u); } /* Byte Sink */ @@ -422,7 +423,7 @@ utf8_num_bytes(const uint8_t c) static inline uint32_t parse_counted_utf8_char(const uint8_t* utf8, size_t size) { - uint32_t c = utf8[0] & ((1 << (8 - size)) - 1); + uint32_t c = utf8[0] & ((1u << (8 - size)) - 1); for (size_t i = 1; i < size; ++i) { const uint8_t in = utf8[i] & 0x3F; c = (c << 6) | in; @@ -438,7 +439,8 @@ parse_utf8_char(const uint8_t* utf8, size_t* size) case 1: case 2: case 3: case 4: return parse_counted_utf8_char(utf8, *size); default: - return *size = 0; + *size = 0; + return 0; } } diff --git a/src/string.c b/src/string.c index cde82134..0869bc23 100644 --- a/src/string.c +++ b/src/string.c @@ -170,7 +170,11 @@ static const char b64_unmap[] = "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"; -static inline uint8_t unmap(const uint8_t in) { return b64_unmap[in] - 47; } +static inline uint8_t +unmap(const uint8_t in) +{ + return (uint8_t)(b64_unmap[in] - 47); +} /** Decode 4 base64 characters to 3 raw bytes. @@ -181,7 +185,7 @@ decode_chunk(const uint8_t in[4], uint8_t out[3]) out[0] = (uint8_t)(((unmap(in[0]) << 2)) | unmap(in[1]) >> 4); out[1] = (uint8_t)(((unmap(in[1]) << 4) & 0xF0) | unmap(in[2]) >> 2); out[2] = (uint8_t)(((unmap(in[2]) << 6) & 0xC0) | unmap(in[3])); - return 1 + (in[2] != '=') + ((in[2] != '=') && (in[3] != '=')); + return 1u + (in[2] != '=') + ((in[2] != '=') && (in[3] != '=')); } void* @@ -58,8 +58,8 @@ serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname) return NULL; } if (hostname) { - *hostname = (uint8_t*)calloc(path - auth + 1, 1); - memcpy(*hostname, auth, path - auth); + *hostname = (uint8_t*)calloc((size_t)(path - auth + 1), 1); + memcpy(*hostname, auth, (size_t)(path - auth)); } } } @@ -128,7 +128,7 @@ serd_uri_parse(const uint8_t* utf8, SerdURI* out) goto path; // Relative URI (starts with path by definition) case ':': out->scheme.buf = utf8; - out->scheme.len = (ptr++) - utf8; + out->scheme.len = (size_t)((ptr++) - utf8); goto maybe_authority; // URI with scheme case '+': case '-': case '.': continue; @@ -297,12 +297,12 @@ merge(SerdChunk* base, SerdChunk* path) } while (up > 0 && (--base_last > base->buf)); // Set path prefix - base->len = base_last - base->buf + 1; + base->len = (size_t)(base_last - base->buf + 1); } // Set path suffix path->buf = begin; - path->len = end - begin; + path->len = (size_t)(end - begin); } /// See http://tools.ietf.org/html/rfc3986#section-5.2.2 |