Skip to content

Commit 82fd17a

Browse files
authored
Fix cppcheck lint findings in espressif executor_runner (#19997)
1 parent 118bc45 commit 82fd17a

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

examples/espressif/executor_runner/esp_executor_runner.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ using torch::executor::etdump_result;
181181
* EXT_RAM_BSS_ATTR places the buffer in PSRAM .bss section.
182182
*/
183183
#if defined(CONFIG_SPIRAM) && defined(ESP_PLATFORM)
184+
#include <esp_attr.h> // EXT_RAM_BSS_ATTR
184185
#include <esp_heap_caps.h>
186+
#ifndef EXT_RAM_BSS_ATTR
187+
// Fallback for static analysis where ESP-IDF headers are unavailable.
188+
#define EXT_RAM_BSS_ATTR
189+
#endif
185190
// Use PSRAM for large allocations
186191
static const size_t method_allocation_pool_size =
187192
ET_ESP_METHOD_ALLOCATOR_POOL_SIZE;
@@ -277,7 +282,7 @@ class Box {
277282
}
278283

279284
private:
280-
alignas(T) uint8_t mem[sizeof(T)];
285+
alignas(T) uint8_t mem[sizeof(T)] = {};
281286
bool has_value = false;
282287

283288
T* ptr() {
@@ -290,7 +295,7 @@ class Box {
290295
};
291296

292297
template <typename ValueType>
293-
void fill_tensor_with_default_value(Tensor& tensor) {
298+
[[maybe_unused]] void fill_tensor_with_default_value(Tensor& tensor) {
294299
ValueType fill_value{};
295300
if constexpr (std::is_same_v<ValueType, bool>) {
296301
fill_value = true;
@@ -482,7 +487,7 @@ struct RunnerContext {
482487
#if defined(ET_EVENT_TRACER_ENABLED)
483488
Box<ETDumpGen> etdump_gen;
484489
#if defined(ET_DUMP_INTERMEDIATE_OUTPUTS) || defined(ET_DUMP_OUTPUTS)
485-
void* debug_buffer;
490+
void* debug_buffer = nullptr;
486491
#endif
487492
#endif
488493
};
@@ -605,7 +610,7 @@ void runner_init(RunnerContext& ctx, size_t pte_size) {
605610
ctx.debug_buffer = ctx.method_allocator->allocate(ET_DEBUG_BUFFER_SIZE, 16);
606611
if (ctx.debug_buffer != nullptr) {
607612
Span<uint8_t> debug_buffer_span(
608-
(uint8_t*)ctx.debug_buffer, ET_DEBUG_BUFFER_SIZE);
613+
reinterpret_cast<uint8_t*>(ctx.debug_buffer), ET_DEBUG_BUFFER_SIZE);
609614

610615
Result<bool> result =
611616
ctx.etdump_gen.value().set_debug_buffer(debug_buffer_span);
@@ -859,6 +864,7 @@ void print_outputs(RunnerContext& ctx) {
859864
}
860865
}
861866

867+
// cppcheck-suppress constParameterReference
862868
void write_etdump(RunnerContext& ctx) {
863869
#if defined(ET_EVENT_TRACER_ENABLED)
864870
ETDumpResult result = ctx.etdump_gen->get_etdump_data();
@@ -876,7 +882,8 @@ void write_etdump(RunnerContext& ctx) {
876882
ET_LOG(Info, "Writing etdump to file: %s", etdump_filename);
877883
FILE* f = fopen(etdump_filename, "wb");
878884
if (f) {
879-
size_t bytes_written = fwrite((uint8_t*)result.buf, 1, result.size, f);
885+
size_t bytes_written =
886+
fwrite(reinterpret_cast<uint8_t*>(result.buf), 1, result.size, f);
880887
if (bytes_written != result.size) {
881888
ET_LOG(
882889
Error,
@@ -894,6 +901,9 @@ void write_etdump(RunnerContext& ctx) {
894901
#endif
895902
}
896903

904+
// cppcheck-suppress constParameterReference
905+
// ET_BUNDLE_IO verification passes ctx.method into devtools/bundled_program
906+
// helpers, which currently require a non-const Method&.
897907
bool verify_result(RunnerContext& ctx, const void* model_pte) {
898908
bool model_ok = false;
899909
#if defined(ET_BUNDLE_IO)
@@ -1213,7 +1223,7 @@ size_t et_runner_outputs_size(void) {
12131223
* On ESP-IDF, this is called from app_main() (see below).
12141224
* The function can also be compiled for host testing without ESP-IDF.
12151225
*/
1216-
void executor_runner_main(void) {
1226+
[[maybe_unused]] void executor_runner_main(void) {
12171227
if (!et_runner_init()) {
12181228
return;
12191229
}

examples/espressif/executor_runner/esp_memory_allocator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void* EspMemoryAllocator::allocate(size_t size, size_t alignment) {
1616
// Keep used_ in sync with the underlying MemoryAllocator by computing it
1717
// from the returned pointer and requested size, which implicitly includes
1818
// any padding/alignment the base allocator applied.
19-
uint8_t* end_ptr = static_cast<uint8_t*>(ret) + size;
19+
const uint8_t* end_ptr = static_cast<uint8_t*>(ret) + size;
2020
used_ = static_cast<size_t>(end_ptr - base_address());
2121
}
2222
return ret;

examples/espressif/executor_runner/esp_pal.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ ET_NORETURN void et_pal_abort(void) {
4141
#else
4242
abort();
4343
#endif
44-
while (1) {
45-
}
4644
}
4745

4846
et_timestamp_t et_pal_current_ticks(void) {
@@ -90,6 +88,7 @@ void* et_pal_allocate(ET_UNUSED size_t size) {
9088
return nullptr;
9189
}
9290

91+
// cppcheck-suppress constParameterPointer
9392
void et_pal_free(ET_UNUSED void* ptr) {}
9493

95-
} // extern "C"
94+
} // extern "C"

0 commit comments

Comments
 (0)