adafruit / Adafruit_TinyUSB_Arduino

Arduino library for TinyUSB
MIT License
476 stars 126 forks source link

nRF52 crashes with longer strings in .print() #57

Open zjwhitehead opened 4 years ago

zjwhitehead commented 4 years ago

Describe the bug Passing in strings over a certain length freezes the device. May be related to buffer overflow? looks like it crashes after 64 characters.

Set up (please complete the following information)

To Reproduce Steps to reproduce the behavior:

  1. Open webusb_serial example here
  2. change line 79 from if ( connected ) usb_web.println("TinyUSB WebUSB Serial example"); to if ( connected ) usb_web.println("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
  3. Compiles & upload as expected
  4. Connect via https://adafruit.github.io/Adafruit_TinyUSB_Arduino/examples/webusb-serial/
  5. Notice expected string gets only partially returned and devices is unresponsive.

Expected behavior Full string is printed out via web_usb without crashing

Screenshots If applicable, add screenshots to help explain your problem. image

Additional context Seems to work as expected on SAMD21 boards

Tested in Chrome 83.0.4103.14 and Brave browser (Chromium)

zjwhitehead commented 2 years ago

Looks like this is still happening. Im also experiencing the issue on the RP2040.

The following code hangs when connected to the webserial example. The main fix is just sending less than 64 bytes but obviously sometimes you need to send more than that... Changing 200 to 64 below changes how many bytes are sent.

#include "Adafruit_TinyUSB.h"

// USB WebUSB object
Adafruit_USBD_WebUSB usb_web;

// Landing Page: scheme (0: http, 1: https), url
WEBUSB_URL_DEF(landingPage, 1 /*https*/, "adafruit.github.io/Adafruit_TinyUSB_Arduino/examples/webusb-serial/index.html");

int led_pin = LED_BUILTIN;

// the setup function runs once when you press reset or power the board
void setup()
{
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
  // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
  TinyUSB_Device_Init(0);
#endif

  usb_web.setLandingPage(&landingPage);
  usb_web.setLineStateCallback(line_state_callback);
  //usb_web.setStringDescriptor("TinyUSB WebUSB");
  usb_web.begin();

  Serial.begin(115200);

  // wait until device mounted
  while( !TinyUSBDevice.mounted() ) delay(1);

  Serial.println("TinyUSB WebUSB Serial example");
}

void loop(){}

void line_state_callback(bool connected){
  if ( connected ) {
    for (int i = 0; i < 200; i++) {
    int digit = i % 10;
    Serial.print(digit);
    usb_web.print(digit);
    usb_web.flush();
    }
  }
}

Any thoughts on how to overcome this?

zjwhitehead commented 1 year ago

This is still happening on 1.18.3 😢 Any updated on how to fix this?