Skip to content

Commit

Permalink
put minimal code under try-catch
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Ivanov <[email protected]>
  • Loading branch information
alexander-e1off committed Jan 10, 2025
1 parent 63f8d50 commit 8e894d6
Showing 1 changed file with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,44 @@ CompositeSequenceNumber::fromString(bool* success,
const bsl::string secondPart = seqNumString.substr(separatorPos + 1);

// Convert parts to numbers
size_t posFirst, posSecond;
unsigned long uLong;
bsls::Types::Uint64 seqNumber;
try {
size_t posFirst, posSecond;

const unsigned long uLong = bsl::stoul(firstPart, &posFirst);
const bsls::Types::Uint64 seqNumber = bsl::stoul(secondPart,
&posSecond);
if (posFirst != firstPart.size() ||
posSecond != secondPart.size()) {
errorDescription
<< "Invalid input: non-numeric values encountered.";
break; // BREAK
}

unsigned int leaseId = static_cast<unsigned int>(uLong);
if (uLong != leaseId) {
errorDescription << "Invalid input: number out of range.";
break; // BREAK
}

if (leaseId == 0 || seqNumber == 0) {
errorDescription << "Invalid input: zero values encountered.";
break; // BREAK
}

d_compositeSequenceNumber = bsl::make_pair(leaseId, seqNumber);

*success = true;
return *this; // RETURN
uLong = bsl::stoul(firstPart, &posFirst);
seqNumber = bsl::stoul(secondPart, &posSecond);
}
catch (const bsl::invalid_argument& e) {
errorDescription
<< "Invalid input: non-numeric values encountered.";
break; // BREAK
}
catch (const bsl::out_of_range& e) {
errorDescription << "Invalid input: number out of range.";
break; // BREAK
}

if (posFirst != firstPart.size() || posSecond != secondPart.size()) {
errorDescription
<< "Invalid input: non-numeric values encountered.";
break; // BREAK
}

unsigned int leaseId = static_cast<unsigned int>(uLong);
if (uLong != leaseId) {
errorDescription << "Invalid input: number out of range.";
break; // BREAK
}

if (leaseId == 0 || seqNumber == 0) {
errorDescription << "Invalid input: zero values encountered.";
break; // BREAK
}

d_compositeSequenceNumber = bsl::make_pair(leaseId, seqNumber);

*success = true;
return *this; // RETURN
} while (false);

*success = false;
Expand Down

0 comments on commit 8e894d6

Please sign in to comment.