One possible problem is that your first memcpy() call will not necessarily result in a null terminated string, since you are not copying the terminator '\ 0' from l->db.param_value.val :
Therefore, when strlen(g->db_cmd) is called in the second memcpy() call, it can return something completely fake. Whether this is a problem depends on whether the g->db_cmd initialized with zeros in advance or not.
Why not use strcat() , which was done in order to do exactly what you are trying to do with memcpy() ?
if (strlen(g->db_cmd) < MAX_DB_CMDS ) { strcat( g->db_cmd, l->db.param_value.val); strcat( g->db_cmd, l->del_const); g->cmd_ctr++; }
This will have the advantage of making it easier for someone to read. You might think that it will be less efficient, but I donβt think so, since you are doing a bunch of strlen() calls explicitly. In any case, I would focus on getting everything right, and then worry about performance. An incorrect code is just as unoptimized as you can get - get it right before you get it quickly. In fact, my next step would be not to improve the code, but to improve the code so that it is less likely to have a buffer overflow (I would probably switch to using something like strlcat() instead of strcat() ).
For example, if g->db_cmd is a char array (and not a pointer), the result might look like this:
size_t orig_len = strlen(g->db_cmd); size_t result = strlcat( g->db_cmd, l->db.param_value.val, sizeof(g->db_cmd)); result = strlcat( g->db_cmd, l->del_const, sizeof(g->db_cmd)); g->cmd_ctr++; if (result >= sizeof(g->db_cmd)) {
If strlcat() not part of your platform, it can be easily found on the net. If you use MSVC, there is a strcat_s() function there that you could use instead (but note that it is not equivalent to strlcat() ), you will have to change the way you check and process the results when strcat_s() called).