Heap Corruption Detected: after Normal block

You are allocating memory that is one byte too short. Your calculations are for the length of the data between e.g. “Subject:” and “Content:” but do not take into account the need for a null terminator in the string. Then when you manually add the null terminator you are invoking undefined behaviour by writing past the end of the array.

Changing your code to the following should fix it.

char* messageSubject = malloc(cPtr - sPtr - strlen("Subject:") + 1)
char* messageContent = malloc(strlen(cPtr + strlen("Content:")) + 1)

You also do not show the code in the “…” section, so you may have an unterminated string in there that if it is being processed by the string library routines could cause problems.

Leave a Comment