xml-backend: Don't try to close m_lockfd if it's not open

m_lockfd is not initialised. If the file is locked then it will not be set
before session_end and close() will be called on an uninitialised int.

Initialise it to -1 in the class definition.
Consistently use -1 instead of "< 0" or "< 1" as the definition of invalid.
Always set it to -1 after closing it.
This commit is contained in:
Simon Arlott 2021-08-24 08:54:06 +01:00
parent 228954c408
commit e4619fdae6
No known key found for this signature in database
GPG Key ID: DF001BFD83E75990
2 changed files with 9 additions and 3 deletions

View File

@ -171,8 +171,11 @@ GncXmlBackend::session_end()
if (!m_linkfile.empty()) if (!m_linkfile.empty())
g_unlink (m_linkfile.c_str()); g_unlink (m_linkfile.c_str());
if (m_lockfd > 0) if (m_lockfd != -1)
{
close (m_lockfd); close (m_lockfd);
m_lockfd = -1;
}
if (!m_lockfile.empty()) if (!m_lockfile.empty())
{ {
@ -643,7 +646,7 @@ GncXmlBackend::get_file_lock ()
m_lockfd = g_open (m_lockfile.c_str(), O_RDWR | O_CREAT | O_EXCL , m_lockfd = g_open (m_lockfile.c_str(), O_RDWR | O_CREAT | O_EXCL ,
S_IRUSR | S_IWUSR); S_IRUSR | S_IWUSR);
if (m_lockfd < 0) if (m_lockfd == -1)
{ {
/* oops .. we can't create the lockfile .. */ /* oops .. we can't create the lockfile .. */
switch (errno) switch (errno)
@ -708,6 +711,7 @@ GncXmlBackend::get_file_lock ()
set_error(ERR_BACKEND_LOCKED); set_error(ERR_BACKEND_LOCKED);
g_unlink (linkfile.str().c_str()); g_unlink (linkfile.str().c_str());
close (m_lockfd); close (m_lockfd);
m_lockfd = -1;
g_unlink (m_lockfile.c_str()); g_unlink (m_lockfile.c_str());
return false; return false;
} }
@ -721,6 +725,7 @@ GncXmlBackend::get_file_lock ()
set_message(msg + m_lockfile); set_message(msg + m_lockfile);
g_unlink (linkfile.str().c_str()); g_unlink (linkfile.str().c_str());
close (m_lockfd); close (m_lockfd);
m_lockfd = -1;
g_unlink (m_lockfile.c_str()); g_unlink (m_lockfile.c_str());
return false; return false;
} }
@ -730,6 +735,7 @@ GncXmlBackend::get_file_lock ()
set_error(ERR_BACKEND_LOCKED); set_error(ERR_BACKEND_LOCKED);
g_unlink (linkfile.str().c_str()); g_unlink (linkfile.str().c_str());
close (m_lockfd); close (m_lockfd);
m_lockfd = -1;
g_unlink (m_lockfile.c_str()); g_unlink (m_lockfile.c_str());
return false; return false;
} }

View File

@ -60,7 +60,7 @@ private:
std::string m_dirname; std::string m_dirname;
std::string m_lockfile; std::string m_lockfile;
std::string m_linkfile; std::string m_linkfile;
int m_lockfd; int m_lockfd = -1;
QofBook* m_book = nullptr; /* The primary, main open book */ QofBook* m_book = nullptr; /* The primary, main open book */
}; };