While looking at some Geany code, I noticed that DOC_VALID() is used in some callbacks where it's supposed to check whether the document was closed in the meantime:

https://github.com/geany/geany/blob/11b4a00a3020b1c9ace3d3ae65aa5ec7d5ff84e0/src/document.c#L1282-L1292

Unless I'm missing something, I don't think it's correct - if the pointer to the doc was freed in the meantime, DOC_VALID() usage would lead to memory access over invalid pointer.

In the LSP plugin plugin I have to deal with a similar situation where I cannot be sure whether the document still exists when I receive a response from the LSP server as a callback. I use this function:

gboolean lsp_utils_doc_is_valid(GeanyDocument *doc)
 {
 	gint i;

 	foreach_document(i)
 	{
 		if (doc == documents[i])
 			return TRUE;
 	}

 	return FALSE;
 }

Even this function isn't 100% correct - the original document could have been closed and a new document created at the same time and the new document's pointer could be identical to the closed document's pointer so doc would be valid but pointing to a different document. (I decided to accept this limitation as I think it's not so frequent.)

My question is, shouldn't we do something similar in Geany?


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <geany/geany/issues/3872@github.com>