char *buf = malloc(len + 1); if (buf == NULL) { fprintf(stderr, "out of memory allocating %zu bytes\n", len + 1); return -1; } memcpy(buf, src, len); buf[len] = '\0';