cancel
Showing results for 
Search instead for 
Did you mean: 

Help with debuging a for statement in BluePill

MNoro.2
Associate II

Hi, I'm using a base64 library to convert my data to my base64 on STM BluePill. everything works fine, but after 5 minutes i receive an imprecise data access violation hard fault error on a for statement. i'm using main loop to run my code every 1 second for testing :

while (1)

 {

 char string[] = "Hello world!";

 sendMessage(string, sizeof(string));

 printf("dec:");

 count++;

    HAL_Delay(1000);

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

and this is the encode function:

char  *out;

size_t elen;

size_t i;

size_t j;

size_t v;

if (in == NULL || len == 0)

return NULL;

elen = b64_encoded_size(len);

out = malloc(elen+1);

out[elen] = '\0';

for (i=0, j=0; i<len; i+=3, j+=4) { <------------------- hard fault on this line

v = in[i];

v = i+1 < len ? v << 8 | in[i+1] : v << 8;

v = i+2 < len ? v << 8 | in[i+2] : v << 8;

out[j]  = b64chars[(v >> 18) & 0x3F];

out[j+1] = b64chars[(v >> 12) & 0x3F];

if (i+1 < len) {

out[j+2] = b64chars[(v >> 6) & 0x3F];

} else {

out[j+2] = '=';

}

if (i+2 < len) {

out[j+3] = b64chars[v & 0x3F];

} else {

out[j+3] = '=';

}

}

return out;

i really don't know whats the problem. thanks for your help and your time.

1 REPLY 1
gbm
Lead III

Which environment do you use? printf() from CubeIDE is risky - it doesn't output anything until newline and it may easily overflow the buffer. Avoid malloc(), use statically allocated buffer for your data. Make sure that you have some heap space available even if you don't use malloc(). You have not supplied enough information but I suspect the lack of heap space problem.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice