1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-06-29 15:27:45 +02:00

Fix bad behaviour with duplicate file names

If the new path is too long for the buffer we cancel the file transfer
and return an error instead of truncating the file name and continuing.
This commit is contained in:
jfreegman 2020-11-03 13:03:47 -05:00
parent a86884c40e
commit 1a723f0e8e
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63

View File

@ -638,21 +638,23 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_
while ((filecheck = fopen(file_path, "r"))) {
fclose(filecheck);
file_path[path_len] = '\0';
char d[9];
sprintf(d, "(%d)", count);
++count;
char d[5];
snprintf(d, sizeof(d), "(%d)", count);
size_t d_len = strlen(d);
if (path_len + d_len >= file_path_buf_size) {
path_len = file_path_buf_size - d_len - 1;
file_path[path_len] = '\0';
tox_file_control(m, friendnum, filenum, TOX_FILE_CONTROL_CANCEL, NULL);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: File path too long.");
free(file_path);
return;
}
strcat(file_path, d);
file_path[path_len + d_len] = '\0';
if (count > 999) {
if (++count > 99) { // If there are this many duplicate file names we should probably give up
tox_file_control(m, friendnum, filenum, TOX_FILE_CONTROL_CANCEL, NULL);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: invalid file path.");
free(file_path);