问题
I try to concatenate the output of AES-256 encryption to a string (to compare this string against the encrypted String sent from an Android phone).
Basically, the concatination seems to work, but after a few runs errors (non readable characters, string getting shorter instead of longer) or crashes occur. It is reproducible, crashes at the exact same point after restart.
I extracted some lines of Arduino code that demonstrate the problem. It does the following:
- Create a random number and write it into an array (works)
- AES- encode this array (works)
- Build a HEX representation of each array index (works)
- Concatenate the indices to a String (crashes)
#include <SPI.h>
#include "aes256.h" //include this lib
uint8_t key[] = {9,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,
1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8 }; //the encryption key
aes256_context ctxt; //context needed for aes library
void setup() {
Serial.begin(9600);
}
void loop() {
uint8_t data[] = {
0x53, 0x73, 0x64, 0x66, 0x61, 0x73, 0x64, 0x66,
0x61, 0x73, 0x64, 0x66, 0x61, 0x73, 0x64, 0x65, }; //the message to be encoded
long InitialRandom = random(2147483647); //0 to highest possible long
String RandomString = "" ;
RandomString+=InitialRandom; //random number to String
Serial.println(RandomString); //for debugging
//update data array: Random String into data array
for (int x=0; x<RandomString.length(); x++){
data[x] = RandomString[x];
}
//this encrypts data array, which changes
aes256_init(&ctxt, key); //initialize the lib
aes256_encrypt_ecb(&ctxt, data); //do the encryption
aes256_done(&ctxt);
//Here the problem starts.............................................
String encrypted=""; //the string I need finally
for (int x=0; x<sizeof(data); x++){ //data array is 16 in size
int a = data[x];
String b = String (a, HEX);
if(b.length()==1) b="0"+b; //if result is e.g "a" it should be "0a"
encrypted.concat(b); //this line causes the problem!!!
//Serial.println(b); //works perfect if above line commented out
Serial.println(encrypted); //see the string geting longer until problems occur
}
//Here the problem ends.............................................
Serial.println(); //go for next round, until crashing
}
I have searched the forums, tried different ways to concatenate (+ operator, strcat). All had similar effects. I read that the String library had a bug, updated Arduino IDE to 1.0.
This has kept me busy for days, any help is highly appreciated,
Thanks a lot!
回答1:
You are probably running out of memory as Arduino only has a small amount.
Check how much memory you have free during your loop.
The culprit may be that the implementation of String (see Arduino WString.cpp) is using realloc(), and your memory is probably being heavily defregmented with one or two byte strings (each of which has a 16 byte heap header cost).
You could re-write the above more efficiently by pre-allocating a String reserve() functions to pre-allocate the buffer. Or re-write it using native C++ char arrays.
来源:https://stackoverflow.com/questions/9168907/arduino-crashes-and-errors-when-concatenating-strings