Skip to content

Potential corruption in attachInterrupt() #25

Open
@NormanDunbar

Description

@NormanDunbar

The Arduino reference for attachInterrupt() states, in the syntax section, that this function should be called as per:

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); (recommended)

However, digitalPinToInterrupt() returns NOT_AN_INTERRUPT (or -1) if a pin other than 2 or 3 is passed. Within the code for attachInterrupt() in WInterrupts.c, I see this:

void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
  if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
      intFunc[interruptNum] = userFunc;

So, if called as recommended, and an invalid pin is passed, the -1is indeed less than EXTERNAL_NUM_INTERRUPTS (or 2) and so the access to the array is out of bounds and may corrupt whatever is in memory just before the array.

I suggest the following should (!) fix it:

 if(interruptNum < EXTERNAL_NUM_INTERRUPTS &&
    interruptNum >= 0) {
      intFunc[interruptNum] = userFunc;

That way, NOT_AN_INTERRUPT will be ignored. Just for safety, I would probably do a similar edit to detachInterrupt() as it doesn't validate the incoming interrupt number for NOT_AN_INTERRUPT either.

Cheers.

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