Skip to content

SPI.end() does not check for NULL => crashes NANO33 #590

Closed
@RobTillaart

Description

@RobTillaart

First mentioned here RobTillaart/MS5611_SPI#6

Summary

It should be safe to call SPI.end() before SPI.begin() to allow dynamic change SPI settings.
Now it crashes on the NANO33

Solution

Current code

From - https://github.com/arduino/ArduinoCore-mbed/blob/master/libraries/SPI/SPI.cpp

void arduino::MbedSPI::begin() {
    if (dev == NULL) {
      dev = new mbed_spi;
      dev->obj = NULL;
    }
    if (dev->obj == NULL) {
      dev->obj = new mbed::SPI(_mosi, _miso, _sck);
    }
}

void arduino::MbedSPI::end() {
    if (dev->obj != NULL) {                      //   <<<<<<<<<<  if dev == NULL this crashes.
        delete dev->obj;
    }
}

Proposal

void arduino::MbedSPI::end() {
    if (dev != NULL) {
        if (dev->obj != NULL) {
            delete dev->obj;
        }
    }
}

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