Skip to content

realloc() kills source pointer when fails #3953

Closed
@d-a-v

Description

@d-a-v

On master, this sketch below would be correct if free() was not commented.
But the fact is that free() must be commented for it to work flawlessly endlessly looping at every ~34KB,
with otherwise a nice Fatal exception 3(LoadStoreErrorCause) crash in umm's check_poison().
This seems to be a bug in umm.
Discovered while trying to debug #3699 in with a too large file uploaded to ESP8266WebServer.

Can someone reproduce and confirm ?

void setup()
{
  Serial.begin(115200);
  Serial.setDebugOutput(true);
}

#define INC 100
size_t len = 0;
char* buf = NULL;

void loop()
{
  if (!buf)
  {
    os_printf(":1\n");
    buf = (char*)malloc(INC);
    if (buf)
      len = INC;
    else
      os_printf(":1null\n");
  }
  else
  {
    os_printf(":re %d -> %d\n", len, len + INC);
    char* newbuf = (char*)realloc(buf, len + INC);
    if (!newbuf)
    {
      os_printf(":re null\n");
      //free(buf);   <--- BUF IS NO MORE ALLOCATED WHERE IT SHOULD BE
      len = 0;
      buf = NULL;
    }
    else
    {
      len += INC;
      buf = newbuf;
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions