     {
       if(newBuf.length() > 0)
       {
-        free(buf);
-        buf = (uint8_t *)malloc(newBuf.length());
+        if(newBuf.length() > *bufLen)
+        {
+          free(buf);
+          buf = (uint8_t *)malloc(newBuf.length());
+        }
         memcpy(buf,newBuf.c_str(),newBuf.length());
       }
       *bufLen = newBuf.length();

    }
    f.close();
  }
  File f = SPIFFS.open(filename, "r");
  int len = f.size();
  if(!cache)
  {
    headerOut(0,len,chk8);
    serial.flush(); // stupid important because otherwise apps that go xoff miss the header info
  }
  bool flowControl=!cache;
  BinType streamType = cache?BTYPE_NORMAL:binType;
  int bct=0;

    if(serial.drainForXonXoff()==3)
    {
      serial.setXON(true);
      free(buf);
      f.close();
      return ZOK;
    }
