pw_containers: Make list iterators safe against deletion

Before this change, if an element is deleted while iterating
over the list, the iteration would never terminate because
the current item's next pointer now points to itself.

This patch adds a shadow copy of the current item's next
pointer to the iterator and uses that when incrementing instead
of the current item's next pointer.

After this change, the following code safe:
    for (auto &item : list) {
      list.remote(item);
    }

Change-Id: I1cd7c051df56e3a8e19a86533c24b1e9f9531c61
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/81447
Reviewed-by: Keir Mierle <keir@google.com>
Commit-Queue: Erik Gilling <konkers@google.com>
4 files changed
tree: f8daf728b53d4ea5d6c809886d0d8bae311ec239
  1. build_overrides/
  2. docker/
  3. docs/
  4. pw_allocator/
  5. pw_analog/
  6. pw_android_toolchain/
  7. pw_arduino_build/
  8. pw_assert/
  9. pw_assert_basic/
  10. pw_assert_log/
  11. pw_assert_tokenized/
  12. pw_assert_zephyr/
  13. pw_base64/
  14. pw_bloat/
  15. pw_blob_store/
  16. pw_bluetooth_hci/
  17. pw_boot/
  18. pw_boot_cortex_m/
  19. pw_build/
  20. pw_build_info/
  21. pw_build_mcuxpresso/
  22. pw_bytes/
  23. pw_checksum/
  24. pw_chrono/
  25. pw_chrono_embos/
  26. pw_chrono_freertos/
  27. pw_chrono_stl/
  28. pw_chrono_threadx/
  29. pw_chrono_zephyr/
  30. pw_cli/
  31. pw_console/
  32. pw_containers/
  33. pw_cpu_exception/
  34. pw_cpu_exception_cortex_m/
  35. pw_crypto/
  36. pw_docgen/
  37. pw_doctor/
  38. pw_env_setup/
  39. pw_file/
  40. pw_function/
  41. pw_fuzzer/
  42. pw_hdlc/
  43. pw_hex_dump/
  44. pw_i2c/
  45. pw_interrupt/
  46. pw_interrupt_cortex_m/
  47. pw_interrupt_zephyr/
  48. pw_kvs/
  49. pw_libc/
  50. pw_log/
  51. pw_log_basic/
  52. pw_log_null/
  53. pw_log_rpc/
  54. pw_log_tokenized/
  55. pw_log_zephyr/
  56. pw_malloc/
  57. pw_malloc_freelist/
  58. pw_metric/
  59. pw_minimal_cpp_stdlib/
  60. pw_module/
  61. pw_multisink/
  62. pw_package/
  63. pw_persistent_ram/
  64. pw_polyfill/
  65. pw_preprocessor/
  66. pw_presubmit/
  67. pw_protobuf/
  68. pw_protobuf_compiler/
  69. pw_random/
  70. pw_result/
  71. pw_ring_buffer/
  72. pw_router/
  73. pw_rpc/
  74. pw_snapshot/
  75. pw_software_update/
  76. pw_span/
  77. pw_spi/
  78. pw_status/
  79. pw_stm32cube_build/
  80. pw_stream/
  81. pw_string/
  82. pw_symbolizer/
  83. pw_sync/
  84. pw_sync_baremetal/
  85. pw_sync_embos/
  86. pw_sync_freertos/
  87. pw_sync_stl/
  88. pw_sync_threadx/
  89. pw_sync_zephyr/
  90. pw_sys_io/
  91. pw_sys_io_arduino/
  92. pw_sys_io_baremetal_lm3s6965evb/
  93. pw_sys_io_baremetal_stm32f429/
  94. pw_sys_io_mcuxpresso/
  95. pw_sys_io_stdio/
  96. pw_sys_io_stm32cube/
  97. pw_sys_io_zephyr/
  98. pw_system/
  99. pw_target_runner/
  100. pw_thread/
  101. pw_thread_embos/
  102. pw_thread_freertos/
  103. pw_thread_stl/
  104. pw_thread_threadx/
  105. pw_tls_client/
  106. pw_tls_client_boringssl/
  107. pw_tls_client_mbedtls/
  108. pw_tokenizer/
  109. pw_tool/
  110. pw_toolchain/
  111. pw_trace/
  112. pw_trace_tokenized/
  113. pw_transfer/
  114. pw_unit_test/
  115. pw_varint/
  116. pw_watch/
  117. pw_web_ui/
  118. pw_work_queue/
  119. targets/
  120. third_party/
  121. zephyr/
  122. .bazelignore
  123. .bazelrc
  124. .clang-format
  125. .clang-tidy
  126. .eslintrc.json
  127. .gitattributes
  128. .gitignore
  129. .gn
  130. .prettierrc.js
  131. .pylintrc
  132. activate.bat
  133. Android.bp
  134. AUTHORS
  135. bootstrap.bat
  136. bootstrap.sh
  137. BUILD.bazel
  138. BUILD.gn
  139. BUILDCONFIG.gn
  140. CMakeLists.txt
  141. Kconfig.zephyr
  142. LICENSE
  143. modules.gni
  144. OWNERS
  145. package.json
  146. PW_PLUGINS
  147. README.md
  148. tsconfig.json
  149. WORKSPACE
  150. yarn.lock
README.md

Pigweed

Pigweed is an open source collection of embedded-targeted libraries–or as we like to call them, modules. These modules are building blocks and infrastructure that enable faster and more reliable development on small-footprint MMU-less 32-bit microcontrollers like the STMicroelectronics STM32L452 or the Nordic nRF52832.

For more information please see our website: https://pigweed.dev/

Links