Skip to content

DOC_VALID() usage correctness #3872

Closed
Closed
@techee

Description

@techee

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:

geany/src/document.c

Lines 1282 to 1292 in 11b4a00

static gboolean show_tab_cb(gpointer data)
{
GeanyDocument *doc = (GeanyDocument *) data;
show_tab_idle = 0;
/* doc might not be valid e.g. if user closed a tab whilst Geany is opening files */
if (DOC_VALID(doc))
document_show_tab(doc);
return G_SOURCE_REMOVE;
}

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions