From a406ec983d1d57611fa231dc587aa060baffae5e Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 12 Jan 2022 23:54:25 +0100 Subject: [PATCH] Fix warnings of clang -Wshorten-64-to-32 --- apps/json_parse.c | 4 ++-- json_object.c | 16 ++++++++-------- json_pointer.c | 10 +++++----- json_tokener.c | 20 ++++++++++---------- json_util.c | 4 ++-- tests/test_parse.c | 2 +- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/apps/json_parse.c b/apps/json_parse.c index 72b31a8..6b46427 100644 --- a/apps/json_parse.c +++ b/apps/json_parse.c @@ -71,7 +71,7 @@ static int parseit(int fd, int (*callback)(struct json_object *)) // everything you can do with a tokener into json_util.c seems // like the wrong approach. size_t total_read = 0; - while ((ret = read(fd, buf, sizeof(buf))) > 0) + while ((ret = (int)read(fd, buf, sizeof(buf))) > 0) { total_read += ret; int start_pos = 0; @@ -85,7 +85,7 @@ static int parseit(int fd, int (*callback)(struct json_object *)) char *aterr = (start_pos + parse_end < sizeof(buf)) ? &buf[start_pos + parse_end] : ""; fflush(stdout); - int fail_offset = total_read - ret + start_pos + parse_end; + int fail_offset = (int)(total_read - ret + start_pos + parse_end); fprintf(stderr, "Failed at offset %d: %s %c\n", fail_offset, json_tokener_error_desc(jerr), aterr[0]); json_tokener_free(tok); diff --git a/json_object.c b/json_object.c index d59a317..f54c275 100644 --- a/json_object.c +++ b/json_object.c @@ -200,7 +200,7 @@ static int json_escape_str(struct printbuf *pb, const char *str, size_t len, int } if (pos > start_offset) - printbuf_memappend(pb, str + start_offset, pos - start_offset); + printbuf_memappend(pb, str + start_offset, (int)(pos - start_offset)); if (c == '\b') printbuf_memappend(pb, "\\b", 2); @@ -227,7 +227,7 @@ static int json_escape_str(struct printbuf *pb, const char *str, size_t len, int char sbuf[7]; if (pos > start_offset) printbuf_memappend(pb, str + start_offset, - pos - start_offset); + (int)(pos - start_offset)); snprintf(sbuf, sizeof(sbuf), "\\u00%c%c", json_hex_chars[c >> 4], json_hex_chars[c & 0xf]); printbuf_memappend_fast(pb, sbuf, (int)sizeof(sbuf) - 1); @@ -238,7 +238,7 @@ static int json_escape_str(struct printbuf *pb, const char *str, size_t len, int } } if (pos > start_offset) - printbuf_memappend(pb, str + start_offset, pos - start_offset); + printbuf_memappend(pb, str + start_offset, (int)(pos - start_offset)); return 0; } @@ -681,7 +681,7 @@ static int json_object_int_to_json_string(struct json_object *jso, struct printb snprintf(sbuf, sizeof(sbuf), "%" PRId64, JC_INT(jso)->cint.c_int64); else snprintf(sbuf, sizeof(sbuf), "%" PRIu64, JC_INT(jso)->cint.c_uint64); - return printbuf_memappend(pb, sbuf, strlen(sbuf)); + return printbuf_memappend(pb, sbuf, (int)strlen(sbuf)); } struct json_object *json_object_new_int(int32_t i) @@ -1035,7 +1035,7 @@ static int json_object_double_to_json_string_format(struct json_object *jso, str /* drop trailing zeroes */ if (*p != 0) *(++p) = 0; - size = p - buf; + size = (int)(p - buf); } } // although unlikely, snprintf can fail @@ -1106,7 +1106,7 @@ static int _json_object_userdata_to_json_string(struct json_object *jso, struct int json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, int level, int flags) { - int userdata_len = strlen((const char *)jso->_userdata); + int userdata_len = (int)strlen((const char *)jso->_userdata); printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len); return userdata_len; } @@ -1274,7 +1274,7 @@ int json_object_get_string_len(const struct json_object *jso) return 0; switch (jso->o_type) { - case json_type_string: return _json_object_get_string_len(JC_STRING_C(jso)); + case json_type_string: return (int)_json_object_get_string_len(JC_STRING_C(jso)); default: return 0; } } @@ -1634,7 +1634,7 @@ int json_c_shallow_copy_default(json_object *src, json_object *parent, const cha case json_type_string: *dst = json_object_new_string_len(get_string_component(src), - _json_object_get_string_len(JC_STRING(src))); + (int)_json_object_get_string_len(JC_STRING(src))); break; case json_type_object: *dst = json_object_new_object(); break; diff --git a/json_pointer.c b/json_pointer.c index 395567a..221cb95 100644 --- a/json_pointer.c +++ b/json_pointer.c @@ -29,8 +29,8 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char) { - int slen = strlen(s); - int skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */ + size_t slen = strlen(s); + int skip = (int)strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */ char *p = s; while ((p = strstr(p, occur))) { @@ -43,7 +43,7 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur, static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx) { - int i, len = strlen(path); + int i, len = (int)strlen(path); /* this code-path optimizes a bit, for when we reference the 0-9 index range * in a JSON array and because leading zeros not allowed */ @@ -73,14 +73,14 @@ static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx } } - *idx = strtol(path, NULL, 10); + *idx = (int)strtol(path, NULL, 10); if (*idx < 0) { errno = EINVAL; return 0; } check_oob: - len = json_object_array_length(jo); + len = (int)json_object_array_length(jo); if (*idx >= len) { errno = ENOENT; diff --git a/json_tokener.c b/json_tokener.c index 4a25645..da647ab 100644 --- a/json_tokener.c +++ b/json_tokener.c @@ -560,11 +560,11 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char * if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { printbuf_memappend_fast(tok->pb, case_start, - str - case_start); + (int)(str - case_start)); goto out; } } - printbuf_memappend_fast(tok->pb, case_start, 1 + str - case_start); + printbuf_memappend_fast(tok->pb, case_start, 1 + (int)(str - case_start)); state = json_tokener_state_comment_end; } break; @@ -578,11 +578,11 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char * if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { printbuf_memappend_fast(tok->pb, case_start, - str - case_start); + (int)(str - case_start)); goto out; } } - printbuf_memappend_fast(tok->pb, case_start, str - case_start); + printbuf_memappend_fast(tok->pb, case_start, (int)(str - case_start)); MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf); state = json_tokener_state_eatws; } @@ -610,7 +610,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char * if (c == tok->quote_char) { printbuf_memappend_fast(tok->pb, case_start, - str - case_start); + (int)(str - case_start)); current = json_object_new_string_len(tok->pb->buf, tok->pb->bpos); if (current == NULL) @@ -622,7 +622,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char * else if (c == '\\') { printbuf_memappend_fast(tok->pb, case_start, - str - case_start); + (int)(str - case_start)); saved_state = json_tokener_state_string; state = json_tokener_state_string_escape; break; @@ -630,7 +630,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char * if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { printbuf_memappend_fast(tok->pb, case_start, - str - case_start); + (int)(str - case_start)); goto out; } } @@ -1130,7 +1130,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char * if (c == tok->quote_char) { printbuf_memappend_fast(tok->pb, case_start, - str - case_start); + (int)(str - case_start)); obj_field_name = strdup(tok->pb->buf); saved_state = json_tokener_state_object_field_end; state = json_tokener_state_eatws; @@ -1139,7 +1139,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char * else if (c == '\\') { printbuf_memappend_fast(tok->pb, case_start, - str - case_start); + (int)(str - case_start)); saved_state = json_tokener_state_object_field; state = json_tokener_state_string_escape; break; @@ -1147,7 +1147,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char * if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { printbuf_memappend_fast(tok->pb, case_start, - str - case_start); + (int)(str - case_start)); goto out; } } diff --git a/json_util.c b/json_util.c index 4312458..6992c76 100644 --- a/json_util.c +++ b/json_util.c @@ -107,7 +107,7 @@ struct json_object *json_object_from_fd_ex(int fd, int in_depth) return NULL; } - while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) + while ((ret = (int)read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) { printbuf_memappend(pb, buf, ret); } @@ -200,7 +200,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const wpos = 0; while (wpos < wsize) { - if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0) + if ((ret = (int)write(fd, json_str + wpos, wsize - wpos)) < 0) { _json_c_set_last_err("json_object_to_file: error writing file %s: %s\n", filename, strerror(errno)); diff --git a/tests/test_parse.c b/tests/test_parse.c index 5363f32..a298391 100644 --- a/tests/test_parse.c +++ b/tests/test_parse.c @@ -49,7 +49,7 @@ static void single_incremental_parse(const char *test_string, int clear_serializ all_at_once_str = json_object_to_json_string(all_at_once_obj); tok = json_tokener_new(); - int test_string_len = strlen(test_string) + 1; // Including '\0' ! + int test_string_len = (int)strlen(test_string) + 1; // Including '\0' ! for (ii = 0; ii < test_string_len; ii += chunksize) { int len_to_parse = chunksize;