I have a dynamically allocated char array with pre_padding_buffer (size 8) and post_padding_buffer (size 6). I need to copy the string over to a character pointer like this:
[ ][ ][ ][ ][ ][ ][ ][ ][e][x][a][m][p][l][e][ ][ ][ ][ ][ ][ ]
The paddings are not optional and required by the specs of a machine I'm communicating with (it can be filled with empty/garbage data, it's overwritten anyways).
Currently this is what I'm doing.
unsigned char *async_string = get_async_string();
unsigned char *response_all_buf = (unsigned char*)malloc(LWS_SEND_BUFFER_PRE_PADDING + strlen(async_string) + LWS_SEND_BUFFER_POST_PADDING);
//Copy string
int i = 0;
for (i = 0; i < strlen(async_string); i++) {
response_all_buf[LWS_SEND_BUFFER_PRE_PADDING + i] = async_string[i];
}
libwebsocket_write(wsi,respones_all_buf,strlen(async_string),LWS_WRITE_TEXT);
free(response_all_buf); //<-- Segmentation fault here
Segmentation fault must indicate that I'm not copying the string correctly. What's the correct way of doing this when you have paddings to insert before and after?