Skip to content

Commit 07f2e81

Browse files
committed
chore: non-mutating increment; use max depth constant
1 parent cad5c7b commit 07f2e81

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

google/cloud/bigtable/value.cc

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ namespace bigtable {
3030
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3131
namespace {
3232

33+
auto constexpr kMaxValueDepth = 10;
34+
3335
// Some Bigtable proto fields use Cord internally and string externally.
3436
template <typename T, typename std::enable_if<
3537
std::is_same<T, std::string>::value>::type* = nullptr>
@@ -339,17 +341,21 @@ std::ostream& operator<<(std::ostream& os, Value const& v) {
339341
return StreamHelper(os, v.value_, v.type_, StreamMode::kScalar);
340342
}
341343

342-
Status MakeDepthExceededError() {
343-
return internal::InternalError("Nested value depth exceeds 10 levels",
344-
GCP_ERROR_INFO());
344+
Status CheckDepthExceeded(int const depth) {
345+
if (depth > kMaxValueDepth) {
346+
return internal::InternalError("Nested value depth exceeds 10 levels",
347+
GCP_ERROR_INFO());
348+
}
349+
return Status{};
345350
}
346351

347352
// NOLINTNEXTLINE(misc-no-recursion)
348353
Status TypeAndArrayValuesMatch(google::bigtable::v2::Type const& type,
349354
google::bigtable::v2::Value const& value,
350-
int depth) {
351-
if (depth > 10) {
352-
return MakeDepthExceededError();
355+
int const depth) {
356+
auto depth_exceeded = CheckDepthExceeded(depth);
357+
if (!depth_exceeded.ok()) {
358+
return depth_exceeded;
353359
}
354360
if (!value.has_array_value()) {
355361
return internal::InternalError(
@@ -369,9 +375,10 @@ Status TypeAndArrayValuesMatch(google::bigtable::v2::Type const& type,
369375
// NOLINTNEXTLINE(misc-no-recursion)
370376
Status TypeAndMapValuesMatch(google::bigtable::v2::Type const& type,
371377
google::bigtable::v2::Value const& value,
372-
int depth) {
373-
if (depth > 10) {
374-
return MakeDepthExceededError();
378+
int const depth) {
379+
auto depth_exceeded = CheckDepthExceeded(depth);
380+
if (!depth_exceeded.ok()) {
381+
return depth_exceeded;
375382
}
376383
if (!value.has_array_value()) {
377384
return internal::InternalError(
@@ -405,9 +412,10 @@ Status TypeAndMapValuesMatch(google::bigtable::v2::Type const& type,
405412
// NOLINTNEXTLINE(misc-no-recursion)
406413
Status TypeAndStructValuesMatch(google::bigtable::v2::Type const& type,
407414
google::bigtable::v2::Value const& value,
408-
int depth) {
409-
if (depth > 10) {
410-
return MakeDepthExceededError();
415+
int const depth) {
416+
auto depth_exceeded = CheckDepthExceeded(depth);
417+
if (!depth_exceeded.ok()) {
418+
return depth_exceeded;
411419
}
412420
if (!value.has_array_value()) {
413421
return internal::InternalError(
@@ -440,9 +448,10 @@ Status TypeAndStructValuesMatch(google::bigtable::v2::Type const& type,
440448
// NOLINTNEXTLINE(misc-no-recursion)
441449
Status Value::TypeAndValuesMatch(google::bigtable::v2::Type const& type,
442450
google::bigtable::v2::Value const& value,
443-
int depth) {
444-
if (depth > 10) {
445-
return MakeDepthExceededError();
451+
int const depth) {
452+
auto depth_exceeded = CheckDepthExceeded(depth);
453+
if (!depth_exceeded.ok()) {
454+
return depth_exceeded;
446455
}
447456
using google::bigtable::v2::Type;
448457
auto make_mismatch_metadata_status = [&](std::string const& value_kind,
@@ -458,13 +467,13 @@ Status Value::TypeAndValuesMatch(google::bigtable::v2::Type const& type,
458467
Status result;
459468
switch (type.kind_case()) {
460469
case Type::kArrayType:
461-
result = TypeAndArrayValuesMatch(type, value, ++depth);
470+
result = TypeAndArrayValuesMatch(type, value, depth + 1);
462471
break;
463472
case Type::kMapType:
464-
result = TypeAndMapValuesMatch(type, value, ++depth);
473+
result = TypeAndMapValuesMatch(type, value, depth + 1);
465474
break;
466475
case Type::kStructType:
467-
result = TypeAndStructValuesMatch(type, value, ++depth);
476+
result = TypeAndStructValuesMatch(type, value, depth + 1);
468477
break;
469478
case Type::kBoolType:
470479
if (!value.has_bool_value()) {

0 commit comments

Comments
 (0)