Updater::begin picks the 4 KB buffer by total free heap, and does not fall back to 256 B when the allocation fails

未关闭 适合新手
#9,342 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp
领域
embedded-iot

调研方向

阅读 cores/esp8266/Updater.cpp 中 UpdaterClass::begin() 和 _writeBuffer() 附近的代码,然后检查 Updater.h 中的流模板,以了解缓冲区大小的用法。验证 4 KB 分配失败时可以选择较小的缓冲区,并且使用 256 字节写入时扇区擦除仍能正常工作。完成标准是:在堆碎片化时,begin() 可以回退到较小的缓冲区,且不改变更新行为。

由索引模型根据 Issue 内容生成。

描述

What happens

UpdaterClass::begin() chooses its write buffer size from the total free heap:

// cores/esp8266/Updater.cpp, 3.1.2 lines 172-181 (same on master, lines 174-183)
if (ESP.getFreeHeap() > 2 * FLASH_SECTOR_SIZE) {
  _bufferSize = FLASH_SECTOR_SIZE;
} else {
  _bufferSize = 256;
}
_buffer = new (std::nothrow) uint8_t[_bufferSize];
if (!_buffer) {
  _setError(UPDATE_ERROR_OOM);
  _reset(false);
  return false;
}

Total free heap says nothing about the largest free block. On a fragmented heap the check can pass while a 4096 byte block does not exist. Then new fails and begin() returns false with UPDATE_ERROR_OOM. The 256 byte buffer is never tried.

What I measured
  • Core 3.1.2, ESP8266 (GeekMagic SmallTV-Ultra).
  • ESP.getFreeHeap() = 19.6 KB.
  • ESP.getMaxFreeBlockSize() = 3,888 bytes.
What I did not measure

I did not run the upload in this state. I restarted the device first. That the upload would be refused with UPDATE_ERROR_OOM comes from reading the code above. 19.6 KB is more than 8192, so the 4 KB branch is taken. A 4096 byte allocation cannot fit in a 3,888 byte block.

Why a smaller buffer should be safe

_bufferSize is private and is only used inside Updater.cpp and the Updater.h stream template. The 256 byte case already exists for low free heap (commit 01e1c586, "Updater: if low on heap memory, use smaller write buffer"). _writeBuffer() erases a sector when _currentAddress % FLASH_SECTOR_SIZE == 0, which still works with 256 byte writes.

Proposed small change (sketch, untested)
-  if (ESP.getFreeHeap() > 2 * FLASH_SECTOR_SIZE) {
+  if (ESP.getMaxFreeBlockSize() >= 2 * FLASH_SECTOR_SIZE) {
     _bufferSize = FLASH_SECTOR_SIZE;
   } else {
     _bufferSize = 256;
   }
   _buffer = new (std::nothrow) uint8_t[_bufferSize];
+  if (!_buffer && _bufferSize > 256) {
+    _bufferSize = 256;
+    _buffer = new (std::nothrow) uint8_t[_bufferSize];
+  }
   if (!_buffer) {

The first part picks by largest block. The second part is a safety net if the allocation still fails. Either part alone would already fix the case above. I can prepare a PR if this direction is fine.

主要语言
C++
星标
16.7k
派生
13k
平均合并
1 天 2 小时
30 天内合并 PR
7

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

esp8266/Arduino 的其他 Issue

查看 esp8266/Arduino 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。