From 7f03db2f1815b6b31959a5a63c4235ccf7d6644d Mon Sep 17 00:00:00 2001 From: Liran-Ar <106250407+Liran-Ar@users.noreply.github.com> Date: Mon, 23 Jan 2023 16:29:54 +0200 Subject: [PATCH] Fix potential risks (#2516) - What I did Fix the following potential risks: Uncaught exceptions in /src/nvos-swss/orchagent/main.cpp and /src/nvos-swss/portsyncd/portsyncd.cpp Uninitialized scalar fields in /src/nvos-swss/orchagent/port.h Unchecked return value in /src/nvos-swss/orchagent/portsorch.cpp Pointer to local outside scope in /src/nvos-swss/orchagent/portsorch.cpp - How I did it Add "catch" to the uncaught exceptions Add initialization value to the uninitialized fields Add a check to the unchecked return value Delete an unnecessary comma - How I verified it Inspection and sanity tests Signed-off-by: Liran Artzi --- orchagent/main.cpp | 114 +++++++++++++++++++++++----------------- orchagent/port.h | 2 +- orchagent/portsorch.cpp | 8 ++- portsyncd/portsyncd.cpp | 52 ++++++++++-------- 4 files changed, 103 insertions(+), 73 deletions(-) diff --git a/orchagent/main.cpp b/orchagent/main.cpp index 8d204dbf2d52..03bb5da55031 100644 --- a/orchagent/main.cpp +++ b/orchagent/main.cpp @@ -170,9 +170,17 @@ void getCfgSwitchType(DBConnector *cfgDb, string &switch_type) { Table cfgDeviceMetaDataTable(cfgDb, CFG_DEVICE_METADATA_TABLE_NAME); - if (!cfgDeviceMetaDataTable.hget("localhost", "switch_type", switch_type)) + try { - //Switch type is not configured. Consider it default = "switch" (regular switch) + if (!cfgDeviceMetaDataTable.hget("localhost", "switch_type", switch_type)) + { + //Switch type is not configured. Consider it default = "switch" (regular switch) + switch_type = "switch"; + } + } + catch(const std::system_error& e) + { + SWSS_LOG_ERROR("System error: %s", e.what()); switch_type = "switch"; } @@ -196,64 +204,72 @@ bool getSystemPortConfigList(DBConnector *cfgDb, DBConnector *appDb, vector