Skip to content

Commit

Permalink
Add helper function to validate interface name length (#931)
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Blyschak <stepanb@nvidia.com>
Co-authored-by: afeigin <afeigin@nvidia.com>
  • Loading branch information
stepanblyschak and arfeigin authored Nov 9, 2024
1 parent dc75f23 commit b686bb0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ common_libswsscommon_la_SOURCES = \
common/zmqserver.cpp \
common/asyncdbupdater.cpp \
common/redis_table_waiter.cpp \
common/interface.h \
common/c-api/util.cpp \
common/c-api/dbconnector.cpp \
common/c-api/consumerstatetable.cpp \
Expand Down
19 changes: 19 additions & 0 deletions common/interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __INTERFACE__
#define __INTERFACE__

#include <string>
#include <net/if.h>

namespace swss
{

const size_t IFACE_NAME_MAX_LEN = IFNAMSIZ - 1;

bool isInterfaceNameValid(const std::string &ifaceName)
{
return !ifaceName.empty() && (ifaceName.length() < IFNAMSIZ);
}

}

#endif
4 changes: 3 additions & 1 deletion pyext/swsscommon.i
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "zmqproducerstatetable.h"
#include <memory>
#include <functional>
#include "interface.h"
%}

%include <std_string.i>
Expand Down Expand Up @@ -282,6 +283,7 @@ T castSelectableObj(swss::Selectable *temp)
%include "zmqserver.h"
%include "zmqclient.h"
%include "zmqconsumerstatetable.h"
%include "interface.h"

%extend swss::DBConnector {
%template(hgetall) hgetall<std::map<std::string, std::string>>;
Expand All @@ -296,7 +298,7 @@ T castSelectableObj(swss::Selectable *temp)
%include "table.h"
#ifdef ENABLE_YANG_MODULES
%include "decoratortable.h"
#endif
#endif
%clear std::vector<std::string> &keys;
%clear std::vector<std::string> &ops;
%clear std::vector<std::vector<std::pair<std::string, std::string>>> &fvss;
Expand Down
8 changes: 8 additions & 0 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from swsscommon import swsscommon

def test_is_interface_name_valid():
invalid_interface_name = "TooLongInterfaceName"
assert not swsscommon.isInterfaceNameValid(invalid_interface_name)

validInterfaceName = "OkInterfaceName"
assert swsscommon.isInterfaceNameValid(validInterfaceName)

0 comments on commit b686bb0

Please sign in to comment.