From aab8da25ad2c3e6d2df03abbc8e35c1725938c40 Mon Sep 17 00:00:00 2001 From: qarmin Date: Tue, 23 Jul 2019 09:14:31 +0200 Subject: [PATCH] Fix some code found by Coverity Scan and PVS Studio --- core/class_db.cpp | 1 + core/io/packet_peer.cpp | 5 +- core/io/resource_format_binary.cpp | 2 +- core/make_binders.py | 3 -- editor/import/editor_import_collada.cpp | 46 +++++++++---------- editor/import/resource_importer_scene.cpp | 1 + editor/plugins/mesh_library_editor_plugin.cpp | 2 + editor/scene_tree_dock.cpp | 2 + editor/script_editor_debugger.h | 2 +- modules/bullet/space_bullet.cpp | 4 ++ platform/uwp/export/export.cpp | 4 +- platform/x11/joypad_linux.cpp | 2 + platform/x11/power_x11.cpp | 4 +- scene/main/instance_placeholder.cpp | 2 + scene/main/scene_tree.cpp | 5 +- scene/resources/visual_shader_nodes.h | 4 +- servers/physics/collision_solver_sat.cpp | 4 +- servers/physics/collision_solver_sw.cpp | 4 -- servers/physics/joints/hinge_joint_sw.cpp | 1 - servers/physics_2d/body_2d_sw.cpp | 24 +++++----- .../physics_2d/collision_solver_2d_sat.cpp | 4 +- servers/physics_2d/collision_solver_2d_sw.cpp | 2 - servers/visual/shader_language.cpp | 4 +- servers/visual_server.cpp | 4 +- 24 files changed, 71 insertions(+), 65 deletions(-) diff --git a/core/class_db.cpp b/core/class_db.cpp index 794d9900838..49e3f94d8f2 100644 --- a/core/class_db.cpp +++ b/core/class_db.cpp @@ -480,6 +480,7 @@ uint64_t ClassDB::get_api_hash(APIType p_api) { for (List::Element *F = snames.front(); F; F = F->next()) { PropertySetGet *psg = t->property_setget.getptr(F->get()); + ERR_FAIL_COND_V(!psg, 0); hash = hash_djb2_one_64(F->get().hash(), hash); hash = hash_djb2_one_64(psg->setter.hash(), hash); diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index c77c81f9e2a..1e4ea715b3f 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -110,10 +110,11 @@ Error PacketPeer::put_var(const Variant &p_packet, bool p_full_objects) { Variant PacketPeer::_bnd_get_var(bool p_allow_objects) { Variant var; - get_var(var, p_allow_objects); + Error err = get_var(var, p_allow_objects); + ERR_FAIL_COND_V(err != OK, Variant()); return var; -}; +} Error PacketPeer::_put_packet(const PoolVector &p_buffer) { return put_packet_buffer(p_buffer); diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 38bef2768ea..146480e5a26 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -718,8 +718,8 @@ Error ResourceInteractiveLoaderBinary::poll() { Resource *r = Object::cast_to(obj); if (!r) { error = ERR_FILE_CORRUPT; - memdelete(obj); //bye ERR_EXPLAIN(local_path + ":Resource type in resource field not a resource, type is: " + obj->get_class()); + memdelete(obj); //bye ERR_FAIL_V(ERR_FILE_CORRUPT); } diff --git a/core/make_binders.py b/core/make_binders.py index 24901c42a11..c38db5cef46 100644 --- a/core/make_binders.py +++ b/core/make_binders.py @@ -334,9 +334,6 @@ def make_version(template, nargs, argmax, const, ret): elif (cmd == "noarg"): for i in range(nargs + 1, argmax + 1): outtext += data.replace("@", str(i)) - elif (cmd == "noarg"): - for i in range(nargs + 1, argmax + 1): - outtext += data.replace("@", str(i)) from_pos = end + 1 diff --git a/editor/import/editor_import_collada.cpp b/editor/import/editor_import_collada.cpp index e152827c632..1c4a8c43a92 100644 --- a/editor/import/editor_import_collada.cpp +++ b/editor/import/editor_import_collada.cpp @@ -1175,35 +1175,33 @@ Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compres morph = &collada.state.morph_controller_data_map[ngsource]; meshid = morph->mesh; - Vector targets; + if (morph->targets.has("MORPH_TARGET")) { + String target = morph->targets["MORPH_TARGET"]; + bool valid = false; + if (morph->sources.has(target)) { + valid = true; + Vector names = morph->sources[target].sarray; + for (int i = 0; i < names.size(); i++) { - morph->targets.has("MORPH_TARGET"); - String target = morph->targets["MORPH_TARGET"]; - bool valid = false; - if (morph->sources.has(target)) { - valid = true; - Vector names = morph->sources[target].sarray; - for (int i = 0; i < names.size(); i++) { + String meshid2 = names[i]; + if (collada.state.mesh_data_map.has(meshid2)) { + Ref mesh = Ref(memnew(ArrayMesh)); + const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid2]; + mesh->set_name(meshdata.name); + Error err = _create_mesh_surfaces(false, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, NULL, Vector >(), false); + ERR_FAIL_COND_V(err, err); - String meshid2 = names[i]; - if (collada.state.mesh_data_map.has(meshid2)) { - Ref mesh = Ref(memnew(ArrayMesh)); - const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid2]; - mesh->set_name(meshdata.name); - Error err = _create_mesh_surfaces(false, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, NULL, Vector >(), false); - ERR_FAIL_COND_V(err, err); - - morphs.push_back(mesh); - } else { - valid = false; + morphs.push_back(mesh); + } else { + valid = false; + } } } + + if (!valid) + morphs.clear(); + ngsource = ""; } - - if (!valid) - morphs.clear(); - - ngsource = ""; } if (ngsource != "") { diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index 53b67c46b0c..8e6a56a9297 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -435,6 +435,7 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map Object::cast_to(sb)->set_transform(Object::cast_to(p_node)->get_transform()); p_node->replace_by(sb); memdelete(p_node); + p_node = NULL; CollisionShape *colshape = memnew(CollisionShape); if (empty_draw_type == "CUBE") { BoxShape *boxShape = memnew(BoxShape); diff --git a/editor/plugins/mesh_library_editor_plugin.cpp b/editor/plugins/mesh_library_editor_plugin.cpp index e582f6ded28..1fc6dae9783 100644 --- a/editor/plugins/mesh_library_editor_plugin.cpp +++ b/editor/plugins/mesh_library_editor_plugin.cpp @@ -201,6 +201,8 @@ void MeshLibraryEditor::_import_scene_cbk(const String &p_str) { ERR_FAIL_COND(ps.is_null()); Node *scene = ps->instance(); + ERR_FAIL_COND(!scene); + _import_scene(scene, mesh_library, option == MENU_OPTION_UPDATE_FROM_SCENE); memdelete(scene); diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index 009ac603e2f..aeee829de2b 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -301,6 +301,8 @@ bool SceneTreeDock::_track_inherit(const String &p_target_scene_path, Node *p_de Ref data = ResourceLoader::load(path); if (data.is_valid()) { p = data->instance(PackedScene::GEN_EDIT_STATE_INSTANCE); + if (!p) + continue; instances.push_back(p); } else break; diff --git a/editor/script_editor_debugger.h b/editor/script_editor_debugger.h index 505eab266f9..947b0cca527 100644 --- a/editor/script_editor_debugger.h +++ b/editor/script_editor_debugger.h @@ -173,7 +173,7 @@ class ScriptEditorDebugger : public Control { void _set_reason_text(const String &p_reason, MessageType p_type); void _scene_tree_property_select_object(ObjectID p_object); void _scene_tree_property_value_edited(const String &p_prop, const Variant &p_value); - int _update_scene_tree(TreeItem *parent, const Array &items, int current_index); + int _update_scene_tree(TreeItem *parent, const Array &nodes, int current_index); void _video_mem_request(); diff --git a/modules/bullet/space_bullet.cpp b/modules/bullet/space_bullet.cpp index 738b415d162..9d632aaf832 100644 --- a/modules/bullet/space_bullet.cpp +++ b/modules/bullet/space_bullet.cpp @@ -581,6 +581,10 @@ void SpaceBullet::create_empty_world(bool p_create_soft_world) { } else { world_mem = malloc(sizeof(btDiscreteDynamicsWorld)); } + if (!world_mem) { + ERR_EXPLAIN("Out of memory"); + ERR_FAIL(); + } if (p_create_soft_world) { collisionConfiguration = bulletnew(GodotSoftCollisionConfiguration(static_cast(world_mem))); diff --git a/platform/uwp/export/export.cpp b/platform/uwp/export/export.cpp index abb7b391d35..75ce422e9e1 100644 --- a/platform/uwp/export/export.cpp +++ b/platform/uwp/export/export.cpp @@ -519,7 +519,9 @@ Error AppxPackager::add_file(String p_file_name, const uint8_t *p_buffer, size_t int total_out_before = strm.total_out; - deflate(&strm, Z_FULL_FLUSH); + int err = deflate(&strm, Z_FULL_FLUSH); + ERR_FAIL_COND_V(err >= 0, ERR_BUG); // Negative means bug + bh.compressed_size = strm.total_out - total_out_before; //package->store_buffer(strm_out.ptr(), strm.total_out - total_out_before); diff --git a/platform/x11/joypad_linux.cpp b/platform/x11/joypad_linux.cpp index e6328ee14d7..42429523749 100644 --- a/platform/x11/joypad_linux.cpp +++ b/platform/x11/joypad_linux.cpp @@ -513,6 +513,8 @@ void JoypadLinux::process_joypads() { break; default: + if (ev.code >= MAX_ABS) + return; if (joy->abs_map[ev.code] != -1 && joy->abs_info[ev.code]) { InputDefault::JoyAxis value = axis_correct(joy->abs_info[ev.code], ev.value); joy->curr_axis[joy->abs_map[ev.code]] = value; diff --git a/platform/x11/power_x11.cpp b/platform/x11/power_x11.cpp index 758bd84114b..c33c77e16b6 100644 --- a/platform/x11/power_x11.cpp +++ b/platform/x11/power_x11.cpp @@ -268,9 +268,7 @@ bool PowerX11::GetPowerInfo_Linux_proc_acpi() { check_proc_acpi_battery(node.utf8().get_data(), &have_battery, &charging /*, seconds, percent*/); node = dirp->get_next(); } - memdelete(dirp); } - dirp->change_dir(proc_acpi_ac_adapter_path); err = dirp->list_dir_begin(); if (err != OK) { @@ -281,7 +279,6 @@ bool PowerX11::GetPowerInfo_Linux_proc_acpi() { check_proc_acpi_ac_adapter(node.utf8().get_data(), &have_ac); node = dirp->get_next(); } - memdelete(dirp); } if (!have_battery) { @@ -294,6 +291,7 @@ bool PowerX11::GetPowerInfo_Linux_proc_acpi() { this->power_state = OS::POWERSTATE_ON_BATTERY; } + memdelete(dirp); return true; /* definitive answer. */ } diff --git a/scene/main/instance_placeholder.cpp b/scene/main/instance_placeholder.cpp index 71addd6fea7..99ecc8bc377 100644 --- a/scene/main/instance_placeholder.cpp +++ b/scene/main/instance_placeholder.cpp @@ -92,6 +92,8 @@ Node *InstancePlaceholder::create_instance(bool p_replace, const Refinstance(); + if (!scene) + return NULL; scene->set_name(get_name()); int pos = get_position_in_parent(); diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp index 2badf19f2bb..dbf3150ae05 100644 --- a/scene/main/scene_tree.cpp +++ b/scene/main/scene_tree.cpp @@ -1507,8 +1507,11 @@ void SceneTree::_live_edit_instance_node_func(const NodePath &p_parent, const St Node *n2 = n->get_node(p_parent); Node *no = ps->instance(); - no->set_name(p_name); + if (!no) { + continue; + } + no->set_name(p_name); n2->add_child(no); } } diff --git a/scene/resources/visual_shader_nodes.h b/scene/resources/visual_shader_nodes.h index cafc7a0f00d..4585e7fdcc4 100644 --- a/scene/resources/visual_shader_nodes.h +++ b/scene/resources/visual_shader_nodes.h @@ -1624,13 +1624,13 @@ public: virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const; //if no output is connected, the output var passed will be empty. if no input is connected and input is NIL, the input var passed will be empty - void set_comparsion_type(ComparsionType p_func); + void set_comparsion_type(ComparsionType p_type); ComparsionType get_comparsion_type() const; void set_function(Function p_func); Function get_function() const; - void set_condition(Condition p_mode); + void set_condition(Condition p_cond); Condition get_condition() const; virtual Vector get_editable_properties() const; diff --git a/servers/physics/collision_solver_sat.cpp b/servers/physics/collision_solver_sat.cpp index a13fa65009e..d0f8fd8aff0 100644 --- a/servers/physics/collision_solver_sat.cpp +++ b/servers/physics/collision_solver_sat.cpp @@ -274,8 +274,8 @@ static void _generate_contacts_from_supports(const Vector3 *p_points_A, int p_po points_B = p_points_B; } - int version_A = (pointcount_A > 3 ? 3 : pointcount_A) - 1; - int version_B = (pointcount_B > 3 ? 3 : pointcount_B) - 1; + int version_A = (pointcount_A > 2 ? 2 : pointcount_A) - 1; + int version_B = (pointcount_B > 2 ? 2 : pointcount_B) - 1; GenerateContactsFunc contacts_func = generate_contacts_func_table[version_A][version_B]; ERR_FAIL_COND(!contacts_func); diff --git a/servers/physics/collision_solver_sw.cpp b/servers/physics/collision_solver_sw.cpp index 0d10dae8cc0..d970dd39fbd 100644 --- a/servers/physics/collision_solver_sw.cpp +++ b/servers/physics/collision_solver_sw.cpp @@ -233,8 +233,6 @@ bool CollisionSolverSW::solve_static(const ShapeSW *p_shape_A, const Transform & return collision_solver(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, r_sep_axis, p_margin_A, p_margin_B); } - - return false; } void CollisionSolverSW::concave_distance_callback(void *p_userdata, ShapeSW *p_convex) { @@ -371,6 +369,4 @@ bool CollisionSolverSW::solve_distance(const ShapeSW *p_shape_A, const Transform return gjk_epa_calculate_distance(p_shape_A, p_transform_A, p_shape_B, p_transform_B, r_point_A, r_point_B); //should pass sepaxis.. } - - return false; } diff --git a/servers/physics/joints/hinge_joint_sw.cpp b/servers/physics/joints/hinge_joint_sw.cpp index 1d1b30286e0..209cddda5e3 100644 --- a/servers/physics/joints/hinge_joint_sw.cpp +++ b/servers/physics/joints/hinge_joint_sw.cpp @@ -198,7 +198,6 @@ bool HingeJointSW::setup(real_t p_step) { plane_space(m_rbAFrame.basis.get_axis(2), jointAxis0local, jointAxis1local); - A->get_transform().basis.xform(m_rbAFrame.basis.get_axis(2)); Vector3 jointAxis0 = A->get_transform().basis.xform(jointAxis0local); Vector3 jointAxis1 = A->get_transform().basis.xform(jointAxis1local); Vector3 hingeAxisWorld = A->get_transform().basis.xform(m_rbAFrame.basis.get_axis(2)); diff --git a/servers/physics_2d/body_2d_sw.cpp b/servers/physics_2d/body_2d_sw.cpp index 60bbcef4b61..5dff655ea17 100644 --- a/servers/physics_2d/body_2d_sw.cpp +++ b/servers/physics_2d/body_2d_sw.cpp @@ -185,28 +185,28 @@ real_t Body2DSW::get_param(Physics2DServer::BodyParameter p_param) const { case Physics2DServer::BODY_PARAM_BOUNCE: { return bounce; - } break; + } case Physics2DServer::BODY_PARAM_FRICTION: { return friction; - } break; + } case Physics2DServer::BODY_PARAM_MASS: { return mass; - } break; + } case Physics2DServer::BODY_PARAM_INERTIA: { return _inv_inertia == 0 ? 0 : 1.0 / _inv_inertia; - } break; + } case Physics2DServer::BODY_PARAM_GRAVITY_SCALE: { return gravity_scale; - } break; + } case Physics2DServer::BODY_PARAM_LINEAR_DAMP: { return linear_damp; - } break; + } case Physics2DServer::BODY_PARAM_ANGULAR_DAMP: { return angular_damp; - } break; + } default: { } } @@ -343,19 +343,19 @@ Variant Body2DSW::get_state(Physics2DServer::BodyState p_state) const { switch (p_state) { case Physics2DServer::BODY_STATE_TRANSFORM: { return get_transform(); - } break; + } case Physics2DServer::BODY_STATE_LINEAR_VELOCITY: { return linear_velocity; - } break; + } case Physics2DServer::BODY_STATE_ANGULAR_VELOCITY: { return angular_velocity; - } break; + } case Physics2DServer::BODY_STATE_SLEEPING: { return !is_active(); - } break; + } case Physics2DServer::BODY_STATE_CAN_SLEEP: { return can_sleep; - } break; + } } return Variant(); diff --git a/servers/physics_2d/collision_solver_2d_sat.cpp b/servers/physics_2d/collision_solver_2d_sat.cpp index 9d75f71ff08..19e4b8c1d93 100644 --- a/servers/physics_2d/collision_solver_2d_sat.cpp +++ b/servers/physics_2d/collision_solver_2d_sat.cpp @@ -172,8 +172,8 @@ static void _generate_contacts_from_supports(const Vector2 *p_points_A, int p_po points_B = p_points_B; } - int version_A = (pointcount_A > 3 ? 3 : pointcount_A) - 1; - int version_B = (pointcount_B > 3 ? 3 : pointcount_B) - 1; + int version_A = (pointcount_A > 2 ? 2 : pointcount_A) - 1; + int version_B = (pointcount_B > 2 ? 2 : pointcount_B) - 1; GenerateContactsFunc contacts_func = generate_contacts_func_table[version_A][version_B]; ERR_FAIL_COND(!contacts_func); diff --git a/servers/physics_2d/collision_solver_2d_sw.cpp b/servers/physics_2d/collision_solver_2d_sw.cpp index e49961c0480..03c0fd59811 100644 --- a/servers/physics_2d/collision_solver_2d_sw.cpp +++ b/servers/physics_2d/collision_solver_2d_sw.cpp @@ -249,6 +249,4 @@ bool CollisionSolver2DSW::solve(const Shape2DSW *p_shape_A, const Transform2D &p return collision_solver(p_shape_A, p_transform_A, p_motion_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, sep_axis, margin_A, margin_B); } - - return false; } diff --git a/servers/visual/shader_language.cpp b/servers/visual/shader_language.cpp index 14fefbf1957..25973aa2957 100644 --- a/servers/visual/shader_language.cpp +++ b/servers/visual/shader_language.cpp @@ -967,7 +967,7 @@ bool ShaderLanguage::_find_identifier(const BlockNode *p_block, const Mapop) { case OP_EQUAL: @@ -3059,7 +3059,7 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons String ident = identifier; bool ok = true; - DataType member_type; + DataType member_type = TYPE_VOID; switch (dt) { case TYPE_BVEC2: case TYPE_IVEC2: diff --git a/servers/visual_server.cpp b/servers/visual_server.cpp index c6468694fda..25e18d06235 100644 --- a/servers/visual_server.cpp +++ b/servers/visual_server.cpp @@ -1118,7 +1118,7 @@ void VisualServer::mesh_add_surface_from_arrays(RID p_mesh, PrimitiveType p_prim } offsets[i] = elem_size; continue; - } break; + } default: { ERR_FAIL(); } @@ -1286,7 +1286,7 @@ Array VisualServer::_get_array_from_surface(uint32_t p_format, PoolVector