Skip to content

Commit 7f03db2

Browse files
authored
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 <[email protected]>
1 parent 383ee68 commit 7f03db2

File tree

4 files changed

+103
-73
lines changed

4 files changed

+103
-73
lines changed

orchagent/main.cpp

+65-49
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,17 @@ void getCfgSwitchType(DBConnector *cfgDb, string &switch_type)
170170
{
171171
Table cfgDeviceMetaDataTable(cfgDb, CFG_DEVICE_METADATA_TABLE_NAME);
172172

173-
if (!cfgDeviceMetaDataTable.hget("localhost", "switch_type", switch_type))
173+
try
174174
{
175-
//Switch type is not configured. Consider it default = "switch" (regular switch)
175+
if (!cfgDeviceMetaDataTable.hget("localhost", "switch_type", switch_type))
176+
{
177+
//Switch type is not configured. Consider it default = "switch" (regular switch)
178+
switch_type = "switch";
179+
}
180+
}
181+
catch(const std::system_error& e)
182+
{
183+
SWSS_LOG_ERROR("System error: %s", e.what());
176184
switch_type = "switch";
177185
}
178186

@@ -196,64 +204,72 @@ bool getSystemPortConfigList(DBConnector *cfgDb, DBConnector *appDb, vector<sai_
196204
return true;
197205
}
198206

199-
string value;
200-
if (!cfgDeviceMetaDataTable.hget("localhost", "switch_id", value))
207+
try
201208
{
202-
//VOQ switch id is not configured.
203-
SWSS_LOG_ERROR("VOQ switch id is not configured");
204-
return false;
205-
}
209+
string value;
210+
if (!cfgDeviceMetaDataTable.hget("localhost", "switch_id", value))
211+
{
212+
//VOQ switch id is not configured.
213+
SWSS_LOG_ERROR("VOQ switch id is not configured");
214+
return false;
215+
}
206216

207-
if (value.size())
208-
gVoqMySwitchId = stoi(value);
217+
if (value.size())
218+
gVoqMySwitchId = stoi(value);
209219

210-
if (gVoqMySwitchId < 0)
211-
{
212-
SWSS_LOG_ERROR("Invalid VOQ switch id %d configured", gVoqMySwitchId);
213-
return false;
214-
}
220+
if (gVoqMySwitchId < 0)
221+
{
222+
SWSS_LOG_ERROR("Invalid VOQ switch id %d configured", gVoqMySwitchId);
223+
return false;
224+
}
215225

216-
if (!cfgDeviceMetaDataTable.hget("localhost", "max_cores", value))
217-
{
218-
//VOQ max cores is not configured.
219-
SWSS_LOG_ERROR("VOQ max cores is not configured");
220-
return false;
221-
}
226+
if (!cfgDeviceMetaDataTable.hget("localhost", "max_cores", value))
227+
{
228+
//VOQ max cores is not configured.
229+
SWSS_LOG_ERROR("VOQ max cores is not configured");
230+
return false;
231+
}
222232

223-
if (value.size())
224-
gVoqMaxCores = stoi(value);
233+
if (value.size())
234+
gVoqMaxCores = stoi(value);
225235

226-
if (gVoqMaxCores == 0)
227-
{
228-
SWSS_LOG_ERROR("Invalid VOQ max cores %d configured", gVoqMaxCores);
229-
return false;
230-
}
236+
if (gVoqMaxCores == 0)
237+
{
238+
SWSS_LOG_ERROR("Invalid VOQ max cores %d configured", gVoqMaxCores);
239+
return false;
240+
}
231241

232-
if (!cfgDeviceMetaDataTable.hget("localhost", "hostname", value))
233-
{
234-
// hostname is not configured.
235-
SWSS_LOG_ERROR("Host name is not configured");
236-
return false;
237-
}
238-
gMyHostName = value;
242+
if (!cfgDeviceMetaDataTable.hget("localhost", "hostname", value))
243+
{
244+
// hostname is not configured.
245+
SWSS_LOG_ERROR("Host name is not configured");
246+
return false;
247+
}
248+
gMyHostName = value;
239249

240-
if (!gMyHostName.size())
241-
{
242-
SWSS_LOG_ERROR("Invalid host name %s configured", gMyHostName.c_str());
243-
return false;
244-
}
250+
if (!gMyHostName.size())
251+
{
252+
SWSS_LOG_ERROR("Invalid host name %s configured", gMyHostName.c_str());
253+
return false;
254+
}
245255

246-
if (!cfgDeviceMetaDataTable.hget("localhost", "asic_name", value))
247-
{
248-
// asic_name is not configured.
249-
SWSS_LOG_ERROR("Asic name is not configured");
250-
return false;
251-
}
252-
gMyAsicName = value;
256+
if (!cfgDeviceMetaDataTable.hget("localhost", "asic_name", value))
257+
{
258+
// asic_name is not configured.
259+
SWSS_LOG_ERROR("Asic name is not configured");
260+
return false;
261+
}
262+
gMyAsicName = value;
253263

254-
if (!gMyAsicName.size())
264+
if (!gMyAsicName.size())
265+
{
266+
SWSS_LOG_ERROR("Invalid asic name %s configured", gMyAsicName.c_str());
267+
return false;
268+
}
269+
}
270+
catch(const std::system_error& e)
255271
{
256-
SWSS_LOG_ERROR("Invalid asic name %s configured", gMyAsicName.c_str());
272+
SWSS_LOG_ERROR("System error: %s", e.what());
257273
return false;
258274
}
259275

orchagent/port.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Port
113113
}
114114

115115
std::string m_alias;
116-
Type m_type;
116+
Type m_type = UNKNOWN;
117117
int m_index = 0; // PHY_PORT: index
118118
uint32_t m_mtu = DEFAULT_MTU;
119119
uint32_t m_speed = 0; // Mbps

orchagent/portsorch.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,11 @@ sai_status_t PortsOrch::removePort(sai_object_id_t port_id)
27132713
*/
27142714
if (getPort(port_id, port))
27152715
{
2716-
setPortAdminStatus(port, false);
2716+
/* Bring port down before removing port */
2717+
if (!setPortAdminStatus(port, false))
2718+
{
2719+
SWSS_LOG_ERROR("Failed to set admin status to DOWN to remove port %" PRIx64, port_id);
2720+
}
27172721
}
27182722
/* else : port is in default state or not yet created */
27192723

@@ -4457,7 +4461,7 @@ void PortsOrch::doTask()
44574461
APP_LAG_TABLE_NAME,
44584462
APP_LAG_MEMBER_TABLE_NAME,
44594463
APP_VLAN_TABLE_NAME,
4460-
APP_VLAN_MEMBER_TABLE_NAME,
4464+
APP_VLAN_MEMBER_TABLE_NAME
44614465
};
44624466

44634467
for (auto tableName: tableOrder)

portsyncd/portsyncd.cpp

+31-21
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,33 @@ void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, boo
4646

4747
int main(int argc, char **argv)
4848
{
49-
Logger::linkToDbNative("portsyncd");
50-
int opt;
51-
52-
while ((opt = getopt(argc, argv, "v:h")) != -1 )
49+
try
5350
{
54-
switch (opt)
51+
Logger::linkToDbNative("portsyncd");
52+
int opt;
53+
54+
while ((opt = getopt(argc, argv, "v:h")) != -1 )
5555
{
56-
case 'h':
57-
usage();
58-
return 1;
59-
default: /* '?' */
60-
usage();
61-
return EXIT_FAILURE;
56+
switch (opt)
57+
{
58+
case 'h':
59+
usage();
60+
return 1;
61+
default: /* '?' */
62+
usage();
63+
return EXIT_FAILURE;
64+
}
6265
}
63-
}
6466

65-
DBConnector cfgDb("CONFIG_DB", 0);
66-
DBConnector appl_db("APPL_DB", 0);
67-
DBConnector state_db("STATE_DB", 0);
68-
ProducerStateTable p(&appl_db, APP_PORT_TABLE_NAME);
67+
DBConnector cfgDb("CONFIG_DB", 0);
68+
DBConnector appl_db("APPL_DB", 0);
69+
DBConnector state_db("STATE_DB", 0);
70+
ProducerStateTable p(&appl_db, APP_PORT_TABLE_NAME);
6971

70-
WarmStart::initialize("portsyncd", "swss");
71-
WarmStart::checkWarmStart("portsyncd", "swss");
72-
const bool warm = WarmStart::isWarmStart();
72+
WarmStart::initialize("portsyncd", "swss");
73+
WarmStart::checkWarmStart("portsyncd", "swss");
74+
const bool warm = WarmStart::isWarmStart();
7375

74-
try
75-
{
7676
NetLink netlink;
7777
Select s;
7878

@@ -136,6 +136,16 @@ int main(int argc, char **argv)
136136
}
137137
}
138138
}
139+
catch (const swss::RedisError& e)
140+
{
141+
cerr << "Exception \"" << e.what() << "\" was thrown in daemon" << endl;
142+
return EXIT_FAILURE;
143+
}
144+
catch (const std::out_of_range& e)
145+
{
146+
cerr << "Exception \"" << e.what() << "\" was thrown in daemon" << endl;
147+
return EXIT_FAILURE;
148+
}
139149
catch (const std::exception& e)
140150
{
141151
cerr << "Exception \"" << e.what() << "\" was thrown in daemon" << endl;

0 commit comments

Comments
 (0)