]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Various improvements to error handling found by Coverity.
authorVictor Julien <victor@inliniac.net>
Wed, 29 Feb 2012 13:32:32 +0000 (14:32 +0100)
committerVictor Julien <victor@inliniac.net>
Wed, 29 Feb 2012 13:32:32 +0000 (14:32 +0100)
src/log-httplog.c
src/log-pcap.c
src/runmode-af-packet.c
src/runmode-pcap.c
src/runmode-pfring.c
src/source-af-packet.c
src/source-pcap.c
src/tm-threads.c
src/util-mem.h

index 1f5fc56daa9154a4a87e57330ec8e4bdeb2ce372..145e3591f04e9ed8f7e9519a9640ba61d2553967 100644 (file)
@@ -403,12 +403,9 @@ void LogHttpLogExitPrintStats(ThreadVars *tv, void *data) {
  * */
 OutputCtx *LogHttpLogInitCtx(ConfNode *conf)
 {
-    LogFileCtx* file_ctx=LogFileNewCtx();
-
-    if(file_ctx == NULL)
-    {
-        SCLogError(SC_ERR_HTTP_LOG_GENERIC, "LogHttpLogInitCtx: Couldn't "
-                   "create new file_ctx");
+    LogFileCtx* file_ctx = LogFileNewCtx();
+    if(file_ctx == NULL) {
+        SCLogError(SC_ERR_HTTP_LOG_GENERIC, "couldn't create new file_ctx");
         return NULL;
     }
 
@@ -417,10 +414,15 @@ OutputCtx *LogHttpLogInitCtx(ConfNode *conf)
         return NULL;
     }
 
-    LogHttpFileCtx *httplog_ctx = SCCalloc(1, sizeof(LogHttpFileCtx));
-    if (httplog_ctx == NULL)
+    LogHttpFileCtx *httplog_ctx = SCMalloc(sizeof(LogHttpFileCtx));
+    if (httplog_ctx == NULL) {
+        LogFileFreeCtx(file_ctx);
         return NULL;
+    }
+    memset(httplog_ctx, 0x00, sizeof(LogHttpFileCtx));
+
     httplog_ctx->file_ctx = file_ctx;
+
     const char *extended = ConfNodeLookupChildValue(conf, "extended");
     if (extended == NULL) {
         httplog_ctx->flags |= LOG_HTTP_DEFAULT;
@@ -431,8 +433,12 @@ OutputCtx *LogHttpLogInitCtx(ConfNode *conf)
     }
 
     OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
-    if (output_ctx == NULL)
+    if (output_ctx == NULL) {
+        LogFileFreeCtx(file_ctx);
+        SCFree(httplog_ctx);
         return NULL;
+    }
+
     output_ctx->data = httplog_ctx;
     output_ctx->DeInit = LogHttpLogDeInitCtx;
 
index 78e1b1afd5bd20a18b7c9e3282f0998a66784819..0ec9bb93cce96786408dcadcc8cd61a680ad0fe7 100644 (file)
@@ -171,6 +171,18 @@ int PcapLogCloseFile(ThreadVars *t, PcapLogData *pl) {
     return 0;
 }
 
+static void PcapFileNameFree(PcapFileName *pf) {
+    if (pf != NULL) {
+        if (pf->filename != NULL) {
+            SCFree(pf->filename);
+        }
+        if (pf->dirname != NULL) {
+            SCFree(pf->dirname);
+        }
+        SCFree(pf);
+    }
+}
+
 /**
  *  \brief Function to rotate pcaplog file
  *
@@ -199,9 +211,8 @@ int PcapLogRotateFile(ThreadVars *t, PcapLogData *pl) {
                  "failed to remove log file %s: %s",
                  pf->filename, strerror( errno ));
              TAILQ_REMOVE(&pcap_file_list, pf, next);
-             if (pf != NULL)
-                 free(pf);
 
+             PcapFileNameFree(pf);
              return -1;
          }
          else {
@@ -226,16 +237,15 @@ int PcapLogRotateFile(ThreadVars *t, PcapLogData *pl) {
                               "failed to remove sguil log %s: %s",
                               pf->dirname, strerror( errno ));
                           TAILQ_REMOVE(&pcap_file_list, pf, next);
-                          if (pf != NULL)
-                              free(pf);
 
+                          PcapFileNameFree(pf);
                           return -1;
                       }
                  }
              }
+
              TAILQ_REMOVE(&pcap_file_list, pf, next);
-             if (pf != NULL)
-                 free(pf);
+             PcapFileNameFree(pf);
 
              pl->file_cnt--;
          }
@@ -619,7 +629,6 @@ int PcapLogOpenFileCtx(PcapLogData *pl)
     memset(&ts, 0x00, sizeof(struct timeval));
     TimeGet(&ts);
 
-
     /* Place to store the name of our PCAP file */
     PcapFileName *pf = SCMalloc(sizeof(PcapFileName));
     if (pf == NULL) {
@@ -646,12 +655,7 @@ int PcapLogOpenFileCtx(PcapLogData *pl)
 #endif
         if ((pf->dirname = SCStrdup(dirfull)) == NULL) {
             SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory for directory name");
-            return -1;
-        }
-
-        if (strlen(pf->dirname) == 0) {
-            SCFree(pf->dirname);
-            return -1;
+            goto error;
         }
 
         if (pl->timestamp_format == TS_FORMAT_SEC) {
@@ -671,10 +675,14 @@ int PcapLogOpenFileCtx(PcapLogData *pl)
 
     if ((pf->filename = SCStrdup(pl->filename)) == NULL) {
         SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory. For filename");
-        return -1;
+        goto error;
     }
-    SCLogDebug("Opening pcap file log %s\n", pf->filename);
+    SCLogDebug("Opening pcap file log %s", pf->filename);
     TAILQ_INSERT_TAIL(&pcap_file_list, pf, next);
 
     return 0;
+
+error:
+    PcapFileNameFree(pf);
+    return -1;
 }
index 28254093ede10d3d059dedeff8da9d54e4841957..1eb9c2e925899813058180750100b394a8800563 100644 (file)
@@ -110,13 +110,15 @@ void *ParseAFPConfig(const char *iface)
     intmax_t value;
     int boolval;
 
-    if (iface == NULL) {
+    if (aconf == NULL) {
         return NULL;
     }
 
-    if (aconf == NULL) {
+    if (iface == NULL) {
+        SCFree(aconf);
         return NULL;
     }
+
     strlcpy(aconf->iface, iface, sizeof(aconf->iface));
     aconf->threads = 1;
     SC_ATOMIC_INIT(aconf->ref);
@@ -190,6 +192,7 @@ void *ParseAFPConfig(const char *iface)
         aconf->cluster_type = PACKET_FANOUT_CPU;
     } else {
         SCLogError(SC_ERR_INVALID_CLUSTER_TYPE,"invalid cluster-type %s",tmpctype);
+        SCFree(aconf);
         return NULL;
     }
 
index 96a4893a31c37021184be29e05059096443cd07c..c723279dd2909038893194121d51e4bdeb1a8646 100644 (file)
@@ -85,13 +85,15 @@ void *ParsePcapConfig(const char *iface)
     char *tmpctype;
     intmax_t value;
 
-    if (iface == NULL) {
+    if (aconf == NULL) {
         return NULL;
     }
 
-    if (aconf == NULL) {
+    if (iface == NULL) {
+        SCFree(aconf);
         return NULL;
     }
+
     strlcpy(aconf->iface, iface, sizeof(aconf->iface));
 
     aconf->buffer_size = 0;
index bbc7eaaeb609f778c1145fd02ebee8668edff560..567b539834224dc2359d27816c6c1dd94cb45dd8 100644 (file)
@@ -114,13 +114,15 @@ void *OldParsePfringConfig(const char *iface)
     cluster_type default_ctype = CLUSTER_ROUND_ROBIN;
 #endif
 
-    if (iface == NULL) {
+    if (pfconf == NULL) {
         return NULL;
     }
 
-    if (pfconf == NULL) {
+    if (iface == NULL) {
+        SCFree(pfconf);
         return NULL;
     }
+
     strlcpy(pfconf->iface, iface, sizeof(pfconf->iface));
     pfconf->threads = 1;
     pfconf->cluster_id = 1;
@@ -167,6 +169,7 @@ void *OldParsePfringConfig(const char *iface)
         pfconf->ctype = (cluster_type)tmpctype;
     } else {
         SCLogError(SC_ERR_INVALID_CLUSTER_TYPE,"invalid cluster-type %s",tmpctype);
+        SCFree(pfconf);
         return NULL;
     }
 #endif
@@ -203,13 +206,15 @@ void *ParsePfringConfig(const char *iface)
     char *bpf_filter = NULL;
 #endif /* HAVE_PFRING_SET_BPF_FILTER */
 
-    if (iface == NULL) {
+    if (pfconf == NULL) {
         return NULL;
     }
 
-    if (pfconf == NULL) {
+    if (iface == NULL) {
+        SCFree(pfconf);
         return NULL;
     }
+
     memset(pfconf, 0, sizeof(PfringIfaceConfig));
     strlcpy(pfconf->iface, iface, sizeof(pfconf->iface));
     pfconf->threads = 1;
@@ -312,6 +317,7 @@ void *ParsePfringConfig(const char *iface)
             SCLogError(SC_ERR_INVALID_CLUSTER_TYPE,
                        "invalid cluster-type %s",
                        tmpctype);
+            SCFree(pfconf);
             return NULL;
         }
     }
index 9378fe0a8369c12b77a0c099e5b477ad75eece6b..e42b0b23242e10fc754cc0af266cf31580091f5f 100644 (file)
@@ -878,6 +878,7 @@ TmEcode ReceiveAFPThreadInit(ThreadVars *tv, void *initdata, void **data) {
     ptv->livedev = LiveGetDevice(ptv->iface);
     if (ptv->livedev == NULL) {
         SCLogError(SC_ERR_INVALID_VALUE, "Unable to find Live device");
+        SCFree(ptv);
         SCReturnInt(TM_ECODE_FAILED);
     }
 
index b5b2ed27a5907656054ea8495c0c167c44ce4348..4d3f59a876a3df1e273ee35841b02ff1d7accb14 100644 (file)
@@ -341,6 +341,7 @@ TmEcode ReceivePcapThreadInit(ThreadVars *tv, void *initdata, void **data) {
     ptv->livedev = LiveGetDevice(pcapconfig->iface);
     if (ptv->livedev == NULL) {
         SCLogError(SC_ERR_INVALID_VALUE, "Unable to find Live device");
+        SCFree(ptv);
         SCReturnInt(TM_ECODE_FAILED);
     }
 
index 75cf12d05fff4bcbd6bd7d2c7acf58916604f18c..bf1bb6912d723420af4700cac3e423f1bba97017 100644 (file)
@@ -1216,7 +1216,10 @@ ThreadVars *TmThreadCreate(char *name, char *inq_name, char *inqh_name,
     return tv;
 
 error:
-    printf("ERROR: failed to setup a thread.\n");
+    SCLogError(SC_ERR_THREAD_CREATE, "failed to setup a thread");
+
+    if (tv != NULL)
+        SCFree(tv);
     return NULL;
 }
 
index ac9c1ba2aaa03e9fb7c025a61c80b2e78c05eaf6..6aedaa1fcf37500ba759570b1dac7a04a9eb1b58 100644 (file)
@@ -157,9 +157,9 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage);
     ptrmem = malloc((a)); \
     if (ptrmem == NULL) { \
         if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
-            uintmax_t size = (uintmax_t)(a); \
+            uintmax_t scmalloc_size_ = (uintmax_t)(a); \
             SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying " \
-                "to allocate %"PRIuMAX" bytes", strerror(errno), size); \
+                "to allocate %"PRIuMAX" bytes", strerror(errno), scmalloc_size_); \
             SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
             exit(EXIT_FAILURE); \
         } \