aboutsummaryrefslogtreecommitdiffstats
path: root/common/rfb/Logger_file.cxx
diff options
context:
space:
mode:
authorPierre Ossman <ossman@cendio.se>2023-01-15 14:41:47 +0100
committerPierre Ossman <ossman@cendio.se>2023-02-04 14:03:13 +0100
commitd608a30d576ed74f7dc7374c1f00d2303fee27f6 (patch)
treea6637dee5800ee1f9af6331d3df02d84afe45d9a /common/rfb/Logger_file.cxx
parentef8c84eb752e1b59ef824cedeb156b2d64fefe37 (diff)
downloadtigervnc-d608a30d576ed74f7dc7374c1f00d2303fee27f6.tar.gz
tigervnc-d608a30d576ed74f7dc7374c1f00d2303fee27f6.zip
Use fixed size character buffer
We know the needed space here, so let's keep it simple with a constant size string buffer.
Diffstat (limited to 'common/rfb/Logger_file.cxx')
-rw-r--r--common/rfb/Logger_file.cxx35
1 files changed, 20 insertions, 15 deletions
diff --git a/common/rfb/Logger_file.cxx b/common/rfb/Logger_file.cxx
index 684614cc..9859eb04 100644
--- a/common/rfb/Logger_file.cxx
+++ b/common/rfb/Logger_file.cxx
@@ -22,20 +22,21 @@
#include <config.h>
#endif
+#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <os/Mutex.h>
-#include <rfb/util.h>
#include <rfb/Logger_file.h>
using namespace rfb;
Logger_File::Logger_File(const char* loggerName)
- : Logger(loggerName), indent(13), width(79), m_filename(0), m_file(0),
+ : Logger(loggerName), indent(13), width(79), m_file(0),
m_lastLogTime(0)
{
+ m_filename[0] = '\0';
mutex = new os::Mutex();
}
@@ -50,11 +51,16 @@ void Logger_File::write(int /*level*/, const char *logname, const char *message)
os::AutoMutex a(mutex);
if (!m_file) {
- if (!m_filename) return;
- CharArray bakFilename(strlen(m_filename) + 1 + 4);
- sprintf(bakFilename.buf, "%s.bak", m_filename);
- remove(bakFilename.buf);
- rename(m_filename, bakFilename.buf);
+ if (m_filename[0] == '\0')
+ return;
+ char bakFilename[PATH_MAX];
+ if (snprintf(bakFilename, sizeof(bakFilename),
+ "%s.bak", m_filename) >= (int)sizeof(bakFilename)) {
+ remove(m_filename);
+ } else {
+ remove(bakFilename);
+ rename(m_filename, bakFilename);
+ }
m_file = fopen(m_filename, "w+");
if (!m_file) return;
}
@@ -93,7 +99,10 @@ void Logger_File::write(int /*level*/, const char *logname, const char *message)
void Logger_File::setFilename(const char* filename)
{
closeFile();
- m_filename = strDup(filename);
+ m_filename[0] = '\0';
+ if (strlen(filename) >= sizeof(filename))
+ return;
+ strcpy(m_filename, filename);
}
void Logger_File::setFile(FILE* file)
@@ -104,13 +113,9 @@ void Logger_File::setFile(FILE* file)
void Logger_File::closeFile()
{
- if (m_filename) {
- if (m_file) {
- fclose(m_file);
- m_file = 0;
- }
- strFree(m_filename);
- m_filename = 0;
+ if (m_file) {
+ fclose(m_file);
+ m_file = 0;
}
}