Commit a436cba0 authored by Fernando Carvajal's avatar Fernando Carvajal

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
parent 12104a23
...@@ -9393,12 +9393,7 @@ int ParseRemoteOptions(char *opts) ...@@ -9393,12 +9393,7 @@ int ParseRemoteOptions(char *opts)
int hasDelta = 0; int hasDelta = 0;
int hasStream = 0; int hasStream = 0;
int hasData = 0; int hasData = 0;
int hasLimit = 0;
int hasRender = 0;
int hasTaint = 0;
int hasType = 0; int hasType = 0;
int hasStrict = 0;
int hasShseg = 0;
// //
// Get rid of the terminating space. // Get rid of the terminating space.
...@@ -9623,7 +9618,6 @@ int ParseRemoteOptions(char *opts) ...@@ -9623,7 +9618,6 @@ int ParseRemoteOptions(char *opts)
} }
} }
hasLimit = 1;
} }
else if (strcasecmp(name, "render") == 0) else if (strcasecmp(name, "render") == 0)
{ {
...@@ -9636,7 +9630,6 @@ int ParseRemoteOptions(char *opts) ...@@ -9636,7 +9630,6 @@ int ParseRemoteOptions(char *opts)
useRender = ValidateArg("remote", name, value); useRender = ValidateArg("remote", name, value);
} }
hasRender = 1;
} }
else if (strcasecmp(name, "taint") == 0) else if (strcasecmp(name, "taint") == 0)
{ {
...@@ -9649,7 +9642,6 @@ int ParseRemoteOptions(char *opts) ...@@ -9649,7 +9642,6 @@ int ParseRemoteOptions(char *opts)
useTaint = ValidateArg("remote", name, value); useTaint = ValidateArg("remote", name, value);
} }
hasTaint = 1;
} }
else if (strcasecmp(name, "type") == 0) else if (strcasecmp(name, "type") == 0)
{ {
...@@ -9682,7 +9674,6 @@ int ParseRemoteOptions(char *opts) ...@@ -9682,7 +9674,6 @@ int ParseRemoteOptions(char *opts)
useStrict = ValidateArg("remote", name, value); useStrict = ValidateArg("remote", name, value);
} }
hasStrict = 1;
} }
else if (strcasecmp(name, "shseg") == 0) else if (strcasecmp(name, "shseg") == 0)
{ {
...@@ -9704,7 +9695,6 @@ int ParseRemoteOptions(char *opts) ...@@ -9704,7 +9695,6 @@ int ParseRemoteOptions(char *opts)
return -1; return -1;
} }
hasShseg = 1;
} }
else if (strcasecmp(name, "delta") == 0) else if (strcasecmp(name, "delta") == 0)
{ {
......
...@@ -237,8 +237,14 @@ FILE *Popen(char * const parameters[], const char *type) ...@@ -237,8 +237,14 @@ FILE *Popen(char * const parameters[], const char *type)
struct passwd *pwent = getpwuid(getuid()); struct passwd *pwent = getpwuid(getuid());
if (pwent) initgroups(pwent->pw_name,getgid()); if (pwent) initgroups(pwent->pw_name,getgid());
setgid(getgid()); if (setgid(getgid()) == -1)
setuid(getuid()); {
_exit(127);
}
if (setuid(getuid()) == -1)
{
_exit(127);
}
if (*type == 'r') if (*type == 'r')
{ {
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <cstdio> #include <cstdio>
#include <unistd.h> #include <unistd.h>
#include <cstdlib> #include <cstdlib>
#include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifdef ANDROID #ifdef ANDROID
...@@ -4194,6 +4195,12 @@ int Proxy::handleSaveStores() ...@@ -4194,6 +4195,12 @@ int Proxy::handleSaveStores()
char *cacheToAdopt = NULL; char *cacheToAdopt = NULL;
//
// Set to false the indicator for cumulative store
// size too small
//
bool isTooSmall = false;
if (control -> PersistentCacheEnableSave) if (control -> PersistentCacheEnableSave)
{ {
#ifdef TEST #ifdef TEST
...@@ -4201,7 +4208,7 @@ int Proxy::handleSaveStores() ...@@ -4201,7 +4208,7 @@ int Proxy::handleSaveStores()
<< logofs_flush; << logofs_flush;
#endif #endif
cacheToAdopt = handleSaveAllStores(control -> PersistentCachePath); cacheToAdopt = handleSaveAllStores(control -> PersistentCachePath, isTooSmall);
} }
#ifdef TEST #ifdef TEST
else else
...@@ -4253,21 +4260,28 @@ int Proxy::handleSaveStores() ...@@ -4253,21 +4260,28 @@ int Proxy::handleSaveStores()
return 1; return 1;
} }
#ifdef TEST
else else
{ {
*logofs << "Proxy: No cache file produced from message stores.\n" #ifdef TEST
<< logofs_flush; *logofs << "Proxy: No cache file produced from message stores.\n"
} << logofs_flush;
#endif #endif
// //
// It can be that we didn't generate a new cache // It can be that we didn't generate a new cache
// because store was too small or persistent cache // because store was too small or persistent cache
// was disabled. This is not an error. // was disabled. This is not an error.
// //
return 0; if (control -> PersistentCacheEnableSave && !isTooSmall)
{
return -1;
}
else
{
return 0;
}
}
} }
int Proxy::handleLoadStores() int Proxy::handleLoadStores()
...@@ -4875,8 +4889,10 @@ int Proxy::handleLoadVersion(const unsigned char *buffer, int &major, ...@@ -4875,8 +4889,10 @@ int Proxy::handleLoadVersion(const unsigned char *buffer, int &major,
return 1; return 1;
} }
char *Proxy::handleSaveAllStores(const char *savePath) const char *Proxy::handleSaveAllStores(const char *savePath, bool & isTooSmall) const
{ {
isTooSmall = false;
int cumulativeSize = MessageStore::getCumulativeTotalStorageSize(); int cumulativeSize = MessageStore::getCumulativeTotalStorageSize();
if (cumulativeSize < control -> PersistentCacheThreshold) if (cumulativeSize < control -> PersistentCacheThreshold)
...@@ -4888,6 +4904,13 @@ char *Proxy::handleSaveAllStores(const char *savePath) const ...@@ -4888,6 +4904,13 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
<< ".\n" << logofs_flush; << ".\n" << logofs_flush;
#endif #endif
//
// Cumulative store size is smaller than threshold
// so the indicator is set to true
//
isTooSmall = true;
return NULL; return NULL;
} }
else if (savePath == NULL) else if (savePath == NULL)
...@@ -4923,20 +4946,28 @@ char *Proxy::handleSaveAllStores(const char *savePath) const ...@@ -4923,20 +4946,28 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
md5_state_t *md5StateClient = NULL; md5_state_t *md5StateClient = NULL;
md5_byte_t *md5DigestClient = NULL; md5_byte_t *md5DigestClient = NULL;
char *tempName = NULL;
char md5String[MD5_LENGTH * 2 + 2]; char md5String[MD5_LENGTH * 2 + 2];
char fullName[strlen(savePath) + MD5_LENGTH * 2 + 4]; char fullName[strlen(savePath) + MD5_LENGTH * 2 + 4];
if (control -> ProxyMode == proxy_client) //
{ // Prepare the template for the temporary file
tempName = tempnam(savePath, "Z-C-"); //
}
else const char* const uniqueTemplate = "XXXXXX";
{ char tempName[strlen(savePath) + strlen("/") + 4 + strlen(uniqueTemplate) + 1];
tempName = tempnam(savePath, "Z-S-");
} 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 // Change the mask to make the file only
...@@ -4946,35 +4977,68 @@ char *Proxy::handleSaveAllStores(const char *savePath) const ...@@ -4946,35 +4977,68 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
mode_t fileMode = umask(0077); mode_t fileMode = umask(0077);
cachefs = new ofstream(tempName, ios::out | ios::binary); //
// Generate a unique temporary filename from tempName
umask(fileMode); // and then create and open the file
//
if (tempName == NULL || cachefs == NULL) int fdTemp = mkstemp(tempName);
if (fdTemp == -1)
{ {
#ifdef PANIC #ifdef PANIC
*logofs << "Proxy: PANIC! Can't create temporary file in '" *logofs << "Proxy: PANIC! Can't create temporary file in '"
<< savePath << "'.\n" << logofs_flush; << savePath << "'. Cause = " << strerror(errno) << ".\n" << logofs_flush;
#endif #endif
cerr << "Error" << ": Can't create temporary file in '" cerr << "Error" << ": Can't create temporary file in '"
<< savePath << "'.\n"; << savePath << "'. Cause = " << strerror(errno) << ".\n";
if (tempName != NULL) umask(fileMode);
{
free(tempName);
}
if (cachefs != NULL) EnableSignals();
{
delete cachefs; 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(); EnableSignals();
return NULL; return NULL;
} }
//
// Close the file descriptor returned by mkstemp
// and restore the old mask
//
close(fdTemp);
umask(fileMode);
md5StateStream = new md5_state_t(); md5StateStream = new md5_state_t();
md5DigestStream = new md5_byte_t[MD5_LENGTH]; md5DigestStream = new md5_byte_t[MD5_LENGTH];
...@@ -5007,8 +5071,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const ...@@ -5007,8 +5071,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateStream; delete md5StateStream;
delete [] md5DigestStream; delete [] md5DigestStream;
free(tempName);
EnableSignals(); EnableSignals();
return NULL; return NULL;
...@@ -5029,8 +5091,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const ...@@ -5029,8 +5091,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateStream; delete md5StateStream;
delete [] md5DigestStream; delete [] md5DigestStream;
free(tempName);
EnableSignals(); EnableSignals();
return NULL; return NULL;
...@@ -5086,7 +5146,7 @@ char *Proxy::handleSaveAllStores(const char *savePath) const ...@@ -5086,7 +5146,7 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
#endif #endif
if (allSaved == 0) if (allSaved == -1)
{ {
handleFailOnSave(tempName, "C"); handleFailOnSave(tempName, "C");
...@@ -5098,8 +5158,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const ...@@ -5098,8 +5158,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateClient; delete md5StateClient;
delete [] md5DigestClient; delete [] md5DigestClient;
free(tempName);
EnableSignals(); EnableSignals();
return NULL; return NULL;
...@@ -5139,8 +5197,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const ...@@ -5139,8 +5197,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateClient; delete md5StateClient;
delete [] md5DigestClient; delete [] md5DigestClient;
free(tempName);
EnableSignals(); EnableSignals();
return NULL; return NULL;
...@@ -5181,8 +5237,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const ...@@ -5181,8 +5237,6 @@ char *Proxy::handleSaveAllStores(const char *savePath) const
delete md5StateClient; delete md5StateClient;
delete [] md5DigestClient; delete [] md5DigestClient;
free(tempName);
// //
// Restore the original handlers. // Restore the original handlers.
// //
......
...@@ -985,7 +985,7 @@ class Proxy ...@@ -985,7 +985,7 @@ class Proxy
int handleLoadStores(); int handleLoadStores();
int handleSaveStores(); int handleSaveStores();
char *handleSaveAllStores(const char *savePath) const; char *handleSaveAllStores(const char *savePath, bool & isTooSmall) const;
virtual int handleSaveAllStores(ostream *cachefs, md5_state_t *md5StateStream, virtual int handleSaveAllStores(ostream *cachefs, md5_state_t *md5StateStream,
md5_state_t *md5StateClient) const = 0; md5_state_t *md5StateClient) const = 0;
......
...@@ -4374,6 +4374,24 @@ int ServerChannel::handleWrite(const unsigned char *message, unsigned int length ...@@ -4374,6 +4374,24 @@ int ServerChannel::handleWrite(const unsigned char *message, unsigned int length
} // End of switch on opcode. } // End of switch on opcode.
// //
// TODO: at the moment the variable hit was being set
// but no used, so to avoid the corresponding warning
// it has been added this block with a logging command.
// This code will be probably optimized away when none
// of the defines is set, but if there is no additional
// use for the hit variable in the future, then maybe
// it could be removed completely.
//
if (hit)
{
#if defined(TEST) || defined(OPCODES)
*logofs << "handleWrite: Cached flag enabled in handled request.\n"
<< logofs_flush;
#endif
}
//
// A packed image request can generate more than just // A packed image request can generate more than just
// a single X_PutImage. Write buffer is handled inside // a single X_PutImage. Write buffer is handled inside
// handleUnpack(). Cannot simply assume that the final // handleUnpack(). Cannot simply assume that the final
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment