From 8a6eaed01ff52e93257a23a964da45573a7027c4 Mon Sep 17 00:00:00 2001 From: Bernd Hahnebach Date: Mon, 20 Jul 2020 22:19:08 +0200 Subject: [PATCH] FEM: coverity, Null pointer dereferences --- src/Mod/Fem/femexamples/manager.py | 12 ++++---- src/Mod/Fem/feminout/importInpMesh.py | 7 ++++- src/Mod/Fem/feminout/importToolsFem.py | 39 +++++++++++++------------- src/Mod/Fem/femmesh/meshtools.py | 2 ++ 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Mod/Fem/femexamples/manager.py b/src/Mod/Fem/femexamples/manager.py index 2c3870b6ed..d26ebd14b1 100644 --- a/src/Mod/Fem/femexamples/manager.py +++ b/src/Mod/Fem/femexamples/manager.py @@ -90,11 +90,13 @@ def run_example(example, solver=None, base_name=None): from importlib import import_module module = import_module("femexamples." + example) - if hasattr(module, "setup"): - if solver is None: - doc = getattr(module, "setup")() - else: - doc = getattr(module, "setup")(solvertype=solver) + if not hasattr(module, "setup"): + FreeCAD.Console.PrintError("Setup method not found in {}\n".format(example)) + + if solver is None: + doc = getattr(module, "setup")() + else: + doc = getattr(module, "setup")(solvertype=solver) if base_name is None: base_name = example diff --git a/src/Mod/Fem/feminout/importInpMesh.py b/src/Mod/Fem/feminout/importInpMesh.py index ebb37a1ab1..0ff1d67f82 100644 --- a/src/Mod/Fem/feminout/importInpMesh.py +++ b/src/Mod/Fem/feminout/importInpMesh.py @@ -135,6 +135,7 @@ def read_inp(file_name): continue read_node = False elm_category = [] + number_of_nodes = 0 elm_2nd_line = False # reading nodes @@ -194,8 +195,10 @@ def read_inp(file_name): elm_category = elements.seg3 number_of_nodes = 3 error_seg3 = True # to print "not supported" + else: + error_not_supported_elemtype = True - elif elm_category != []: + elif elm_category != [] and number_of_nodes > 0: line_list = line.split(",") if elm_2nd_line is False: number = int(line_list[0]) @@ -216,6 +219,8 @@ def read_inp(file_name): model_definition = False if error_seg3 is True: # to print "not supported" Console.PrintError("Error: seg3 (3-node beam element type) not supported, yet.\n") + elif error_not_supported_elemtype is True: + Console.PrintError("Error: {} not supported.\n".format(elm_type)) f.close() # switch from the CalculiX node numbering to the FreeCAD node numbering diff --git a/src/Mod/Fem/feminout/importToolsFem.py b/src/Mod/Fem/feminout/importToolsFem.py index 0b93e6fa6a..9fe1afbe33 100644 --- a/src/Mod/Fem/feminout/importToolsFem.py +++ b/src/Mod/Fem/feminout/importToolsFem.py @@ -430,25 +430,26 @@ def fill_femresult_mechanical( if eigenmode_number > 0: res_obj.Eigenmode = eigenmode_number - # fill res_obj.Temperature if they exist - # TODO, check if it is possible to have Temperature without disp - # we would need to set NodeNumbers than - if "temp" in result_set: - Temperature = result_set["temp"] - if len(Temperature) > 0: - if len(Temperature.values()) != len(disp.values()): - Temp = [] - Temp_extra_nodes = list(Temperature.values()) - nodes = len(disp.values()) - for i in range(nodes): - # how is this possible? An example is needed! - Console.PrintError("Temperature seams to have exptra nodes.\n") - Temp_value = Temp_extra_nodes[i] - Temp.append(Temp_value) - res_obj.Temperature = list(map((lambda x: x), Temp)) - else: - res_obj.Temperature = list(map((lambda x: x), Temperature.values())) - res_obj.Time = step_time + # it is assumed Temperature can not exist without disp + # TODO really proof this + # if temperature can exist without disp: + # move them out of disp if conditiona and set NodeNumbers + if "temp" in result_set: + Temperature = result_set["temp"] + if len(Temperature) > 0: + if len(Temperature.values()) != len(disp.values()): + Temp = [] + Temp_extra_nodes = list(Temperature.values()) + nodes = len(disp.values()) + for i in range(nodes): + # how is this possible? An example is needed! + Console.PrintError("Temperature seams to have exptra nodes.\n") + Temp_value = Temp_extra_nodes[i] + Temp.append(Temp_value) + res_obj.Temperature = list(map((lambda x: x), Temp)) + else: + res_obj.Temperature = list(map((lambda x: x), Temperature.values())) + res_obj.Time = step_time # fill res_obj.MassFlow if "mflow" in result_set: diff --git a/src/Mod/Fem/femmesh/meshtools.py b/src/Mod/Fem/femmesh/meshtools.py index d281f309a5..eab19d2047 100644 --- a/src/Mod/Fem/femmesh/meshtools.py +++ b/src/Mod/Fem/femmesh/meshtools.py @@ -1699,6 +1699,7 @@ def get_contact_obj_faces( "or not supported reference shape elements, contact face combination " "(example: multiple element faces per master or slave\n" ) + return [[], []] FreeCAD.Console.PrintLog(" Slave: {}, {}\n".format(slave_ref[0].Name, slave_ref)) FreeCAD.Console.PrintLog(" Master: {}, {}\n".format(master_ref[0].Name, master_ref)) @@ -1790,6 +1791,7 @@ def get_tie_obj_faces( "or not supported reference shape elements, contact face combination " "(example: multiple element faces per master or slave\n" ) + return [[], []] FreeCAD.Console.PrintLog("Slave: {}, {}\n".format(slave_ref[0].Name, slave_ref)) FreeCAD.Console.PrintLog("Master: {}, {}\n".format(master_ref[0].Name, master_ref))