Browse code

Fix Valgrind. Fix bug with clamd reloading database, forcing a stats submission.

Shawn Webb authored on 2014/01/23 11:15:19
Showing 4 changed files
... ...
@@ -546,6 +546,9 @@ int cli_checkfp(unsigned char *digest, size_t size, cli_ctx *ctx)
546 546
     if (ctx->engine->cb_stats_add_sample)
547 547
         ctx->engine->cb_stats_add_sample(cli_get_last_virus(ctx), digest, size, &sections, ctx->engine->stats_data);
548 548
 
549
+    if (sections.sections)
550
+        free(sections.sections);
551
+
549 552
     return CL_VIRUS;
550 553
 }
551 554
 
... ...
@@ -699,7 +699,6 @@ struct cl_settings *cl_engine_settings_copy(const struct cl_engine *engine)
699 699
     settings->cb_meta = engine->cb_meta;
700 700
     settings->engine_options = engine->engine_options;
701 701
 
702
-    settings->stats_data = engine->stats_data;
703 702
     settings->cb_stats_add_sample = engine->cb_stats_add_sample;
704 703
     settings->cb_stats_remove_sample = engine->cb_stats_remove_sample;
705 704
     settings->cb_stats_decrement_count = engine->cb_stats_decrement_count;
... ...
@@ -714,6 +713,8 @@ struct cl_settings *cl_engine_settings_copy(const struct cl_engine *engine)
714 714
 
715 715
 int cl_engine_settings_apply(struct cl_engine *engine, const struct cl_settings *settings)
716 716
 {
717
+    cli_intel_t *intel;
718
+
717 719
     engine->ac_only = settings->ac_only;
718 720
     engine->ac_mindepth = settings->ac_mindepth;
719 721
     engine->ac_maxdepth = settings->ac_maxdepth;
... ...
@@ -762,7 +763,12 @@ int cl_engine_settings_apply(struct cl_engine *engine, const struct cl_settings
762 762
     engine->cb_hash = settings->cb_hash;
763 763
     engine->cb_meta = settings->cb_meta;
764 764
 
765
-    engine->stats_data = settings->stats_data;
765
+    intel = (cli_intel_t *)cli_calloc(1, sizeof(cli_intel_t));
766
+    intel->engine = engine;
767
+    intel->maxsamples = STATS_MAX_SAMPLES;
768
+    intel->maxmem = STATS_MAX_MEM;
769
+
770
+    engine->stats_data = (void *)intel;
766 771
     engine->cb_stats_add_sample = settings->cb_stats_add_sample;
767 772
     engine->cb_stats_remove_sample = settings->cb_stats_remove_sample;
768 773
     engine->cb_stats_decrement_count = settings->cb_stats_decrement_count;
... ...
@@ -3241,9 +3241,17 @@ int cl_engine_free(struct cl_engine *engine)
3241 3241
         engine->cb_stats_submit(engine, engine->stats_data);
3242 3242
 
3243 3243
 #ifdef CL_THREAD_SAFE
3244
-    pthread_mutex_destroy(&(((cli_intel_t *)(engine->stats_data))->mutex));
3244
+    if (engine->stats_data) {
3245
+        cli_intel_t *intel = (cli_intel_t *)(engine->stats_data);
3246
+
3247
+        pthread_mutex_destroy(&(intel->mutex));
3248
+    }
3249
+
3245 3250
     pthread_mutex_unlock(&cli_ref_mutex);
3246 3251
 #endif
3252
+    if (engine->stats_data)
3253
+        free(engine->stats_data);
3254
+
3247 3255
     if(engine->root) {
3248 3256
 	for(i = 0; i < CLI_MTARGETS; i++) {
3249 3257
 	    if((root = engine->root[i])) {
... ...
@@ -216,8 +216,16 @@ void clamav_stats_add_sample(const char *virname, const unsigned char *md5, size
216 216
         if (sections && sections->nsections && !(sample->sections)) {
217 217
             /* Copy the section data that has already been allocated. We don't care if calloc fails; just skip copying if it does. */
218 218
             sample->sections = calloc(1, sizeof(stats_section_t));
219
-            if ((sample->sections))
220
-                memcpy(sample->sections, sections, sizeof(stats_section_t));
219
+            if ((sample->sections)) {
220
+                sample->sections->sections = calloc(sections->nsections, sizeof(struct cli_section_hash));
221
+                if ((sample->sections->sections)) {
222
+                    memcpy(sample->sections->sections, sections->sections, sections->nsections * sizeof(struct cli_section_hash));
223
+                    sample->sections->nsections = sections->nsections;
224
+                } else {
225
+                    free(sample->sections);
226
+                    sample->sections = NULL;
227
+                }
228
+            }
221 229
         }
222 230
     }
223 231
 
... ...
@@ -261,6 +269,10 @@ void clamav_stats_flush(struct cl_engine *engine, void *cbdata)
261 261
 
262 262
     intel->samples = NULL;
263 263
     intel->nsamples = 0;
264
+    if (intel->hostid) {
265
+        free(intel->hostid);
266
+        intel->hostid = NULL;
267
+    }
264 268
 
265 269
 #ifdef CL_THREAD_SAFE
266 270
     err = pthread_mutex_unlock(&(intel->mutex));
... ...
@@ -347,6 +359,11 @@ void clamav_stats_submit(struct cl_engine *engine, void *cbdata)
347 347
         submit_post(STATS_HOST, STATS_PORT, "PUT", "/clamav/1/submit/stats", json);
348 348
         free(json);
349 349
     }
350
+
351
+    if (myintel.hostid && !(intel->hostid)) {
352
+        free(myintel.hostid);
353
+        myintel.hostid = NULL;
354
+    }
350 355
 }
351 356
 
352 357
 void clamav_stats_remove_sample(const char *virname, const unsigned char *md5, size_t size, void *cbdata)