Prevent possible crash when using tm_workspace API by plugins
commit8cfa9278e62863228731df00c270578bb47f5c05
authorJiří Techet <techet@gmail.com>
Sun, 2 Apr 2023 22:32:21 +0000 (3 00:32 +0200)
committerJiří Techet <techet@gmail.com>
Sun, 2 Apr 2023 22:32:21 +0000 (3 00:32 +0200)
treeec7695ff66373cb5cbda6f9c28a6f110657e631f
parente2ce7db7063d6d854ba859dd975d4b0de6d0f384
Prevent possible crash when using tm_workspace API by plugins

The current code uses an unreffed TMSourceFile->short_name as the key
into source_file_map. With Geany itself this is no problem as the
TMSourceFiles are always destroyed after their removal from the hash
table.

However, if a plugin adds 2 different files to the TM using

tm_workspace_add_source_file(file1);
tm_workspace_add_source_file(file2);

that have a different path but the same file name, such as

/A/B/myfile.c
/C/D/myfile.c

and at a later point removes them using something like

tm_workspace_remove_source_file(file1);
tm_source_file_free(file1);
tm_workspace_remove_source_file(file2);
tm_source_file_free(file2);

the

tm_source_file_free(file1);

call deallocates the key in the hash table which is now an invalid
pointer and the subsequent

tm_workspace_remove_source_file(file2);

crashes the plugin.

While it would be possible to solve this crash at the plugin level by
reordering the operations such as

tm_workspace_remove_source_file(file1);
tm_workspace_remove_source_file(file2);
tm_source_file_free(file1);
tm_source_file_free(file2);

this is not obvious from the plugin's perspective which doesn't know
Geany's internals so better to solve it at Geany's level by using a
g_strdupped value as the key.

This problem currently happens with the ProjectOrganizer plugin.
src/tagmanager/tm_workspace.c