aboutsummaryrefslogtreecommitdiff
path: root/nxcomp/Proxy.cpp
diff options
context:
space:
mode:
authorFernando Carvajal <fcarvajal@qindel.com>2016-04-25 12:53:00 +0200
committerFernando Carvajal <fcarvajal@qindel.com>2016-04-27 12:46:00 +0200
commita436cba04834da945d4a2be5335d1d7e95c6bf08 (patch)
treebcbcc29b01a86d2035e7397ded5b24b90c841645 /nxcomp/Proxy.cpp
parent12104a232a6de163f7e871be288ac933083c4226 (diff)
downloadnx-libs-a436cba04834da945d4a2be5335d1d7e95c6bf08.tar.gz
nx-libs-a436cba04834da945d4a2be5335d1d7e95c6bf08.tar.bz2
nx-libs-a436cba04834da945d4a2be5335d1d7e95c6bf08.zip
Clean up compiler warnings in nxcomp
This commit removes several warnings in nxcomp related to unused but set variables. It also replaces tempnam function with the more secure one mkstemp and there has been removed another warning related to setgid and setuid returning values not being checked. So these were the compiler warnings in nxcomp that have been fixed: Loop.cpp: In function ‘int ParseRemoteOptions(char*)’: Loop.cpp:9423:7: warning: variable ‘hasLimit’ set but not used [-Wunused-but-set-variable] int hasLimit = 0; ^ Loop.cpp:9424:7: warning: variable ‘hasRender’ set but not used [-Wunused-but-set-variable] int hasRender = 0; ^ Loop.cpp:9425:7: warning: variable ‘hasTaint’ set but not used [-Wunused-but-set-variable] int hasTaint = 0; ^ Loop.cpp:9427:7: warning: variable ‘hasStrict’ set but not used [-Wunused-but-set-variable] int hasStrict = 0; ^ Loop.cpp:9428:7: warning: variable ‘hasShseg’ set but not used [-Wunused-but-set-variable] int hasShseg = 0; ^ ServerChannel.cpp: In member function ‘virtual int ServerChannel::handleWrite(const unsigned char*, unsigned int)’: ServerChannel.cpp:2132:9: warning: variable ‘hit’ set but not used [-Wunused-but-set-variable] int hit; ^ Proxy.o: In function `Proxy::handleSaveAllStores(char const*) const': Proxy.cpp:(.text+0x2cac): warning: the use of `tempnam' is dangerous, better use `mkstemp' Pipe.cpp: In function ‘FILE* Popen(char* const*, const char*)’: Pipe.cpp:240:23: warning: ignoring return value of ‘int setgid(__gid_t)’, declared with attribute warn_unused_result [-Wunused-result] setgid(getgid()); ^ Pipe.cpp:241:23: warning: ignoring return value of ‘int setuid(__uid_t)’, declared with attribute warn_unused_result [-Wunused-result] setuid(getuid()); ^ There was also a hidden problem in the way Proxy::handleSaveAllStores was checking for an error in the returning value from the call to the virtual method handleSaveAllStores of the specific proxy class really being used (ClientProxy or ServerProxy). Former code was considering the value 0 as the returning value in case of an error whereas both subclasses return the value -1 when there is an error in their handleSaveAllStores method. This bug has been fixed in this commit taking advantage of the modification that was already being made to this method in order to replace tempnam function with the more secure one mkstemp. Fixes: ArcticaProject/nx-libs#103
Diffstat (limited to 'nxcomp/Proxy.cpp')
-rw-r--r--nxcomp/Proxy.cpp150
1 files changed, 102 insertions, 48 deletions
diff --git a/nxcomp/Proxy.cpp b/nxcomp/Proxy.cpp
index ae115fede..7655ccb40 100644
--- a/nxcomp/Proxy.cpp
+++ b/nxcomp/Proxy.cpp
@@ -18,6 +18,7 @@
#include <cstdio>
#include <unistd.h>
#include <cstdlib>
+#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef ANDROID
@@ -4194,6 +4195,12 @@ int Proxy::handleSaveStores()
char *cacheToAdopt = NULL;
+ //
+ // Set to false the indicator for cumulative store
+ // size too small
+ //
+ bool isTooSmall = false;
+
if (control -> PersistentCacheEnableSave)
{
#ifdef TEST
@@ -4201,7 +4208,7 @@ int Proxy::handleSaveStores()
<< logofs_flush;
#endif
- cacheToAdopt = handleSaveAllStores(control -> PersistentCachePath);
+ cacheToAdopt = handleSaveAllStores(control -> PersistentCachePath, isTooSmall);
}
#ifdef TEST
else
@@ -4253,21 +4260,28 @@ int Proxy::handleSaveStores()
return 1;
}
- #ifdef TEST
else
{
- *logofs << "Proxy: No cache file produced from message stores.\n"
- << logofs_flush;
- }
- #endif
+ #ifdef TEST
+ *logofs << "Proxy: No cache file produced from message stores.\n"
+ << logofs_flush;
+ #endif
- //
- // It can be that we didn't generate a new cache
- // because store was too small or persistent cache
- // was disabled. This is not an error.
- //
+ //
+ // It can be that we didn't generate a new cache
+ // because store was too small or persistent cache
+ // was disabled. This is not an error.
+ //
- return 0;
+ if (control -> PersistentCacheEnableSave && !isTooSmall)
+ {
+ return -1;
+ }
+ else
+ {
+ return 0;
+ }
+ }
}
int Proxy::handleLoadStores()
@@ -4875,8 +4889,10 @@ int Proxy::handleLoadVersion(const unsigned char *buffer, int &major,
return 1;
}
-char *Proxy::handleSaveAllStores(const char *savePath) const
+char *Proxy::handleSaveAllStores(const char *savePath, bool & isTooSmall) const
{
+ isTooSmall = false;
+
int cumulativeSize = MessageStore::getCumulativeTotalStorageSize();
if (cumulativeSize < control -> PersistentCacheThreshold)
@@ -4888,6 +4904,13 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
<< ".\n" << logofs_flush;
#endif
+ //
+ // Cumulative store size is smaller than threshold
+ // so the indicator is set to true
+ //
+
+ isTooSmall = true;
+
return NULL;
}
else if (savePath == NULL)
@@ -4923,20 +4946,28 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
md5_state_t *md5StateClient = NULL;
md5_byte_t *md5DigestClient = NULL;
- char *tempName = NULL;
-
char md5String[MD5_LENGTH * 2 + 2];
char fullName[strlen(savePath) + MD5_LENGTH * 2 + 4];
- if (control -> ProxyMode == proxy_client)
- {
- tempName = tempnam(savePath, "Z-C-");
- }
- else
- {
- tempName = tempnam(savePath, "Z-S-");
- }
+ //
+ // Prepare the template for the temporary file
+ //
+
+ const char* const uniqueTemplate = "XXXXXX";
+ char tempName[strlen(savePath) + strlen("/") + 4 + strlen(uniqueTemplate) + 1];
+
+ snprintf(tempName, sizeof tempName, "%s/%s%s",
+ savePath,
+ control -> ProxyMode == proxy_client ?
+ "Z-C-" :
+ "Z-S-",
+ uniqueTemplate);
+
+ #ifdef TEST
+ *logofs << "Proxy: Generating temporary file with template '"
+ << tempName << "'.\n" << logofs_flush;
+ #endif
//
// Change the mask to make the file only
@@ -4946,35 +4977,68 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
mode_t fileMode = umask(0077);
- cachefs = new ofstream(tempName, ios::out | ios::binary);
-
- umask(fileMode);
+ //
+ // Generate a unique temporary filename from tempName
+ // and then create and open the file
+ //
- if (tempName == NULL || cachefs == NULL)
+ int fdTemp = mkstemp(tempName);
+ if (fdTemp == -1)
{
#ifdef PANIC
*logofs << "Proxy: PANIC! Can't create temporary file in '"
- << savePath << "'.\n" << logofs_flush;
+ << savePath << "'. Cause = " << strerror(errno) << ".\n" << logofs_flush;
#endif
cerr << "Error" << ": Can't create temporary file in '"
- << savePath << "'.\n";
+ << savePath << "'. Cause = " << strerror(errno) << ".\n";
- if (tempName != NULL)
- {
- free(tempName);
- }
+ umask(fileMode);
- if (cachefs != NULL)
- {
- delete cachefs;
- }
+ EnableSignals();
+
+ return NULL;
+ }
+
+ #ifdef TEST
+ *logofs << "Proxy: Saving cache to file '"
+ << tempName << "'.\n" << logofs_flush;
+ #endif
+
+ //
+ // Create and open the output stream for the new temporary
+ // file
+ //
+
+ cachefs = new (std::nothrow) ofstream(tempName, ios::out | ios::binary);
+ if ((cachefs == NULL) || cachefs->fail())
+ {
+ #ifdef PANIC
+ *logofs << "Proxy: PANIC! Can't create stream for temporary file '"
+ << tempName << "'.\n" << logofs_flush;
+ #endif
+
+ cerr << "Error" << ": Can't create stream for temporary file '"
+ << tempName << "'.\n";
+
+ close(fdTemp);
+ unlink(tempName);
+
+ umask(fileMode);
EnableSignals();
return NULL;
}
+ //
+ // Close the file descriptor returned by mkstemp
+ // and restore the old mask
+ //
+
+ close(fdTemp);
+ umask(fileMode);
+
md5StateStream = new md5_state_t();
md5DigestStream = new md5_byte_t[MD5_LENGTH];
@@ -5007,8 +5071,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateStream;
delete [] md5DigestStream;
- free(tempName);
-
EnableSignals();
return NULL;
@@ -5029,8 +5091,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateStream;
delete [] md5DigestStream;
- free(tempName);
-
EnableSignals();
return NULL;
@@ -5086,7 +5146,7 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
#endif
- if (allSaved == 0)
+ if (allSaved == -1)
{
handleFailOnSave(tempName, "C");
@@ -5098,8 +5158,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateClient;
delete [] md5DigestClient;
- free(tempName);
-
EnableSignals();
return NULL;
@@ -5139,8 +5197,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateClient;
delete [] md5DigestClient;
- free(tempName);
-
EnableSignals();
return NULL;
@@ -5181,8 +5237,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateClient;
delete [] md5DigestClient;
- free(tempName);
-
//
// Restore the original handlers.
//