Adafruit_TinyUSB_Arduino icon indicating copy to clipboard operation
Adafruit_TinyUSB_Arduino copied to clipboard

nRF52 crashes with longer strings in .print()

Open zjwhitehead opened this issue 5 years ago • 2 comments

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)

  • nRF52840 Feather Express
  • TinyUSB library version: 0.8.2
  • MacOS 10.15.4
  • Chrome 83

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 avatar Apr 22 '20 15:04 zjwhitehead

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 avatar Aug 07 '22 22:08 zjwhitehead

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

zjwhitehead avatar Mar 04 '23 22:03 zjwhitehead