Fix -Wsign-compare warnings.

I decided to modify code in a defensive way. Ideally functions
like size() or length() should return an unsigned type.
This commit is contained in:
marxin 2019-02-21 20:57:39 +01:00
parent ce114e35dd
commit e5f665c718
21 changed files with 40 additions and 41 deletions

View file

@ -339,7 +339,6 @@ if selected_platform in platform_list:
if (env["werror"]): if (env["werror"]):
env.Append(CCFLAGS=['/WX']) env.Append(CCFLAGS=['/WX'])
else: # Rest of the world else: # Rest of the world
disable_nonessential_warnings = ['-Wno-sign-compare']
shadow_local_warning = [] shadow_local_warning = []
all_plus_warnings = ['-Wwrite-strings'] all_plus_warnings = ['-Wwrite-strings']
@ -350,9 +349,9 @@ if selected_platform in platform_list:
if (env["warnings"] == 'extra'): if (env["warnings"] == 'extra'):
env.Append(CCFLAGS=['-Wall', '-Wextra'] + all_plus_warnings + shadow_local_warning) env.Append(CCFLAGS=['-Wall', '-Wextra'] + all_plus_warnings + shadow_local_warning)
elif (env["warnings"] == 'all'): elif (env["warnings"] == 'all'):
env.Append(CCFLAGS=['-Wall'] + all_plus_warnings + shadow_local_warning + disable_nonessential_warnings) env.Append(CCFLAGS=['-Wall'] + shadow_local_warning)
elif (env["warnings"] == 'moderate'): elif (env["warnings"] == 'moderate'):
env.Append(CCFLAGS=['-Wall', '-Wno-unused'] + shadow_local_warning + disable_nonessential_warnings) env.Append(CCFLAGS=['-Wall', '-Wno-unused'] + shadow_local_warning)
else: # 'no' else: # 'no'
env.Append(CCFLAGS=['-w']) env.Append(CCFLAGS=['-w'])
if (env["werror"]): if (env["werror"]):

View file

@ -224,7 +224,7 @@ Error PacketPeerStream::get_packet(const uint8_t **r_buffer, int &r_buffer_size)
uint32_t len = decode_uint32(lbuf); uint32_t len = decode_uint32(lbuf);
ERR_FAIL_COND_V(remaining < (int)len, ERR_UNAVAILABLE); ERR_FAIL_COND_V(remaining < (int)len, ERR_UNAVAILABLE);
ERR_FAIL_COND_V(input_buffer.size() < len, ERR_UNAVAILABLE); ERR_FAIL_COND_V(input_buffer.size() < (int)len, ERR_UNAVAILABLE);
ring_buffer.read(lbuf, 4); //get rid of first 4 bytes ring_buffer.read(lbuf, 4); //get rid of first 4 bytes
ring_buffer.read(input_buffer.ptrw(), len); // read packet ring_buffer.read(input_buffer.ptrw(), len); // read packet

View file

@ -106,7 +106,7 @@ StringName ResourceInteractiveLoaderBinary::_get_string() {
uint32_t id = f->get_32(); uint32_t id = f->get_32();
if (id & 0x80000000) { if (id & 0x80000000) {
uint32_t len = id & 0x7FFFFFFF; uint32_t len = id & 0x7FFFFFFF;
if (len > str_buf.size()) { if ((int)len > str_buf.size()) {
str_buf.resize(len); str_buf.resize(len);
} }
if (len == 0) if (len == 0)

View file

@ -509,7 +509,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur
texture = texture->get_ptr(); texture = texture->get_ptr();
if (next_power_of_2(texture->alloc_width) != texture->alloc_width && next_power_of_2(texture->alloc_height) != texture->alloc_height) { if (next_power_of_2(texture->alloc_width) != (unsigned int)texture->alloc_width && next_power_of_2(texture->alloc_height) != (unsigned int)texture->alloc_height) {
state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, true); state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, true);
can_tile = false; can_tile = false;
} }

View file

@ -158,7 +158,7 @@ void RasterizerSceneGLES2::shadow_atlas_set_quadrant_subdivision(RID p_atlas, in
subdiv = int(Math::sqrt((float)subdiv)); subdiv = int(Math::sqrt((float)subdiv));
if (shadow_atlas->quadrants[p_quadrant].shadows.size() == subdiv) if (shadow_atlas->quadrants[p_quadrant].shadows.size() == (int)subdiv)
return; return;
// erase all data from the quadrant // erase all data from the quadrant

View file

@ -246,7 +246,7 @@ void FileAccessUnix::store_8(uint8_t p_dest) {
void FileAccessUnix::store_buffer(const uint8_t *p_src, int p_length) { void FileAccessUnix::store_buffer(const uint8_t *p_src, int p_length) {
ERR_FAIL_COND(!f); ERR_FAIL_COND(!f);
ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length); ERR_FAIL_COND((int)fwrite(p_src, 1, p_length, f) != p_length);
} }
bool FileAccessUnix::file_exists(const String &p_path) { bool FileAccessUnix::file_exists(const String &p_path) {

View file

@ -76,7 +76,7 @@ int64_t GDAPI godot_videodecoder_file_seek(void *ptr, int64_t pos, int whence) {
} break; } break;
case SEEK_CUR: { case SEEK_CUR: {
// Just in case it doesn't exist // Just in case it doesn't exist
if (pos < 0 && -pos > file->get_position()) { if (pos < 0 && (size_t)-pos > file->get_position()) {
return -1; return -1;
} }
pos = pos + static_cast<int>(file->get_position()); pos = pos + static_cast<int>(file->get_position());
@ -86,7 +86,7 @@ int64_t GDAPI godot_videodecoder_file_seek(void *ptr, int64_t pos, int whence) {
} break; } break;
case SEEK_END: { case SEEK_END: {
// Just in case something goes wrong // Just in case something goes wrong
if (-pos > len) { if ((size_t)-pos > len) {
return -1; return -1;
} }
file->seek_end(pos); file->seek_end(pos);

View file

@ -1417,7 +1417,7 @@ StringName GDScriptTokenizerBuffer::get_token_identifier(int p_offset) const {
ERR_FAIL_INDEX_V(offset, tokens.size(), StringName()); ERR_FAIL_INDEX_V(offset, tokens.size(), StringName());
uint32_t identifier = tokens[offset] >> TOKEN_BITS; uint32_t identifier = tokens[offset] >> TOKEN_BITS;
ERR_FAIL_UNSIGNED_INDEX_V(identifier, identifiers.size(), StringName()); ERR_FAIL_UNSIGNED_INDEX_V(identifier, (uint32_t)identifiers.size(), StringName());
return identifiers[identifier]; return identifiers[identifier];
} }
@ -1473,7 +1473,7 @@ const Variant &GDScriptTokenizerBuffer::get_token_constant(int p_offset) const {
int offset = token + p_offset; int offset = token + p_offset;
ERR_FAIL_INDEX_V(offset, tokens.size(), nil); ERR_FAIL_INDEX_V(offset, tokens.size(), nil);
uint32_t constant = tokens[offset] >> TOKEN_BITS; uint32_t constant = tokens[offset] >> TOKEN_BITS;
ERR_FAIL_UNSIGNED_INDEX_V(constant, constants.size(), nil); ERR_FAIL_UNSIGNED_INDEX_V(constant, (uint32_t)constants.size(), nil);
return constants[constant]; return constants[constant];
} }
String GDScriptTokenizerBuffer::get_token_error(int p_offset) const { String GDScriptTokenizerBuffer::get_token_error(int p_offset) const {

View file

@ -449,7 +449,7 @@ static String variant_type_to_managed_name(const String &p_var_type_name) {
Variant::_RID Variant::_RID
}; };
for (int i = 0; i < sizeof(var_types) / sizeof(Variant::Type); i++) { for (unsigned int i = 0; i < sizeof(var_types) / sizeof(Variant::Type); i++) {
if (p_var_type_name == Variant::get_type_name(var_types[i])) if (p_var_type_name == Variant::get_type_name(var_types[i]))
return p_var_type_name; return p_var_type_name;
} }
@ -2172,7 +2172,7 @@ bool CSharpScript::_get_member_export(GDMonoClass *p_class, IMonoClassMember *p_
return false; return false;
} }
if (val != i) { if (val != (unsigned int)i) {
uses_default_values = false; uses_default_values = false;
} }

View file

@ -86,7 +86,7 @@ bool godot_icall_Array_Contains(Array *ptr, MonoObject *item) {
} }
void godot_icall_Array_CopyTo(Array *ptr, MonoArray *array, int array_index) { void godot_icall_Array_CopyTo(Array *ptr, MonoArray *array, int array_index) {
int count = ptr->size(); unsigned int count = ptr->size();
if (mono_array_length(array) < (array_index + count)) { if (mono_array_length(array) < (array_index + count)) {
MonoException *exc = mono_get_exception_argument("", "Destination array was not long enough. Check destIndex and length, and the array's lower bounds."); MonoException *exc = mono_get_exception_argument("", "Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
@ -94,7 +94,7 @@ void godot_icall_Array_CopyTo(Array *ptr, MonoArray *array, int array_index) {
return; return;
} }
for (int i = 0; i < count; i++) { for (unsigned int i = 0; i < count; i++) {
MonoObject *boxed = GDMonoMarshal::variant_to_mono_object(ptr->operator[](i)); MonoObject *boxed = GDMonoMarshal::variant_to_mono_object(ptr->operator[](i));
mono_array_setref(array, array_index, boxed); mono_array_setref(array, array_index, boxed);
array_index++; array_index++;

View file

@ -91,7 +91,7 @@ static bool _wait_for_debugger_msecs(uint32_t p_msecs) {
OS::get_singleton()->delay_usec((p_msecs < 25 ? p_msecs : 25) * 1000); OS::get_singleton()->delay_usec((p_msecs < 25 ? p_msecs : 25) * 1000);
int tdiff = OS::get_singleton()->get_ticks_msec() - last_tick; uint32_t tdiff = OS::get_singleton()->get_ticks_msec() - last_tick;
if (tdiff > p_msecs) { if (tdiff > p_msecs) {
p_msecs = 0; p_msecs = 0;
@ -864,7 +864,7 @@ Error GDMono::reload_scripts_domain() {
metadata_set_api_assembly_invalidated(APIAssembly::API_EDITOR, true); metadata_set_api_assembly_invalidated(APIAssembly::API_EDITOR, true);
} }
Error err = _unload_scripts_domain(); err = _unload_scripts_domain();
if (err != OK) { if (err != OK) {
WARN_PRINT("Mono: Failed to unload scripts domain"); WARN_PRINT("Mono: Failed to unload scripts domain");
} }

View file

@ -216,7 +216,7 @@ static void _compress_pvrtc4(Image *p_img) {
int ofs, size, w, h; int ofs, size, w, h;
img->get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h); img->get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h);
Javelin::RgbaBitmap bm(w, h); Javelin::RgbaBitmap bm(w, h);
for (unsigned j = 0; j < size / 4; j++) { for (int j = 0; j < size / 4; j++) {
Javelin::ColorRgba<unsigned char> *dp = bm.GetData(); Javelin::ColorRgba<unsigned char> *dp = bm.GetData();
/* red and Green colors are swapped. */ /* red and Green colors are swapped. */
new (dp) Javelin::ColorRgba<unsigned char>(r[ofs + 4 * j + 2], r[ofs + 4 * j + 1], r[ofs + 4 * j], r[ofs + 4 * j + 3]); new (dp) Javelin::ColorRgba<unsigned char>(r[ofs + 4 * j + 2], r[ofs + 4 * j + 1], r[ofs + 4 * j], r[ofs + 4 * j + 3]);

View file

@ -421,7 +421,7 @@ void VisualScriptPropertySelector::get_visual_node_names(const String &root_filt
} }
Vector<String> desc = path[path.size() - 1].replace("(", "( ").replace(")", " )").replace(",", ", ").split(" "); Vector<String> desc = path[path.size() - 1].replace("(", "( ").replace(")", " )").replace(",", ", ").split(" ");
for (size_t i = 0; i < desc.size(); i++) { for (int i = 0; i < desc.size(); i++) {
desc.write[i] = desc[i].capitalize(); desc.write[i] = desc[i].capitalize();
if (desc[i].ends_with(",")) { if (desc[i].ends_with(",")) {
desc.write[i] = desc[i].replace(",", ", "); desc.write[i] = desc[i].replace(",", ", ");

View file

@ -50,7 +50,7 @@ public:
Error write_packet(const uint8_t *p_payload, uint32_t p_size, const T *p_info) { Error write_packet(const uint8_t *p_payload, uint32_t p_size, const T *p_info) {
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
// Verbose buffer warnings // Verbose buffer warnings
if (p_payload && _payload.space_left() < p_size) { if (p_payload && _payload.space_left() < (int32_t)p_size) {
ERR_PRINT("Buffer payload full! Dropping data."); ERR_PRINT("Buffer payload full! Dropping data.");
ERR_FAIL_V(ERR_OUT_OF_MEMORY); ERR_FAIL_V(ERR_OUT_OF_MEMORY);
} }
@ -83,8 +83,8 @@ public:
ERR_FAIL_COND_V(_packets.data_left() < 1, ERR_UNAVAILABLE); ERR_FAIL_COND_V(_packets.data_left() < 1, ERR_UNAVAILABLE);
_Packet p; _Packet p;
_packets.read(&p, 1); _packets.read(&p, 1);
ERR_FAIL_COND_V(_payload.data_left() < p.size, ERR_BUG); ERR_FAIL_COND_V(_payload.data_left() < (int)p.size, ERR_BUG);
ERR_FAIL_COND_V(p_bytes < p.size, ERR_OUT_OF_MEMORY); ERR_FAIL_COND_V(p_bytes < (int)p.size, ERR_OUT_OF_MEMORY);
r_read = p.size; r_read = p.size;
copymem(r_info, &p.info, sizeof(T)); copymem(r_info, &p.info, sizeof(T));

View file

@ -213,7 +213,7 @@ void WebSocketMultiplayerPeer::_send_add(int32_t p_peer_id) {
_send_sys(get_peer(p_peer_id), SYS_ADD, 1); _send_sys(get_peer(p_peer_id), SYS_ADD, 1);
for (Map<int, Ref<WebSocketPeer> >::Element *E = _peer_map.front(); E; E = E->next()) { for (Map<int, Ref<WebSocketPeer> >::Element *E = _peer_map.front(); E; E = E->next()) {
uint32_t id = E->key(); int32_t id = E->key();
if (p_peer_id == id) if (p_peer_id == id)
continue; // Skip the newwly added peer (already confirmed) continue; // Skip the newwly added peer (already confirmed)
@ -226,7 +226,7 @@ void WebSocketMultiplayerPeer::_send_add(int32_t p_peer_id) {
void WebSocketMultiplayerPeer::_send_del(int32_t p_peer_id) { void WebSocketMultiplayerPeer::_send_del(int32_t p_peer_id) {
for (Map<int, Ref<WebSocketPeer> >::Element *E = _peer_map.front(); E; E = E->next()) { for (Map<int, Ref<WebSocketPeer> >::Element *E = _peer_map.front(); E; E = E->next()) {
uint32_t id = E->key(); int32_t id = E->key();
if (p_peer_id != id) if (p_peer_id != id)
_send_sys(get_peer(id), SYS_DEL, p_peer_id); _send_sys(get_peer(id), SYS_DEL, p_peer_id);
} }
@ -288,7 +288,7 @@ void WebSocketMultiplayerPeer::_process_multiplayer(Ref<WebSocketPeer> p_peer, u
data_size = size - PROTO_SIZE; data_size = size - PROTO_SIZE;
uint8_t type = 0; uint8_t type = 0;
int32_t from = 0; uint32_t from = 0;
int32_t to = 0; int32_t to = 0;
copymem(&type, in_buffer, 1); copymem(&type, in_buffer, 1);
copymem(&from, &in_buffer[1], 4); copymem(&from, &in_buffer[1], 4);

View file

@ -108,7 +108,7 @@ bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_ver
float max_x = 0; float max_x = 0;
float max_y = 0; float max_y = 0;
for (int i = 0; i < output->vertexCount; i++) { for (uint32_t i = 0; i < output->vertexCount; i++) {
(*r_vertex)[i] = output->vertexArray[i].xref; (*r_vertex)[i] = output->vertexArray[i].xref;
(*r_uv)[i * 2 + 0] = output->vertexArray[i].uv[0] / w; (*r_uv)[i * 2 + 0] = output->vertexArray[i].uv[0] / w;
(*r_uv)[i * 2 + 1] = output->vertexArray[i].uv[1] / h; (*r_uv)[i * 2 + 1] = output->vertexArray[i].uv[1] / h;
@ -119,7 +119,7 @@ bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_ver
printf("final texsize: %f,%f - max %f,%f\n", w, h, max_x, max_y); printf("final texsize: %f,%f - max %f,%f\n", w, h, max_x, max_y);
*r_vertex_count = output->vertexCount; *r_vertex_count = output->vertexCount;
for (int i = 0; i < output->indexCount; i++) { for (uint32_t i = 0; i < output->indexCount; i++) {
(*r_index)[i] = output->indexArray[i]; (*r_index)[i] = output->indexArray[i];
} }

View file

@ -180,8 +180,8 @@ int detect_prime() {
const char *vendor = (const char *)glGetString(GL_VENDOR); const char *vendor = (const char *)glGetString(GL_VENDOR);
const char *renderer = (const char *)glGetString(GL_RENDERER); const char *renderer = (const char *)glGetString(GL_RENDERER);
int vendor_len = strlen(vendor) + 1; unsigned int vendor_len = strlen(vendor) + 1;
int renderer_len = strlen(renderer) + 1; unsigned int renderer_len = strlen(renderer) + 1;
if (vendor_len + renderer_len >= sizeof(string)) { if (vendor_len + renderer_len >= sizeof(string)) {
renderer_len = 200 - vendor_len; renderer_len = 200 - vendor_len;

View file

@ -887,7 +887,7 @@ void VoxelLightBaker::plot_light_directional(const Vector3 &p_direction, const C
distance -= distance_adv; distance -= distance_adv;
} }
if (result == idx) { if (result == (uint32_t)idx) {
//cell hit itself! hooray! //cell hit itself! hooray!
Vector3 normal(cells[idx].normal[0], cells[idx].normal[1], cells[idx].normal[2]); Vector3 normal(cells[idx].normal[0], cells[idx].normal[1], cells[idx].normal[2]);
@ -1018,7 +1018,7 @@ void VoxelLightBaker::plot_light_omni(const Vector3 &p_pos, const Color &p_color
distance -= distance_adv; distance -= distance_adv;
} }
if (result == idx) { if (result == (uint32_t)idx) {
//cell hit itself! hooray! //cell hit itself! hooray!
if (normal == Vector3()) { if (normal == Vector3()) {
@ -1152,7 +1152,7 @@ void VoxelLightBaker::plot_light_spot(const Vector3 &p_pos, const Vector3 &p_axi
distance -= distance_adv; distance -= distance_adv;
} }
if (result == idx) { if (result == (uint32_t)idx) {
//cell hit itself! hooray! //cell hit itself! hooray!
if (normal == Vector3()) { if (normal == Vector3()) {
@ -2252,7 +2252,7 @@ void VoxelLightBaker::_debug_mesh(int p_idx, int p_level, const AABB &p_aabb, Re
uint32_t child = bake_cells[p_idx].children[i]; uint32_t child = bake_cells[p_idx].children[i];
if (child == CHILD_EMPTY || child >= max_original_cells) if (child == CHILD_EMPTY || child >= (uint32_t)max_original_cells)
continue; continue;
AABB aabb = p_aabb; AABB aabb = p_aabb;

View file

@ -332,7 +332,7 @@ Vector<Vector2> BitMap::_march_square(const Rect2i &rect, const Point2i &start)
prevx = stepx; prevx = stepx;
prevy = stepy; prevy = stepy;
ERR_FAIL_COND_V(count > width * height, _points); ERR_FAIL_COND_V((int)count > width * height, _points);
} while (curx != startx || cury != starty); } while (curx != startx || cury != starty);
return _points; return _points;
} }

View file

@ -138,7 +138,7 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr
Vector<int32_t> buf = AudioDriver::get_singleton()->get_input_buffer(); Vector<int32_t> buf = AudioDriver::get_singleton()->get_input_buffer();
unsigned int input_size = AudioDriver::get_singleton()->get_input_size(); unsigned int input_size = AudioDriver::get_singleton()->get_input_size();
int mix_rate = AudioDriver::get_singleton()->get_mix_rate(); int mix_rate = AudioDriver::get_singleton()->get_mix_rate();
int playback_delay = MIN(((50 * mix_rate) / 1000) * 2, buf.size() >> 1); unsigned int playback_delay = MIN(((50 * mix_rate) / 1000) * 2, buf.size() >> 1);
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
unsigned int input_position = AudioDriver::get_singleton()->get_input_position(); unsigned int input_position = AudioDriver::get_singleton()->get_input_position();
#endif #endif
@ -152,11 +152,11 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr
for (int i = 0; i < p_frames; i++) { for (int i = 0; i < p_frames; i++) {
if (input_size > input_ofs) { if (input_size > input_ofs) {
float l = (buf[input_ofs++] >> 16) / 32768.f; float l = (buf[input_ofs++] >> 16) / 32768.f;
if (input_ofs >= buf.size()) { if ((int)input_ofs >= buf.size()) {
input_ofs = 0; input_ofs = 0;
} }
float r = (buf[input_ofs++] >> 16) / 32768.f; float r = (buf[input_ofs++] >> 16) / 32768.f;
if (input_ofs >= buf.size()) { if ((int)input_ofs >= buf.size()) {
input_ofs = 0; input_ofs = 0;
} }
@ -168,7 +168,7 @@ void AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fr
} }
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (input_ofs > input_position && (input_ofs - input_position) < (p_frames * 2)) { if (input_ofs > input_position && (int)(input_ofs - input_position) < (p_frames * 2)) {
print_verbose(String(get_class_name()) + " buffer underrun: input_position=" + itos(input_position) + " input_ofs=" + itos(input_ofs) + " input_size=" + itos(input_size)); print_verbose(String(get_class_name()) + " buffer underrun: input_position=" + itos(input_position) + " input_ofs=" + itos(input_ofs) + " input_size=" + itos(input_size));
} }
#endif #endif

View file

@ -91,10 +91,10 @@ void AudioDriver::input_buffer_init(int driver_buffer_frames) {
void AudioDriver::input_buffer_write(int32_t sample) { void AudioDriver::input_buffer_write(int32_t sample) {
input_buffer.write[input_position++] = sample; input_buffer.write[input_position++] = sample;
if (input_position >= input_buffer.size()) { if ((int)input_position >= input_buffer.size()) {
input_position = 0; input_position = 0;
} }
if (input_size < input_buffer.size()) { if ((int)input_size < input_buffer.size()) {
input_size++; input_size++;
} }
} }