fix(bin.c): safer generate_id()
set next byte of ID to zero each time a new character is appended to prevent undefined behaviours.
This commit is contained in:
parent
fdf588e523
commit
de51c9830a
1 changed files with 2 additions and 3 deletions
5
bin.c
5
bin.c
|
@ -34,7 +34,7 @@ char *generate_id(int min_length)
|
|||
|
||||
/* allocate a buffer to store the ID */
|
||||
char *buffer;
|
||||
if ((buffer = calloc(length + 1, sizeof(char))) == NULL)
|
||||
if ((buffer = malloc((length + 1) * sizeof(char))) == NULL)
|
||||
return NULL;
|
||||
|
||||
/* for each letter, generate a random one */
|
||||
|
@ -43,6 +43,7 @@ char *generate_id(int min_length)
|
|||
return NULL;
|
||||
|
||||
buffer[i] = id_symbols[rand() % strlen(id_symbols)];
|
||||
buffer[i + 1] = 0;
|
||||
|
||||
/* collision? */
|
||||
if (i == length - 1 && paste_exists(buffer)) {
|
||||
|
@ -54,12 +55,10 @@ char *generate_id(int min_length)
|
|||
free(buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buffer = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
buffer[length] = 0;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue