title: Program crahes on malloc function - syntax correct, dont know why
body: <p>The program crashes on the first malloc of command. I have no idea why. All the values being passed in are correct, and as far as I can tell there is no issue with the syntax of the statement. Oddly, when I put in debug print statements, the program sometimes gets to the first strcpy before crashing. I honestly have no idea what is going wrong.</p>
  <pre><code>void email(char *name, char *email, char *message, char *header)
  {
     FILE* script = fopen(&quot;email.ps1&quot;, &quot;w&quot;);
  
     char staticArgs[] = &quot;$EmailFrom = \&quot;msomeemail@outlook.com\&quot;\n$SMTPServer = \&quot;smtp-mail.outlook.com\&quot;\n$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)\n$SMTPClient.EnableSsl = $true\n$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(\&quot;someemail@outlook.com\&quot;, \&quot;apasswrd\&quot;)\n$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)&quot;;
     char emailTo[] = &quot;$EmailTo = &quot;;
     char subject[] = &quot;$Subject = &quot;;
     char body[] = &quot;$Body = &quot;;
     
     char *command = (char *)malloc( (strlen(emailTo) + strlen(email) + 2) );
  
     strcpy(command, emailTo);
     strcat(command, email);
     strcat(command, &quot;\n&quot;);
     fputs(command, script);
     printf(&quot;command: %s\n&quot;, command);
     memset(command, '0', strlen(command));
  
     command = (char *)realloc(command, (strlen(subject) + strlen(header) + 2) * sizeof(char));
     strcpy(command, subject);
     strcat(command, header);
     strcat(command, &quot;\n&quot;);
     command[strlen(command)] = '\0';
     fputs(command, script);
     printf(&quot;command: %s\n&quot;, command);
     memset(command, '0', strlen(command));
  
     command = (char *)realloc(command, (strlen(body) + strlen(message) + 2) * sizeof(char));
     strcpy(command, body);
     strcat(command, message);
     strcat(command, &quot;\n&quot;);
     command[strlen(command)] = '\0';
     fputs(command, script);
     printf(&quot;command: %s\n&quot;, command);
  
     fputs(staticArgs, script);
  
     fclose(script);
  
     system(&quot;powershell -command \&quot;&amp; .\\email.ps1\&quot;&quot;);
  }
  </code></pre>