Invalid C-string in `examples/c/ADM_register_job.c`
The generated strings that we are using to mock datasets are always missing the last character (i.e. input-dataset-
is generated instead of input-dataset-0
). The reason is that even though we are computing the length of the final string and allocating its buffer correctly with:
const char* pattern = "output-dataset-%d";
size_t n = snprintf(NULL, 0, pattern, i);
char* id = (char*) malloc(n + 1);
the following snprintf(id, n, pattern, i);
statement does not take into account that n
indicates the resulting string length without accounting for the terminating \0
character.
The correct code would thus be:
const char* pattern = "output-dataset-%d";
size_t n = snprintf(NULL, 0, pattern, i);
char* id = (char*) malloc(n + 1);
snprintf(id, n + 1, pattern, i);
Edited by Alberto Miranda