Skip to content

Commit

Permalink
Fix exception thrown by boost in MaskToPrefix
Browse files Browse the repository at this point in the history
Boost Ip4Address() takes unsigned long as argument. If the address
passed is greater than 0xFFFFFFFF, it throws exception. Method
MaskToPrefix has potential to pass values greater than 0xFFFFFFFF.

Renamed MaskToPrefix to PrefixToIpAddress and moved to base/util.h.
The new method uses uint32_t for mask. This ensures the value is
never greater than 0xFFFFFFFF

Change-Id: I64a69c59e7902142e68e09ecef8964a3d79933f7
Closes-Bug: #1390594
  • Loading branch information
Praveen K V committed Nov 8, 2014
1 parent ee5d2c6 commit 3cd4beb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/base/task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ tbb::task *TaskImpl::execute() {
// information from the core.
static std::string what = e.what();

LOG(DEBUG, "!!!! ERROR !!!! Task caught fatal exception: " << what);
LOG(DEBUG, "!!!! ERROR !!!! Task caught fatal exception: " << what
<< " TaskImpl: " << this);
assert(0);
} catch (...) {
LOG(DEBUG, "!!!! ERROR !!!! Task caught fatal unknown exception");
LOG(DEBUG, "!!!! ERROR !!!! Task caught fatal unknown exception"
<< " TaskImpl: " << this);
assert(0);
}

Expand Down
11 changes: 11 additions & 0 deletions src/base/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ static inline bool ValidateIPAddressString(std::string ip_address_str,
return true;
}

static inline IpAddress PrefixToIpNetmask(uint32_t prefix_len) {
uint32_t mask;

if (prefix_len == 0) {
mask = 0;
} else {
mask = (~((1 << (32 - prefix_len)) - 1));
}
return IpAddress(Ip4Address(mask));
}

static inline uint32_t NetmaskToPrefix(uint32_t netmask) {
uint32_t count = 0;

Expand Down
11 changes: 2 additions & 9 deletions src/vnsw/agent/filter/acl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,6 @@ DBTableBase *AclTable::CreateTable(DB *db, const std::string &name) {
return acl_table_;
}

static inline IpAddress MaskToPrefix(int prefix_len) {
if (prefix_len == 0 )
return (IpAddress(Ip4Address(~((unsigned int) -1))));
else
return (IpAddress(Ip4Address(~((1 << (32 - prefix_len)) - 1))));
}

static void AclEntryObjectTrace(AclEntrySandeshData &ace_sandesh, AclEntrySpec &ace_spec)
{
ace_sandesh.set_ace_id(integerToString(ace_spec.id));
Expand Down Expand Up @@ -738,7 +731,7 @@ bool AclEntrySpec::Populate(const MatchConditionType *match_condition) {
return false;
}
src_ip_plen = st.ip_prefix_len;
src_ip_mask = MaskToPrefix(st.ip_prefix_len);
src_ip_mask = PrefixToIpNetmask(st.ip_prefix_len);
src_addr_type = AddressMatch::IP_ADDR;
} else if (match_condition->src_address.virtual_network.size()) {
std::string nt;
Expand Down Expand Up @@ -766,7 +759,7 @@ bool AclEntrySpec::Populate(const MatchConditionType *match_condition) {
return false;
}
dst_ip_plen = st.ip_prefix_len;
dst_ip_mask = MaskToPrefix(st.ip_prefix_len);
dst_ip_mask = PrefixToIpNetmask(st.ip_prefix_len);
dst_addr_type = AddressMatch::IP_ADDR;
} else if (match_condition->dst_address.virtual_network.size()) {
std::string nt;
Expand Down

0 comments on commit 3cd4beb

Please sign in to comment.