Commit a97d8f11 authored by Nathaniel Wesley Filardo's avatar Nathaniel Wesley Filardo Committed by Marcel Stör
Browse files

pipe: fix unread buffer chunk management

unread contained two subtle bugs:

 - it created a buffer chunk at pipe[1], when that position is reserved
   for the pipe reader function.  Because it shifted pipe[i] to
   pipe[i+1], including pipe[1], this was likely to manifest as the pipe
   later attempting to dequeue a function rather than a buffer chunk
   user datum.  Solve this by adjusting the loop bounds and creation
   index.

 - when it created a new buffer chunk, it left that chunk's ->start and
   ->end fields at 0, but would then exit the loop to do the tail fill,
   which was in turn assuming the exit condition of the loop when an
   existing chunk had room, namely that data had been moved to the right
   and so ->start > l.  Solve this by making new chunks empty but with
   ->start = ->end = LUAL_BUFFERSIZE.  It may be slightly better to
   instead try to leave room at both ends if the buffer was empty prior
   to this unread.

FIXES: https://github.com/nodemcu/nodemcu-firmware/issues/3155
parent 1b29843b
...@@ -336,12 +336,14 @@ int pipe_unread(lua_State *L) { ...@@ -336,12 +336,14 @@ int pipe_unread(lua_State *L) {
if (used == LUAL_BUFFERSIZE) { if (used == LUAL_BUFFERSIZE) {
/* If the current UD is full insert a new UD at T[2] */ /* If the current UD is full insert a new UD at T[2] */
int i, nUD = lua_objlen(L, 1); int i, nUD = lua_objlen(L, 1);
for (i = nUD; i > 0; i--) { /* for i = nUD-1,1,-1 */ for (i = nUD; i > 1; i--) { /* for i = nUD,1,-1 */
lua_rawgeti(L, 1, i); lua_rawseti(L, 1, i+1); /* T[i+1] = T[i] */ lua_rawgeti(L, 1, i); lua_rawseti(L, 1, i+1); /* T[i+1] = T[i] */
} }
ud = newPipeUD(L, 1, 1); ud = newPipeUD(L, 1, 2);
used = 0; lrem = LUAL_BUFFERSIZE; used = 0; lrem = LUAL_BUFFERSIZE;
/* Filling leftwards; make this chunk "empty but at the right end" */
ud->start = ud->end = LUAL_BUFFERSIZE;
} else if (ud->start < l) { } else if (ud->start < l) {
/* If the unread can't fit it before the start then shift content to end */ /* If the unread can't fit it before the start then shift content to end */
memmove(ud->buf + lrem, memmove(ud->buf + lrem,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment