Fix GCC 12.1.1 LTO -Walloc-size-larger-than= warnings
With -flto=auto, GCC emits multiple warnings in release builds such as
In function ‘make_unique’,
inlined from ‘Create’ at ceres-solver/internal/ceres/scratch_evaluate_preparer.cc:43:75,
inlined from ‘CreateEvaluatePreparers’ at ceres-solver/internal/ceres/compressed_row_jacobian_writer.h:95:66,
inlined from ‘__ct ’ at ceres-solver/internal/ceres/program_evaluator.h:120:9,
inlined from ‘Evaluate.constprop.isra’ at ceres-solver/internal/ceres/problem_impl.cc:695:65:
/usr/include/c++/12.1.1/bits/unique_ptr.h:1080:30: warning: argument 1 value ‘18446744073709551615’ exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
1080 | { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
| ^
because a signed integer is used to specify the size of allocated arrays
instead of the expected unsigned (specifically, std::size_t) without
checking for negative values at the call site.
Change-Id: I923b1d074241535426bfea041568ef1dc7f3ec86
diff --git a/internal/ceres/block_jacobian_writer.cc b/internal/ceres/block_jacobian_writer.cc
index 2b1742a..727d649 100644
--- a/internal/ceres/block_jacobian_writer.cc
+++ b/internal/ceres/block_jacobian_writer.cc
@@ -138,12 +138,12 @@
// Create evaluate prepareres that point directly into the final jacobian. This
// makes the final Write() a nop.
std::unique_ptr<BlockEvaluatePreparer[]>
-BlockJacobianWriter::CreateEvaluatePreparers(int num_threads) {
+BlockJacobianWriter::CreateEvaluatePreparers(unsigned num_threads) {
int max_derivatives_per_residual_block =
program_->MaxDerivativesPerResidualBlock();
auto preparers = std::make_unique<BlockEvaluatePreparer[]>(num_threads);
- for (int i = 0; i < num_threads; i++) {
+ for (unsigned i = 0; i < num_threads; i++) {
preparers[i].Init(&jacobian_layout_[0], max_derivatives_per_residual_block);
}
return preparers;
diff --git a/internal/ceres/block_jacobian_writer.h b/internal/ceres/block_jacobian_writer.h
index 00dcb36..7f5c50b 100644
--- a/internal/ceres/block_jacobian_writer.h
+++ b/internal/ceres/block_jacobian_writer.h
@@ -60,7 +60,7 @@
// Create evaluate prepareres that point directly into the final jacobian.
// This makes the final Write() a nop.
std::unique_ptr<BlockEvaluatePreparer[]> CreateEvaluatePreparers(
- int num_threads);
+ unsigned num_threads);
std::unique_ptr<SparseMatrix> CreateJacobian() const;
diff --git a/internal/ceres/program_evaluator.h b/internal/ceres/program_evaluator.h
index 826a73a..1176bb8 100644
--- a/internal/ceres/program_evaluator.h
+++ b/internal/ceres/program_evaluator.h
@@ -1,5 +1,5 @@
// Ceres Solver - A fast non-linear least squares minimizer
-// Copyright 2015 Google Inc. All rights reserved.
+// Copyright 2022 Google Inc. All rights reserved.
// http://ceres-solver.org/
//
// Redistribution and use in source and binary forms, with or without
@@ -129,8 +129,8 @@
#endif // CERES_NO_THREADS
BuildResidualLayout(*program, &residual_layout_);
- evaluate_scratch_ =
- std::move(CreateEvaluatorScratch(*program, options.num_threads));
+ evaluate_scratch_ = std::move(CreateEvaluatorScratch(
+ *program, static_cast<unsigned>(options.num_threads)));
}
// Implementation of Evaluator interface.
@@ -345,7 +345,7 @@
// Create scratch space for each thread evaluating the program.
static std::unique_ptr<EvaluateScratch[]> CreateEvaluatorScratch(
- const Program& program, int num_threads) {
+ const Program& program, unsigned num_threads) {
int max_parameters_per_residual_block =
program.MaxParametersPerResidualBlock();
int max_scratch_doubles_needed_for_evaluate =
diff --git a/internal/ceres/scratch_evaluate_preparer.cc b/internal/ceres/scratch_evaluate_preparer.cc
index 680dab9..f20f8fc 100644
--- a/internal/ceres/scratch_evaluate_preparer.cc
+++ b/internal/ceres/scratch_evaluate_preparer.cc
@@ -1,5 +1,5 @@
// Ceres Solver - A fast non-linear least squares minimizer
-// Copyright 2015 Google Inc. All rights reserved.
+// Copyright 2022 Google Inc. All rights reserved.
// http://ceres-solver.org/
//
// Redistribution and use in source and binary forms, with or without
@@ -39,19 +39,19 @@
namespace ceres::internal {
std::unique_ptr<ScratchEvaluatePreparer[]> ScratchEvaluatePreparer::Create(
- const Program& program, int num_threads) {
+ const Program& program, unsigned num_threads) {
auto preparers = std::make_unique<ScratchEvaluatePreparer[]>(num_threads);
int max_derivatives_per_residual_block =
program.MaxDerivativesPerResidualBlock();
- for (int i = 0; i < num_threads; i++) {
+ for (unsigned i = 0; i < num_threads; i++) {
preparers[i].Init(max_derivatives_per_residual_block);
}
return preparers;
}
void ScratchEvaluatePreparer::Init(int max_derivatives_per_residual_block) {
- jacobian_scratch_ =
- std::make_unique<double[]>(max_derivatives_per_residual_block);
+ jacobian_scratch_ = std::make_unique<double[]>(
+ static_cast<std::size_t>(max_derivatives_per_residual_block));
}
// Point the jacobian blocks into the scratch area of this evaluate preparer.
diff --git a/internal/ceres/scratch_evaluate_preparer.h b/internal/ceres/scratch_evaluate_preparer.h
index 946244b..dbd4e50 100644
--- a/internal/ceres/scratch_evaluate_preparer.h
+++ b/internal/ceres/scratch_evaluate_preparer.h
@@ -1,5 +1,5 @@
// Ceres Solver - A fast non-linear least squares minimizer
-// Copyright 2015 Google Inc. All rights reserved.
+// Copyright 2022 Google Inc. All rights reserved.
// http://ceres-solver.org/
//
// Redistribution and use in source and binary forms, with or without
@@ -50,7 +50,7 @@
public:
// Create num_threads ScratchEvaluatePreparers.
static std::unique_ptr<ScratchEvaluatePreparer[]> Create(
- const Program& program, int num_threads);
+ const Program& program, unsigned num_threads);
// EvaluatePreparer interface
void Init(int max_derivatives_per_residual_block);