Go Down

Topic: ArduinoISP, suggestion to improve portability (Read 3917 times) previous topic - next topic


ArduinoISP.pde provided with Arduino 0022, line 364

Code: [Select]
int length = 256 * getch() + getch();

With the GNU compiler, if 0x00 is received first and then 0x80 is received second, then this evaluates to

Code: [Select]
int length = 256 * 0x00 + 0x80;

and the result of "length" is 128 as expected

However, if this code is ported to another compiler, the following happens

Code: [Select]
int length = 256 * 0x80 + 0x00;

the result of "length" is 0x8000, which is not the expected result. This has caused a port of the ArduinoISP code to fail by running the for loop on line 343 for longer than what avrdude specified, causing a timeout.

please see http://en.wikipedia.org/wiki/Sequence_point about the nature of the problem
Consider two functions f() and g(). In C and C++, the + operator is not a sequence point, and therefore in the expression f()+g() it is possible that either f() or g() will be executed first.

Due to this, I plead that the next release would use something like

Code: [Select]

unsigned char higherBits = getch();
unsigned char lowerBits = getch();
int length = 256 * higherBits + lowerBits;

There are several locations where this change needs to be made, the one I pointed out is only one such location.


You should submit this as an issue at http://code.google.com/p/arduino/


bah, I don't think anybody watches the issue list, there are 266 issues still open :-/


The beauty of the issue list is that your issue will stay there until addressed, even if no one is paying any attention.  Whereas if you just report it in the forums, it falls off of everyone's radar screen and becomes forgotten...

Right now, there's a lot of focus on the changes they want to get made for the 1.0 release.

Issues get fixed faster with complete patchable diffs attached...

Go Up