Compiler bug in the 1.0.x series of the Arduino IDE

I’ve found what appears to be a compiler bug in the 1.0.x series of the Arduino IDE. This bug may be an code alignment issue or some other weird boundary condition in connection with optimization - and it can be remedied by simply wedging in a line of redundant code, or even code that does nothing useful whatsoever other than survive optimization (examples are in the code). (Note that I have NOT seen the bug in the 1.5.x series of the IDE.)

I first noticed this bug when I tried to pass a buffer to a function… it took a little while to realize that this error had nothing to do with the new code and everything to do with an obscure compiler bug.

When this fails to build (it’ll fail consistently for a given configuration of code) this is the error I get. At first I assumed it was me, but if adding nonsense code or removing innocuous code causes a broken build to start passing then something’s wrong. In my experience, it’s usually because of optimization bugs in the compiler. (I once investigated a bug that manifested depending on the placement of a valid comment line in the code, so I’ve had some experience with even weirder optimization foibles in GCC.)

Hopefully this post helps others avoid wasting time debugging an “error” that’s not necessarily their own.

The error:

"c:/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr6/crtm2560.o: In function `__vector_default':
(.vectors+0xd0): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_52' defined in .text.__vector_52 section in core.a(HardwareSerial.cpp.o)
// Test failed with 1.0.3 and 1.0.5 IDEs, target board = Arduino Mega 2560
// -- Bug appears and is reproducible
//
// Tested OK with 1.5.2 IDE
// -- Builds fine on the 1.5.x IDEs (have yet to see the bug there)

#include <Wire.h>

uint8_t buf[24];

void setup (void)
{
  Serial.begin(57600);
  Serial.println("-- Starts"); 
  Wire.begin();
}

void loop (void) {
  randomSeed(analogRead(0)); // Remove this line and this builds
  //randomSeed(analogRead(0)); // ... Or uncomment this second copy of the line above and it builds
  //int buf2; buf2 = random(0); // ... Or add this useless line and it builds
 
  while (1 == 1) {
      for (int i = 0; i < 24; i++) {
          buf[i] = random(255);
      }
      writeFrame(buf);
  }
}

void writeFrame(uint8_t bufptr[]) {
      Wire.beginTransmission(0x58);
      Wire.write(bufptr, 24);
      Wire.endTransmission();
      delay(25);
}

MartyMacGyver:
Hopefully this post helps others avoid wasting time debugging an "error" that's not necessarily their own.

I don't know how helpful it will be considering you yourself missed the many other posts about this same bug.

wonder

is this going to be fixed ?

judging by past experience.

drjiohnsmith:
is this going to be fixed ?

What did your Google searching reveal?