diff --git a/src/core/thread/network_data_leader.cpp b/src/core/thread/network_data_leader.cpp index 862dbc80b..8da052127 100644 --- a/src/core/thread/network_data_leader.cpp +++ b/src/core/thread/network_data_leader.cpp @@ -902,32 +902,24 @@ ThreadError Leader::AddBorderRouter(PrefixTlv &aPrefix, BorderRouterTlv &aBorder dstPrefix->Init(aPrefix.GetDomainId(), aPrefix.GetPrefixLength(), aPrefix.GetPrefix()); } - if (aBorderRouter.IsStable()) + if ((dstContext = FindContext(*dstPrefix)) != NULL) { - dstPrefix->SetStable(); - - if ((dstContext = FindContext(*dstPrefix)) != NULL) - { - dstContext->SetCompress(); - } - else if ((contextId = AllocateContext()) >= 0) - { - dstContext = reinterpret_cast(dstPrefix->GetNext()); - Insert(reinterpret_cast(dstContext), sizeof(ContextTlv)); - dstPrefix->SetLength(dstPrefix->GetLength() + sizeof(ContextTlv)); - dstContext->Init(); - dstContext->SetStable(); - dstContext->SetCompress(); - dstContext->SetContextId(static_cast(contextId)); - dstContext->SetContextLength(aPrefix.GetPrefixLength()); - } - else - { - ExitNow(error = kThreadError_NoBufs); - } - - mContextLastUsed[dstContext->GetContextId() - kMinContextId] = 0; + dstContext->SetCompress(); } + else if ((contextId = AllocateContext()) >= 0) + { + dstContext = reinterpret_cast(dstPrefix->GetNext()); + Insert(reinterpret_cast(dstContext), sizeof(ContextTlv)); + dstPrefix->SetLength(dstPrefix->GetLength() + sizeof(ContextTlv)); + dstContext->Init(); + dstContext->SetCompress(); + dstContext->SetContextId(static_cast(contextId)); + dstContext->SetContextLength(aPrefix.GetPrefixLength()); + } + + VerifyOrExit(dstContext != NULL, error = kThreadError_NoBufs); + mContextLastUsed[dstContext->GetContextId() - kMinContextId] = 0; + if ((dstBorderRouter = FindBorderRouter(*dstPrefix, aBorderRouter.IsStable())) == NULL) { @@ -935,11 +927,6 @@ ThreadError Leader::AddBorderRouter(PrefixTlv &aPrefix, BorderRouterTlv &aBorder Insert(reinterpret_cast(dstBorderRouter), sizeof(BorderRouterTlv)); dstPrefix->SetLength(dstPrefix->GetLength() + sizeof(BorderRouterTlv)); dstBorderRouter->Init(); - - if (aBorderRouter.IsStable()) - { - dstBorderRouter->SetStable(); - } } Insert(reinterpret_cast(dstBorderRouter->GetNext()), sizeof(BorderRouterEntry)); @@ -948,6 +935,13 @@ ThreadError Leader::AddBorderRouter(PrefixTlv &aPrefix, BorderRouterTlv &aBorder memcpy(dstBorderRouter->GetEntry(dstBorderRouter->GetNumEntries() - 1), aBorderRouter.GetEntry(0), sizeof(BorderRouterEntry)); + if (aBorderRouter.IsStable()) + { + dstPrefix->SetStable(); + dstContext->SetStable(); + dstBorderRouter->SetStable(); + } + exit: return error; }