fixed bugs detected by clang analyzer
authorVictor Kirhenshtein <victor@netxms.org>
Wed, 5 Aug 2015 19:57:50 +0000 (22:57 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Wed, 5 Aug 2015 19:57:50 +0000 (22:57 +0300)
src/agent/subagents/filemgr/filemgr.cpp
src/agent/subagents/ups/bcmxcp.cpp
src/libnetxms/crypto.cpp
src/libnxsl/library.cpp
src/server/core/dcitem.cpp
src/server/core/events.cpp
src/server/core/interface.cpp
src/server/core/session.cpp
src/server/tools/nxadm/nxadm.cpp

index e3eac4c..45927a2 100644 (file)
@@ -409,35 +409,38 @@ static BOOL Rename(TCHAR* oldName, TCHAR * newName)
 /**
  * Copy file/folder
  */
-static BOOL CopyFile(NX_STAT_STRUCT *st, TCHAR* oldName, TCHAR* newName)
+static BOOL CopyFile(NX_STAT_STRUCT *st, const TCHAR *oldName, const TCHAR *newName)
 {
-   int oldFile, newFile;
-   oldFile = _topen(oldName, O_RDONLY | O_BINARY);
-   newFile = _topen(newName, O_CREAT | O_BINARY | O_WRONLY, st->st_mode); // should be copyed with the same acess rights
-   if (oldFile == -1 || newFile == -1)
+   int oldFile = _topen(oldName, O_RDONLY | O_BINARY);
+   if (oldFile == -1)
+      return FALSE;
+      
+   int newFile = _topen(newName, O_CREAT | O_BINARY | O_WRONLY, st->st_mode); // should be copied with the same acess rights
+   if (newFile == -1)
    {
+      close(oldFile);
       return FALSE;
    }
 
-   BYTE *readBytes;
-   int readSize = 16384, readIn, readOut;
-   readBytes = (BYTE*)malloc(readSize);
+   int size = 16384, in, out;
+   BYTE *bytes = (BYTE *)malloc(size);
 
-   while((readIn = read (oldFile, readBytes, readSize)) > 0)
+   while((in = read(oldFile, bytes, size)) > 0)
    {
-      readOut = write(newFile, readBytes, (ssize_t) readIn);
-      if(readOut != readIn)
+      out = write(newFile, bytes, (ssize_t)in);
+      if (out != in)
       {
          close(oldFile);
          close(newFile);
+         free(bytes);
          return FALSE;
       }
-    }
+   }
 
-    /* Close file descriptors */
-    close(oldFile);
-    close(newFile);
-    return TRUE;
+   close(oldFile);
+   close(newFile);
+   free(bytes);
+   return TRUE;
 }
 
 #endif
@@ -772,10 +775,9 @@ static BOOL ProcessCommands(UINT32 command, NXCPMessage *request, NXCPMessage *r
          }
          ConvertPathToHost(directory);
 
-         bool rootFolder = request->getFieldAsUInt16(VID_ROOT) ? 1 : 0;
          if (CheckFullPath(directory, false) && session->isMasterServer())
          {
-            if(CreateFolder(directory))
+            if (CreateFolder(directory))
             {
                response->setField(VID_RCC, ERR_SUCCESS);
             }
index 9bb3bc4..7495029 100644 (file)
@@ -167,11 +167,13 @@ static void DecodeValue(BYTE *pData, int nDataFmt, int nOutputFmt, char *pszValu
        else if (nDataFmt == 0xE0)
    {
                // Date
+      nValue = GetLong(pData);
       nInputFmt = FMT_INTEGER;
        }
        else if (nDataFmt == 0xE1)
    {
                // Time
+      nValue = GetLong(pData);
       nInputFmt = FMT_INTEGER;
        }
        else
index d3dd5db..6657a36 100644 (file)
@@ -283,34 +283,41 @@ UINT32 LIBNETXMS_EXPORTABLE SetupEncryptionContext(NXCPMessage *msg,
                        RSA *pServerKey;
 
                        *ppCtx = NXCPEncryptionContext::create(dwCiphers);
-
-         // Encrypt key
-         int size = msg->getFieldAsBinary(VID_PUBLIC_KEY, ucKeyBuffer, KEY_BUFFER_SIZE);
-         pBufPos = ucKeyBuffer;
-         pServerKey = d2i_RSAPublicKey(NULL, (OPENSSL_CONST BYTE **)&pBufPos, size);
-         if (pServerKey != NULL)
+         if (*ppCtx != NULL)
          {
-            (*ppResponse)->setField(VID_RCC, RCC_SUCCESS);
-            
-            size = RSA_public_encrypt((*ppCtx)->getKeyLength(), (*ppCtx)->getSessionKey(), ucKeyBuffer, pServerKey, RSA_PKCS1_OAEP_PADDING);
-            (*ppResponse)->setField(VID_SESSION_KEY, ucKeyBuffer, (UINT32)size);
-            (*ppResponse)->setField(VID_KEY_LENGTH, (WORD)(*ppCtx)->getKeyLength());
-            
-            int ivLength = EVP_CIPHER_iv_length(s_ciphers[(*ppCtx)->getCipher()]());
-            if ((ivLength <= 0) || (ivLength > EVP_MAX_IV_LENGTH))
-               ivLength = EVP_MAX_IV_LENGTH;
-            size = RSA_public_encrypt(ivLength, (*ppCtx)->getIV(), ucKeyBuffer, pServerKey, RSA_PKCS1_OAEP_PADDING);
-            (*ppResponse)->setField(VID_SESSION_IV, ucKeyBuffer, (UINT32)size);
-            (*ppResponse)->setField(VID_IV_LENGTH, (WORD)ivLength);
-
-            (*ppResponse)->setField(VID_CIPHER, (WORD)(*ppCtx)->getCipher());
-            RSA_free(pServerKey);
-            dwResult = RCC_SUCCESS;
+            // Encrypt key
+            int size = msg->getFieldAsBinary(VID_PUBLIC_KEY, ucKeyBuffer, KEY_BUFFER_SIZE);
+            pBufPos = ucKeyBuffer;
+            pServerKey = d2i_RSAPublicKey(NULL, (OPENSSL_CONST BYTE **)&pBufPos, size);
+            if (pServerKey != NULL)
+            {
+               (*ppResponse)->setField(VID_RCC, RCC_SUCCESS);
+               
+               size = RSA_public_encrypt((*ppCtx)->getKeyLength(), (*ppCtx)->getSessionKey(), ucKeyBuffer, pServerKey, RSA_PKCS1_OAEP_PADDING);
+               (*ppResponse)->setField(VID_SESSION_KEY, ucKeyBuffer, (UINT32)size);
+               (*ppResponse)->setField(VID_KEY_LENGTH, (WORD)(*ppCtx)->getKeyLength());
+               
+               int ivLength = EVP_CIPHER_iv_length(s_ciphers[(*ppCtx)->getCipher()]());
+               if ((ivLength <= 0) || (ivLength > EVP_MAX_IV_LENGTH))
+                  ivLength = EVP_MAX_IV_LENGTH;
+               size = RSA_public_encrypt(ivLength, (*ppCtx)->getIV(), ucKeyBuffer, pServerKey, RSA_PKCS1_OAEP_PADDING);
+               (*ppResponse)->setField(VID_SESSION_IV, ucKeyBuffer, (UINT32)size);
+               (*ppResponse)->setField(VID_IV_LENGTH, (WORD)ivLength);
+
+               (*ppResponse)->setField(VID_CIPHER, (WORD)(*ppCtx)->getCipher());
+               RSA_free(pServerKey);
+               dwResult = RCC_SUCCESS;
+            }
+            else
+            {
+               (*ppResponse)->setField(VID_RCC, RCC_INVALID_PUBLIC_KEY);
+               dwResult = RCC_INVALID_PUBLIC_KEY;
+            }
          }
          else
          {
-            (*ppResponse)->setField(VID_RCC, RCC_INVALID_PUBLIC_KEY);
-            dwResult = RCC_INVALID_PUBLIC_KEY;
+            (*ppResponse)->setField(VID_RCC, RCC_ENCRYPTION_ERROR);
+            dwResult = RCC_ENCRYPTION_ERROR;
          }
       }
    }
index c70e17a..b455ef4 100644 (file)
@@ -17,7 +17,7 @@
 ** along with this program; if not, write to the Free Software
 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 **
-** fILE: library.cpp
+** File: library.cpp
 **
 **/
 
@@ -64,7 +64,7 @@ BOOL NXSL_Library::addScript(UINT32 dwId, const TCHAR *pszName, NXSL_Program *pS
 
    m_dwNumScripts++;
    m_ppScriptList = (NXSL_Program **)realloc(m_ppScriptList, sizeof(NXSL_Program *) * m_dwNumScripts);
-   m_ppszNames = (TCHAR **)realloc(m_ppszNames, sizeof(char *) * m_dwNumScripts);
+   m_ppszNames = (TCHAR **)realloc(m_ppszNames, sizeof(TCHAR *) * m_dwNumScripts);
    m_pdwIdList = (UINT32 *)realloc(m_pdwIdList, sizeof(UINT32) * m_dwNumScripts);
    m_ppScriptList[i] = pScript;
    m_ppszNames[i] = _tcsdup(pszName);
index 2196417..d6c639c 100644 (file)
@@ -383,7 +383,7 @@ BOOL DCItem::saveToDB(DB_HANDLE hdb)
    DBBind(hStmt, 28, DB_SQLTYPE_TEXT, m_comments, DB_BIND_STATIC);
        DBBind(hStmt, 29, DB_SQLTYPE_INTEGER, m_id);
 
-   BOOL bResult = DBExecute(hStmt);
+   bool bResult = DBExecute(hStmt);
        DBFreeStatement(hStmt);
 
    // Save thresholds
@@ -433,7 +433,7 @@ BOOL DCItem::saveToDB(DB_HANDLE hdb)
          DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, m_id);
          DBBind(hStmt, 2, DB_SQLTYPE_TEXT, m_prevRawValue.getString(), DB_BIND_STATIC, 255);
          DBBind(hStmt, 3, DB_SQLTYPE_INTEGER, (INT64)m_tPrevValueTimeStamp);
-         BOOL bResult = DBExecute(hStmt);
+         bResult = DBExecute(hStmt);
          DBFreeStatement(hStmt);
       }
       DBFreeResult(hResult);
index f5665c9..ce8cf13 100644 (file)
@@ -859,7 +859,7 @@ static EVENT_TEMPLATE *FindEventTemplate(UINT32 eventCode)
 static BOOL RealPostEvent(Queue *queue, UINT32 eventCode, UINT32 sourceId,
                           const TCHAR *userTag, const char *format, const TCHAR **names, va_list args)
 {
-   EVENT_TEMPLATE *pEventTemplate;
+   EVENT_TEMPLATE *pEventTemplate = NULL;
    Event *pEvent;
    BOOL bResult = FALSE;
 
index 4ff7357..8a7fc50 100644 (file)
@@ -655,7 +655,7 @@ void Interface::updatePingData()
                                   {
                   DbgPrintf(7, _T("Interface::updatePingData: proxy response: \"%s\""), buffer);
                                           TCHAR *eptr;
-                                          long value = _tcstol(buffer, &eptr, 10);
+                                          value = _tcstol(buffer, &eptr, 10);
                   if (*eptr != 0)
                   {
                      value = -1;
index 16535d8..0aa5cf9 100644 (file)
@@ -8779,6 +8779,10 @@ void ClientSession::SaveAgentConfig(NXCPMessage *pRequest)
                }
                msg.setField(VID_SEQUENCE_NUMBER, dwSeqNum);
             }
+            else
+            {
+               dwSeqNum = pRequest->getFieldAsUInt32(VID_SEQUENCE_NUMBER);
+            }
             _sntprintf(pszQuery, qlen, _T("INSERT INTO agent_configs (config_id,config_name,config_filter,config_file,sequence_number) VALUES (%d,'%s','%s','%s',%d)"),
                       dwCfgId, pszEscName, pszEscFilter, pszEscText, dwSeqNum);
          }
@@ -9977,20 +9981,31 @@ void ClientSession::saveGraph(NXCPMessage *pRequest)
 
             int nACLSize;
             GRAPH_ACL_ENTRY *pACL = LoadGraphACL(hdb, 0, &nACLSize);
-            UINT32 *pdwUsers, *pdwRights;
-            pdwUsers = (UINT32 *)malloc(sizeof(UINT32) * nACLSize);
-            pdwRights = (UINT32 *)malloc(sizeof(UINT32) * nACLSize);
-            // ACL for graph
-            for(int j = 0; j < nACLSize; j++)
+            if ((pACL != NULL) && (nACLSize > 0))
             {
-               pdwUsers[j] = pACL[j].dwUserId;
-               pdwRights[j] = pACL[j].dwAccess;
+               UINT32 *pdwUsers = (UINT32 *)malloc(sizeof(UINT32) * nACLSize);
+               UINT32 *pdwRights = (UINT32 *)malloc(sizeof(UINT32) * nACLSize);
+               // ACL for graph
+               for(int j = 0; j < nACLSize; j++)
+               {
+                  pdwUsers[j] = pACL[j].dwUserId;
+                  pdwRights[j] = pACL[j].dwAccess;
+               }
+               msg.setField(dwId++, nACLSize);
+               msg.setFieldFromInt32Array(dwId++, nACLSize, pdwUsers);
+               msg.setFieldFromInt32Array(dwId++, nACLSize, pdwRights);
+               
+               free(pdwUsers);
+               free(pdwRights);
+            }
+            else
+            {
+               msg.setField(dwId++, (INT32)0);
+               msg.setFieldFromInt32Array(dwId++, 0, &graphId);
+               msg.setFieldFromInt32Array(dwId++, 0, &graphId);
             }
-            msg.setField(dwId++, nACLSize);
-            msg.setFieldFromInt32Array(dwId++, nACLSize, pdwUsers);
-            msg.setFieldFromInt32Array(dwId++, nACLSize, pdwRights);
             msg.setField(VID_NUM_GRAPHS, 1);
-
+            
             NotifyClientGraphUpdate(&update, graphId);
                        }
                        else
index 61c56bf..2298340 100644 (file)
@@ -201,6 +201,7 @@ int main(int argc, char *argv[])
                break;
             case '?':
                bStart = FALSE;
+               iError = 1;
                break;
             default:
                break;