1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-21 21:22:31 +03:00

Ensure that we never write an out of bounds value (_bufend) to _begin or _end, even temporarily.

Testing:
  - Boot tested, ran basic serial I/O code

Notes:
  - Before this change, there are instruction like "s32i.n <reg>, <this>, <_begin>" in the
    disassembled output, followed by an overwrite if <reg> turns out to be _bufend.
    After this change, there is only one store instruction to <_begin> per function.
This commit is contained in:
Christopher Pascoe
2015-12-07 00:23:46 -08:00
parent cc0a8ead55
commit 7133a6c1f9

View File

@ -62,7 +62,7 @@ class cbuf {
if(getSize() == 0) return -1; if(getSize() == 0) return -1;
char result = *_begin; char result = *_begin;
if(++_begin == _bufend) _begin = _buf; _begin = wrap_if_bufend(_begin + 1);
return static_cast<int>(result); return static_cast<int>(result);
} }
@ -78,8 +78,7 @@ class cbuf {
dst += top_size; dst += top_size;
} }
memcpy(dst, _begin, size_to_read); memcpy(dst, _begin, size_to_read);
_begin += size_to_read; _begin = wrap_if_bufend(_begin + size_to_read);
if(_begin == _bufend) _begin = _buf;
return size_read; return size_read;
} }
@ -87,7 +86,7 @@ class cbuf {
if(room() == 0) return 0; if(room() == 0) return 0;
*_end = c; *_end = c;
if(++_end == _bufend) _end = _buf; _end = wrap_if_bufend(_end + 1);
return 1; return 1;
} }
@ -103,8 +102,7 @@ class cbuf {
src += top_size; src += top_size;
} }
memcpy(_end, src, size_to_write); memcpy(_end, src, size_to_write);
_end += size_to_write; _end = wrap_if_bufend(_end + size_to_write);
if(_end == _bufend) _end = _buf;
return size_written; return size_written;
} }
@ -114,6 +112,10 @@ class cbuf {
} }
private: private:
inline char* wrap_if_bufend(char* ptr) {
return (ptr == _bufend) ? _buf : ptr;
}
size_t _size; size_t _size;
char* _buf; char* _buf;
char* _bufend; char* _bufend;