title: Reliable string concatenation in embedded system using C language
body: <p>I'm currently working on a piece of code that will send some XML data as string via Ethernet. The data should be constructed first, in order to be sent to the socket. I'm using STM32F4xx type microcontrollers and using IAR EWARM embedded developement environment.</p>
  
  <p>I've been using the following function:</p>
  
  <pre><code>char* concat(char *s1, char *s2)
  {
      size_t len1 = strlen(s1);
      size_t len2 = strlen(s2);
      char *result = malloc(len1+len2+1);//+1 for the zero-terminator
      //in real code you would check for errors in malloc here
      memcpy(result, s1, len1);
      memcpy(result+len1, s2, len2+1);//+1 to copy the null-terminator
      return result;
  }
  
  char* data = concat("&lt;data&gt;\n&lt;items&gt;\n&lt;radar x=\"", "1"); 
  data = concat(data, "\"&gt;&lt;/radar&gt;\n&lt;radar v=\"");
  data = concat(data, "1");
  data = concat(data, "\"&gt;&lt;/radar&gt;\n&lt;/items&gt;\n&lt;/data&gt;");
  len = strlen(data);
  
  Socket_Send(data,len);
  </code></pre>
  
  <p>The ethernet server output results the following:</p>
  
  <pre><code>Send:(09:41:17) alive
  Rec:(09:41:17)&lt;data&gt;
  &lt;items&gt;
  &lt;radar x="1"&gt;&lt;/radar&gt;
  &lt;radar v="1"&gt;&lt;/radar&gt;
  &lt;/items&gt;
  &lt;/data&gt;
  Send:(09:41:18) alive
  Rec:(09:41:18)&lt;data&gt;
  &lt;items&gt;
  &lt;radar x="1"&gt;&lt;/radar&gt;
  &lt;radar v="1"&gt;&lt;/radar&gt;
  &lt;/items&gt;
  &lt;/data&gt;
  Send:(09:41:18) alive
  Rec:(09:41:18)&lt;data&gt;
  </code></pre>
  
  <p>It sends the data correctly for 2 times, then writes only 6 characters. How do I fix this? Any help is greatly appreciated. Thanks in advance..</p>
  
  <p><strong>EDIT 1:</strong>
  Suggested code:</p>
  
  <pre><code>data = concat("&lt;data&gt;\n&lt;items&gt;\n&lt;radar x=\"", "1"); 
  char *tmp = data;
  data = concat(data, "\"&gt;&lt;/radar&gt;\n&lt;radar v=\"");
  free(tmp);
  data = concat(data, "1");
  data = concat(data, "\"&gt;&lt;/radar&gt;\n&lt;/items&gt;\n&lt;/data&gt;");
  
  len = strlen(data);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Send:(09:51:38) alive
  Rec:(09:51:38)&lt;data&gt;
  &lt;items&gt;
  &lt;radar x="1"&gt;&lt;/radar&gt;
  &lt;radar v="1"&gt;&lt;/radar&gt;
  &lt;/items&gt;
  &lt;/data&gt;
  Send:(09:51:40) alive
  Rec:(09:51:40)&lt;data&gt;
  &lt;items&gt;
  &lt;radar x="1"&gt;&lt;/radar&gt;
  &lt;radar v="1"&gt;&lt;/radar&gt;
  &lt;/items&gt;
  &lt;/data&gt;
  Send:(09:51:44) alive
  Rec:(09:51:44)&lt;data&gt;
  &lt;items&gt;
  &lt;radar x="1"&gt;&lt;/radar&gt;
  &lt;radar v="1"&gt;&lt;/radar&gt;
  &lt;/items&gt;
  &lt;/data&gt;
  Send:(09:51:44) alive
  Rec:(09:51:44)8 ¹Õ"&gt;&lt;/radar&gt;
  &lt;/items&gt;
  &lt;/data&gt;
  Send:(09:51:44) alive
  Rec:(09:51:44)8 ¹Õ1"&gt;&lt;/radar&gt;
  &lt;/items&gt;
  &lt;/data&gt;
  Send:(09:51:45) alive
  Rec:(09:51:45)8 ¹Õ1"&gt;&lt;/radar&gt;
  &lt;/items&gt;
  &lt;/data&gt;
  Send:(09:51:45) alive
  Rec:(09:51:45)8 ¹Õ
  Send:(09:51:45) alive
  Rec:(09:51:45)8 ¹Õ
  Send:(09:51:46) alive
  Rec:(09:51:46)8 ¹Õ
  Send:(09:51:46) alive
  Rec:(09:51:46)8 ¹Õ
  </code></pre>
  
  <p><strong>EDIT 2 (POOR SOLUTION):</strong></p>
  
  <pre><code>char* data;
  char* data1;
  char* data2;
  char* data3;
  data1 = concat("&lt;data&gt;\n&lt;items&gt;\n&lt;radar x=\"", "1"); 
  
  data2 = concat(data1, "\"&gt;&lt;/radar&gt;\n&lt;radar v=\"");
  free(data1);
  
  data3 = concat(data2, "1");
  free(data2);
  
  data = concat(data3, "\"&gt;&lt;/radar&gt;\n&lt;/items&gt;\n&lt;/data&gt;");
  free(data3);
  
  len = strlen(data);
  Socket_Send(data,len);
  free(data);
  </code></pre>
  
  <p><strong>EDIT 3 (SOLVED):</strong></p>
  
  <p>When the number of tags increased in the XML, the mem leakage happened as expected. As people suggested, I've tried <strong><em>strcat</em></strong> and it worked. But strcpy does not work in my compiler. I would strongly suggest to anyone who have the same issue to use strcat. </p>